ios点击改变uiview背景颜色

ios点击改变uiview背景颜色是一个再常见不过的需求。第一反应应该不麻烦,于是写了个第一个版本

@interface RespondentUIView()
{
    UIColor * bgColor;
}
@end

@implementation RespondentUIView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { bgColor = self.backgroundColor; self.backgroundColor = White_Down; } - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { self.backgroundColor = bgColor; }
@end
 

好像也能用。但是一会问题来了。发现touchesBegan很延时很严重的样子。于是有了第二个版本。

@interface RespondentUIView()
{
    UIColor * bgColor;
}
@end

@implementation RespondentUIView

- (void)willMoveToSuperview:(UIView *)newSuperview
{
    UILongPressGestureRecognizer *tapRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(changeColor:)];
    tapRecognizer.minimumPressDuration = 0;//Up to you;
    tapRecognizer.cancelsTouchesInView = NO;
    [self addGestureRecognizer:tapRecognizer];
}

-(void) changeColor:(UIGestureRecognizer *)gestureRecognizer{
    
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
    {
        bgColor = self.backgroundColor;
        self.backgroundColor = White_Down;
    }
    else if (gestureRecognizer.state == UIGestureRecognizerStateEnded)
    {
        self.backgroundColor = bgColor;
    }
}
@end

用UILongPressGestureRecognizer一下子就好多了,点击反应嗖嗖的。一会又发现问题了,有的view需要注册点击事件,一个uiView注册多个UIGestureRecognizer时,总有一个不响应。真是shit。于是又google一通,发现了一个uiview使用多个

UIGestureRecognizer的方法,于是有了第三版。

@interface RespondentUIView()<UIGestureRecognizerDelegate>
{
    UIColor * bgColor;
}
@end

@implementation RespondentUIView

- (void)willMoveToSuperview:(UIView *)newSuperview
{
    UILongPressGestureRecognizer *tapRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(changeColor:)];
    tapRecognizer.minimumPressDuration = 0;//Up to you;
    tapRecognizer.cancelsTouchesInView = NO;
    tapRecognizer.delegate = self;
    [self addGestureRecognizer:tapRecognizer];
}

-(void) changeColor:(UIGestureRecognizer *)gestureRecognizer{
    
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
    {
        bgColor = self.backgroundColor;
        self.backgroundColor = White_Down;
    }
    else if (gestureRecognizer.state == UIGestureRecognizerStateEnded)
    {
        self.backgroundColor = bgColor;
    }
}


//下面三个函数用于多个GestureRecognizer 协同作业,避免按下的手势影响了而别的手势不响应
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return YES;
}
@end

这下感觉整个世界清静了。顺带提一下

tapRecognizer.cancelsTouchesInView = NO;
这是一个很犀利的属性。一般ios响应链传递的顺序是先交给UIView的UIGestureRecognizer处理。如果它处理了,就把事件丢掉了,于是UIView上面的touchesBegan和touchesEnded等优先级比较低的UIResponder就没机会响应了。把cancelsTouchesInView置为NO。于是大家就能和谐相处了。

转载于:https://www.cnblogs.com/chyl411/p/5384445.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值