UIButton与手势混搭的结果【入门】
2020-03-12 本文已影响0人
码农二哥
只有UIButton
@interface WUIButton : UIButton
@end
@implementation WUIButton
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
NSLog(@"xiaowei.li:%s", __FUNCTION__);
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
NSLog(@"xiaowei.li:%s", __FUNCTION__);
}
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[super touchesCancelled:touches withEvent:event];
NSLog(@"xiaowei.li:%s", __FUNCTION__);
}
@end
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.wButton = [[WUIButton alloc] initWithFrame:CGRectMake(100, 100, 150, 44)];
self.wButton.backgroundColor = UIColor.purpleColor;
[self.wButton setTitle:@"test-normal" forState:UIControlStateNormal];
[self.wButton setTitle:@"test-highlighted" forState:UIControlStateHighlighted];
[self.wButton addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.wButton];
}
- (void)buttonClicked
{
NSLog(@"xiaowei.li:%s", __FUNCTION__);
}
- 正常点击以下的输入如下:
xiaowei.li:-[WUIButton touchesBegan:withEvent:]
xiaowei.li:-[ViewController buttonClicked]
xiaowei.li:-[WUIButton touchesEnded:withEvent:]
UIButton+手势
- WUIButton实现逻辑不变
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.wButton = [[WUIButton alloc] initWithFrame:CGRectMake(100, 100, 150, 44)];
self.wButton.backgroundColor = UIColor.purpleColor;
[self.wButton setTitle:@"test-normal" forState:UIControlStateNormal];
[self.wButton setTitle:@"test-highlighted" forState:UIControlStateHighlighted];
[self.wButton addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.wButton];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
[self.wButton addGestureRecognizer:tap];
}
- (void)buttonClicked
{
NSLog(@"xiaowei.li:%s", __FUNCTION__);
}
- (void)tap
{
NSLog(@"xiaowei.li:%s", __FUNCTION__);
}
- 结果会怎样?
xiaowei.li:-[WUIButton touchesBegan:withEvent:]
xiaowei.li:-[ViewController tap]
xiaowei.li:-[WUIButton touchesCancelled:withEvent:]
如果换成pan手势呢?结果会怎样?
- 普通点击效果(点击时不要移动):
xiaowei.li:-[WUIButton touchesBegan:withEvent:]
xiaowei.li:-[ViewController buttonClicked]
xiaowei.li:-[WUIButton touchesEnded:withEvent:]
- 点击后稍微移动以下:
xiaowei.li:-[WUIButton touchesBegan:withEvent:]
xiaowei.li:-[ViewController panCallback]
xiaowei.li:-[WUIButton touchesCancelled:withEvent:]
xiaowei.li:-[ViewController panCallback]
xiaowei.li:-[ViewController panCallback]
...