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+手势


- (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]
...
上一篇 下一篇

猜你喜欢

热点阅读