突出显示类似于UIButton的UIView [英] Highlighting a UIView similar to UIButton

查看:138
本文介绍了突出显示类似于UIButton的UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIView,其中包含许多子视图和Tap关联的手势,我想模仿它具有触摸效果。也就是说,当点击发生时,我想显示容器视图具有不同的背景颜色,并且任何子视图UILabel的文本也看起来都突出显示。

I have a UIView with a number of subviews and a Tap gesture recognized associated and I want to mimic it having a 'touch' effect. That is, when the tap happens, I want to show the container view to have a different background color and the text of any subview UILabels to also look highlighted.

当我从UITapGestureRecognizer接收点击事件,我可以很好地改变背景颜色甚至将UILabel设置为 [label setHighlighted:YES];

When I receive the tap event from UITapGestureRecognizer, I can change the background color just fine and even set the UILabel to [label setHighlighted:YES];

出于各种原因,我无法将UIView更改为UIControl。

For various reasons, I cannot change the UIView to UIControl.

但如果我添加一些UIViewAnimation来恢复突出显示,则没有任何反应。有什么建议吗?

But if I add some UIViewAnimation to revert the highlighting, nothing happens. Any suggestions?

    - (void)handleTapGesture:(UITapGestureRecognizer *)tapGesture {
      [label setHighlighted:YES]; // change the label highlight property

[UIView animateWithDuration:0.20 
                          delay:0.0 
                        options:UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         [containerView setBackgroundColor:originalBgColor];          
                         [label setHighlighted:NO]; // Problem: don't see the highlight reverted
                     } completion:^(BOOL finished) {                         
                         // nothing to handle here
                     }];    
}


推荐答案

setHighlighted不是动画查看属性。另外,你说的是两件相反的事情:你将亮度设置为YES而NO设置为同一口气。结果是没有任何反应,因为没有整体变化。

setHighlighted isn't an animatable view property. Plus, you're saying two opposite things: you setHighlighted to YES and NO in the same breath. The result will be that nothing happens because there's no overall change.

使用完成处理程序或延迟性能来更改突出显示以后

Use the completion handler or delayed performance to change the highlight later.

编辑:

你说尝试过但都没有工作过。也许你需要澄清延迟表现的意思。我刚试过这个并且效果很好:

You say "tried both but neither worked." Perhaps you need clarification on what I mean by delayed performance. I just tried this and it works perfectly:

- (void) tapped: (UIGestureRecognizer*) g {
    label.highlighted = YES;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.2 * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        label.highlighted = NO;
    });
}

标签必须有不同的 textColor vs其 highlightedTextColor 以便可见的事情发生。

The label must have a different textColor vs its highlightedTextColor so that something visible happens.

这篇关于突出显示类似于UIButton的UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆