繁体   English   中英

突出显示类似于 UIButton 的 UIView

[英]Highlighting a UIView similar to UIButton

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

当我从 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
                     }];    
}

setHighlighted 不是动画视图属性。 另外,您说的是两件相反的事情:您同时将Highlighted 设置为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与它的highlightedTextColor以便发生可见的事情。

斯威夫特 4

例如,您可以使用这样的自定义视图

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

随心所欲地玩弄持续时间和动画。 最后,您可以使用它代替UIView ,无论何时单击它,它都会更改其alpha值,因此它看起来像一个亮点。

简单的解决方案是覆盖点击手势识别器,如下所示:

斯威夫特 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