简体   繁体   English

突出显示类似于 UIButton 的 UIView

[英]Highlighting a UIView similar to UIButton

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.我有一个带有多个子视图的 UIView 和一个识别出关联的 Tap 手势,我想模仿它具有“触摸”效果。 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.也就是说,当点击发生时,我想显示容器视图具有不同的背景颜色,并且任何子视图 UILabels 的文本也看起来突出显示。

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];当我从 UITapGestureRecognizer 收到点击事件时,我可以很好地改变背景颜色,甚至将 UILabel 设置为[label setHighlighted:YES];

For various reasons, I cannot change the UIView to UIControl.由于各种原因,我无法将 UIView 更改为 UIControl。

But if I add some UIViewAnimation to revert the highlighting, nothing happens.但是,如果我添加一些 UIViewAnimation 来恢复突出显示,则什么也不会发生。 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 isn't an animatable view property. setHighlighted 不是动画视图属性。 Plus, you're saying two opposite things: you setHighlighted to YES and NO in the same breath.另外,您说的是两件相反的事情:您同时将Highlighted 设置为YES 和NO。 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 .使用完成处理程序或延迟性能稍后更改突出显示。

EDIT:编辑:

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;
    });
}

The label must have a different textColor vs its highlightedTextColor so that something visible happens.标签必须具有不同的textColor与它的highlightedTextColor以便发生可见的事情。

Swift 4斯威夫特 4

You can use a custom view like this for example:例如,您可以使用这样的自定义视图

class HighlightView: UIView {

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        DispatchQueue.main.async {
            self.alpha = 1.0
            UIView.animate(withDuration: 0.4, delay: 0.0, options: .curveLinear, animations: {
                self.alpha = 0.5
            }, completion: nil)
        }
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        DispatchQueue.main.async {
            self.alpha = 0.5
            UIView.animate(withDuration: 0.4, delay: 0.0, options: .curveLinear, animations: {
                self.alpha = 1.0
            }, completion: nil)
        }
    }

    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        DispatchQueue.main.async {
            self.alpha = 0.5
            UIView.animate(withDuration: 0.4, delay: 0.0, options: .curveLinear, animations: {
                self.alpha = 1.0
            }, completion: nil)
        }
    }
}

Just play around with the duration and animations as you like.随心所欲地玩弄持续时间和动画。 In the end you can use it instead of UIView and whenever you will click on it, it will change its alpha value so it will look like a highlight.最后,您可以使用它代替UIView ,无论何时单击它,它都会更改其alpha值,因此它看起来像一个亮点。

Simple solution is override tap gesture regognizer like below:简单的解决方案是覆盖点击手势识别器,如下所示:

Swift 4.x斯威夫特 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
                })
            }
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM