Ios 有没有办法在自定义UIButton中调用TouchUpInside操作?
我正在为我的应用程序编写自定义Ios 有没有办法在自定义UIButton中调用TouchUpInside操作?,ios,objective-c,uibutton,Ios,Objective C,Uibutton,我正在为我的应用程序编写自定义ui按钮。但是,我想为按钮添加一个完整的操作。这样,我可以从操作中返回BOOL,然后在按钮中执行一些代码(即显示动画),然后调用完成方法 因此,理想情况下,我希望能够做到以下几点: [button addAction:^(){ NSLog(@"Action!"); return true; } completion:^() { NSLog(@"Completion!"); return true; } forControlEvents
ui按钮。但是,我想为按钮添加一个完整的操作。这样,我可以从操作中返回BOOL
,然后在按钮中执行一些代码(即显示动画),然后调用完成方法
因此,理想情况下,我希望能够做到以下几点:
[button addAction:^(){
NSLog(@"Action!");
return true;
} completion:^() {
NSLog(@"Completion!");
return true;
} forControlEvents:UIControlEventTouchUpInside];
如何覆盖发生UIControlEventTouchUpInside
时发生的情况?或者另一个控制事件。您可以通过执行以下操作来实现这一点:
自定义按钮
@interface CustomButton : UIButton
- (void)addAction:(void (^)(CustomButton *button))action onCompletion:(void (^)(CustomButton *button))completion forControlEvents:(UIControlEvents)event;
@end
自定义按钮
#import "CustomButton.h"
@interface CustomButton ()
@property (nonatomic, copy) void(^actionHandler)(CustomButton *button);
@property (nonatomic, copy) void(^completionHandler)(CustomButton *button);
@end
@implementation CustomButton
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (void)addAction:(void (^)(CustomButton *button))action onCompletion:(void (^)(CustomButton *button))completion forControlEvents:(UIControlEvents)event
{
self.actionHandler = action;
self.completionHandler = completion;
__weak __typeof__(self) weakSelf = self;
[self addTarget:weakSelf action:@selector(buttonAction) forControlEvents:event];
}
- (void)buttonAction
{
if (self.actionHandler) {
self.actionHandler(self);
}
// This will execute right after executing action handler.
// NOTE: If action handler is dispatching task, then execution of completionHandler will not wait for completion of dispatched task
// that should handled using some notification/kvo.
if (self.completionHandler) {
self.completionHandler(self);
}
}
@end
您可以通过执行以下操作来实现这一点:
自定义按钮
@interface CustomButton : UIButton
- (void)addAction:(void (^)(CustomButton *button))action onCompletion:(void (^)(CustomButton *button))completion forControlEvents:(UIControlEvents)event;
@end
自定义按钮
#import "CustomButton.h"
@interface CustomButton ()
@property (nonatomic, copy) void(^actionHandler)(CustomButton *button);
@property (nonatomic, copy) void(^completionHandler)(CustomButton *button);
@end
@implementation CustomButton
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (void)addAction:(void (^)(CustomButton *button))action onCompletion:(void (^)(CustomButton *button))completion forControlEvents:(UIControlEvents)event
{
self.actionHandler = action;
self.completionHandler = completion;
__weak __typeof__(self) weakSelf = self;
[self addTarget:weakSelf action:@selector(buttonAction) forControlEvents:event];
}
- (void)buttonAction
{
if (self.actionHandler) {
self.actionHandler(self);
}
// This will execute right after executing action handler.
// NOTE: If action handler is dispatching task, then execution of completionHandler will not wait for completion of dispatched task
// that should handled using some notification/kvo.
if (self.completionHandler) {
self.completionHandler(self);
}
}
@end
您需要的信息在UIControl
参考/编程指南中。它不特定于ui按钮
。苹果的搜索现在很糟糕,所以我无法快速找到/粘贴任何内容,但这将为您指明正确的方向。您需要的信息在UIControl
参考/编程指南中。它不特定于ui按钮
。苹果的搜索现在糟透了,所以我无法快速找到/粘贴任何内容,但这将为您指明正确的方向。