Swift防止按钮重复点击实现+Swift如何运用Runtime

Swift防止按钮重复点击实现+Swift如何运用Runtime

做过OC开发的都知道,我们想要给一个系统的类添加一个属性我们有几种方法,比如继承,我们创建一个父类,给父类写一个属性,之后所有使用的类都采用继承该父类的方式,这样就会都拥有该属性.更高级一点的我们会用到OC的Runtime的机制,
给分类添加属性,即使用 Runtime 中的 objc_setAssociatedObject 和 objc_getAssociatedObject

此外,我们还经常使用方法交换Method Swizzling,对一个既有的类进行方法交换,从而完成一些本来不能完成的事情,比如在viewDidAppear:方法调用的时候想要打印该类,我们通常就会采用方法交换的方式

比如我之前有写一个防止按钮重复点击的分类
之前是这么写的

#import <UIKit/UIKit.h>

#ifndef xlx_defaultInterval
#define xlx_defaultInterval 0.5  //默认时间间隔
#endif

@interface UIButton (Interval)

@property (nonatomic, assign) NSTimeInterval customInterval;//自定义时间间隔

@property (nonatomic, assign) BOOL ignoreInterval;//是否忽略间隔(采用系统默认)

@end


#import "UIButton+Interval.h"
#import <objc/runtime.h>

@implementation UIButton (Interval)

static const char *xlx_customInterval = "xlx_customInterval";

- (NSTimeInterval)customInterval {
    return [objc_getAssociatedObject(self, xlx_customInterval) doubleValue];
}

- (void)setCustomInterval:(NSTimeInterval)customInterval {
    objc_setAssociatedObject(self, xlx_customInterval, @(customInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

static const char *xlx_ignoreInterval = "xlx_ignoreInterval";

-(BOOL)ignoreInterval {
    return [objc_getAssociatedObject(self, xlx_ignoreInterval) integerValue];
}

-(void)setIgnoreInterval:(BOOL)ignoreInterval {
    objc_setAssociatedObject(self, xlx_ignoreInterval, @(ignoreInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

+ (void)load{
    if ([NSStringFromClass(self.class) isEqualToString:@"UIButton"]) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            SEL systemSel = @selector(sendAction:to:forEvent:);
            SEL swizzSel = @selector(mySendAction:to:forEvent:);
            Method systemMethod = class_getInstanceMethod([self class], systemSel);
            Method swizzMethod = class_getInstanceMethod([self class], swizzSel);
            BOOL isAdd = class_addMethod(self, systemSel, method_getImplementation(swizzMethod), method_getTypeEncoding(swizzMethod));
            if (isAdd) {
                class_replaceMethod(self, swizzSel, method_getImplementation(systemMethod), method_getTypeEncoding(systemMethod));
            }else{
                method_exchangeImplementations(systemMethod, swizzMethod);
            }
        });
    }
}

- (void)mySendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event{
    if (!self
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 iOS 应用开发,通常使用 UINavigationController 来管理多个页面的转换。在使用 UINavigationController 的情况下,你可以通过调用它的 pushViewController(_:animated:) 方法来转换到另一个页面。 举个例子,假设你有一个名为 FirstViewController 的视图控制器,并且要通过按钮点击事件来转换到另一个名为 SecondViewController 的视图控制器。 首先,需要在你的故事板创建两个场景。并将场景关联到对应的视图控制器。 其次,在 FirstViewController 添加一个按钮,并在它的点击事件,调用 UINavigationController 的 pushViewController(_:animated:) 方法,将 SecondViewController 压入导航 ``` swift import UIKit class FirstViewController: UIViewController { @IBOutlet weak var transitionButton: UIButton! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } @IBAction func transitionButtonDidTapped(_ sender: Any) { let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController self.navigationController?.pushViewController(secondViewController, animated: true) } } ``` 这样你就可以通过按钮点击来在两个场景间转换了。 需要注意的是,需要在你的 Appdelegate 里面配置 Navigation 并且设置第一个页面是哪一个. ```swift let firstViewController = self.storyboard?.instantiateViewController(withIdentifier: "FirstViewController") as! FirstViewController let navigationController = UINavigationController(rootViewController: firstViewController) self.window?.rootViewController = navigationController

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值