IOS UIGestureRecognizer 手势的实用汇总

文章目录

  • UITapGestureRecognizer 的使用,判断点击哪个控件

UITapGestureRecognizer 的使用,判断点击哪个控件

UITapGestureRecognizer 的使用,判断点击哪个控件

说明:_backgroundImg 和_userHeaderImg都在_headerView上,给_headerView添加手势,判断用户点击是在_userHeaderImg还是_backgroundImg上,一个是修改头像,一个是修改背景图

//添加手势

        UITapGestureRecognizer *backgroundTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeHeaderImageAction:)];

        [_headerView addGestureRecognizer:backgroundTap];

-(void)changeHeaderImageAction :(UITapGestureRecognizer *)tap {

    CGPoint touchPoint = [tap locationInView:_headerView];
    CGRect userHeaderImageRect = [_headerView convertRect:_userHeaderImg.bounds fromView:_userHeaderImg];
    if (CGRectContainsPoint(userHeaderImageRect, touchPoint)) {
        //clicked user header image.
        _isHeaderImg=@"1";
        [self setPhotoOrTakePictureAction];
        return;
    }   

    CGRect backgroundImageRect = [_backgroundImg convertRect:_backgroundImg.bounds toView:_headerView];

    if (CGRectContainsPoint(backgroundImageRect, touchPoint)) {
        //clicked back image.
        _isHeaderImg=@"0";
        [self setPhotoOrTakePictureAction];
    }
}

你可能感兴趣的:(手势)