2012-01-05 106 views
12

我有一个UIView与一些子视图和一个点击手势识别相关联,我想模仿它有'触摸'的影响。也就是说,当轻敲发生时,我想显示容器视图具有不同的背景颜色,并且任何子视图UILabels的文本也显示为突出显示。突出显示UIView类似于UIButton

当我收到来自UITapGestureRecognizer水龙头事件,我可以改变背景颜色就好了,甚至设置的UILabel到[label setHighlighted:YES];

由于种种原因,我不能UIView的改变UIControl。

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

- (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 
        }];  
} 
+0

为什么不让它'UIButton'? – 2012-01-05 17:40:12

+0

因为它不是我拥有的代码库,还有其他的依赖关系,所以我必须将它作为UIView。 – 2012-01-05 17:41:04

+0

看看这个库:https://github.com/mta452/UIView-TouchHighlighting – 2016-07-23 16:21:25

回答

6

setHighlighted不是一个动画视图属性。另外,你说的是两个相反的东西:你把同样的气息强调为YES和NO。结果将是没有发生任何事情,因为没有整体变化。

使用完成处理程序或延迟性能更改高亮后面

编辑:

你说“两个都试过但都没有工作。”也许你需要澄清我的意思是延迟的表现。我只是想这和它完美的作品:

- (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使事情发生可见。

+0

虽然都尝试过,但都没有工作..我可能不得不找出另一种创造性的方式来做到这一点,或去重新布线现有的代码库使用UIControl。 – 2012-01-05 17:54:54

0

简单的解决方案是重写双击手势regognizer 象下面这样:

斯威夫特4.x的

class TapGestureRecognizer: UITapGestureRecognizer { 
    var highlightOnTouch = true 

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) { 
     super.touchesBegan(touches, with: event) 

     if highlightOnTouch { 
      let bgcolor = view?.backgroundColor 
      UIView.animate(withDuration: 0.1, delay: 0, options: [.allowUserInteraction, .curveLinear], animations: { 
       self.view?.backgroundColor = .lightGray 
      }) { (_) in 
       UIView.animate(withDuration: 0.1, delay: 0, options: [.allowUserInteraction, .curveLinear], animations: { 
        self.view?.backgroundColor = bgcolor 
       }) 
      } 
     } 
    } 

} 
相关问题