我无法理解Event Handling Guide for iOS-Interacting with Other User Interface Controls
中的表达方式谁能给我一些例子? THX如果您有其中一个控件的自定义子类,并且想要更改>默认操作,请将手势识别器直接附加到控件而不是>父视图。然后,手势识别器首先接收触摸事件。
答案 0 :(得分:0)
UIButton
的子类附加手势识别器。
在CustomButton.m
中#import "CustomButton.h"
@implementation CustomButton
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(handleTap)];
tapGesture.numberOfTapsRequired = 2;
[self addGestureRecognizer:tapGesture];
}
return self;
}
- (void)handleTap
{
NSLog(@"Button tapped twice");
}
在ViewController中启动按钮并添加为子视图
#import "ViewController.h"
#import "CustomButton.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CustomButton *customButton = [[CustomButton alloc] initWithFrame:CGRectMake(100.0f, 100.0f, 150.0f, 100.0f)];
[customButton setTitle:@"Tap Twice" forState:UIControlStateNormal];
customButton.backgroundColor = [UIColor grayColor];
[self.view addSubview:customButton];
}
通常按钮适用于单击。我们通过添加点击手势识别器和点击次数2来更改按钮的默认行为。