Location>code7788 >text

iOS Development Fundamentals 136 - Anti-Violence Clicks

Popularity:800 ℃/2024-07-23 14:40:00

To create a highly reusable tool class in Objective-C that prevents brute force clicking of buttons and uses a faceted programming (AOP) approach, we might consider using theAspectsThis library to implement method interception. The following are the steps to implement it:

Step 1: Introduce the Aspects library

First, it is necessary to place theAspectsIntegration into projects.Aspectsis a lightweight AOP framework that allows you to intercept class instance methods and class methods at runtime. You can add this library via CocoaPods:

pod 'Aspects'

Step 2: Create UIButton's Category

Next, create a Category for the UIButton that will be used to add anti-violent click logic.

UIButton+:

#import <UIKit/>

@interface UIButton (PreventSpam)

// Set the interval for the anti-violent click setting
- (void)setPreventSpamInterval:(NSTimeInterval)interval;

@end

UIButton+:

#import "UIButton+"
#import <objc/>
#import <Aspects/>

static const char *UIButton_preventSpamIntervalKey = "UIButton_preventSpamIntervalKey";

@implementation UIButton (PreventSpam)

- (void)setPreventSpamInterval:(NSTimeInterval)interval {
    objc_setAssociatedObject(self, UIButton_preventSpamIntervalKey, @(interval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    // utilizationAspectsPerform method interception
    [self aspect_hookSelector:@selector(sendAction:to:forEvent:) withOptions:AspectPositionInstead usingBlock:^(id<AspectInfo> aspectInfo, SEL action, id target, UIEvent *event) {
        UIButton *button = ;
        if () {
            return;
        }
         = YES;

        // Execute the original sendAction:to:forEvent: methodologies
        // 这里直接调用原始methodologies是不合适的,Because it's a post-interceptblock,We have to reinvoke original invocation
        [[aspectInfo originalInvocation] invoke];
        
        // After the delay interval,Reset to be clickable
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(interval * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
             = NO;
        });
    } error:NULL];
}

// Determine if the click should be ignored at this time,Adding Properties via Associated Objects
- (void)setIsIgnoring:(BOOL)isIgnoring {
    objc_setAssociatedObject(self, @selector(isIgnoring), @(isIgnoring), OBJC_ASSOCIATION_ASSIGN);
}

- (BOOL)isIgnoring {
    return [objc_getAssociatedObject(self, @selector(isIgnoring)) boolValue];
}

@end

utilization

Set the click interval of the button at the appropriate position:

#import "UIButton+"

[myButton setPreventSpamInterval:1.0]; // Click interval set to 1 second

take note of

  1. This example uses theAspectsThe library performs method interception, which is a runtime AOP technique. The dynamic nature of Objective-C is utilized here.AOP can help increase the maintainability and reusability of the code, but it also increases the complexity of the code, which needs to be weighed while using it.

  2. Use the associated object (objc_setAssociatedObjectcap (a poem)objc_getAssociatedObject) to dynamically add properties to the UIButton for storing whether clicks should be ignored and the interval time, a common Objective-C trick for extending the functionality of existing classes.

  3. In real projects, you may need to consider a variety of situations and details, such as handling the response to button click events when switching interfaces quickly.