高亮类似于UIButton的UIView

问题描述 投票:0回答:5

我有一个带有多个子视图的 UIView 和一个可识别的关联点击手势,我想模仿它具有“触摸”效果。也就是说,当点击发生时,我想显示容器视图具有不同的背景颜色,并且任何子视图 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
                     }];    
}
ios iphone ios5 uiviewanimation
5个回答
7
投票

setHighlighted 不是可动画化的视图属性。另外,您说的是两个相反的事情:您同时将突出显示设置为“是”和“否”。结果将是什么也没有发生,因为没有整体改变。

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

编辑:

你说“都试过了,但都没有成功。”也许您需要澄清我所说的延迟性能的含义。我刚刚尝试过,效果非常好:

- (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
,以便发生可见的事情。


7
投票

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

简单的解决方案是覆盖点击手势识别器 就像下面这样:

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

}

0
投票

斯威夫特 5.7

import UIKit

final class HighlightingTapRecognizer: UITapGestureRecognizer {
    private(set) var viewToHighlight: UIView
    
    init(target: Any?, action: Selector?, viewToHighlight: UIView) {
        self.viewToHighlight = viewToHighlight
        super.init(target: target, action: action)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
        super.touchesBegan(touches, with: event)
        
        UIView.animate(withDuration: 0.1, delay: 0, options: [.allowUserInteraction, .curveLinear], animations: {
            self.viewToHighlight.alpha = 0.7
        })
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
        super.touchesEnded(touches, with: event)
        
        UIView.animate(withDuration: 0.1, delay: 0, options: [.allowUserInteraction, .curveLinear], animations: {
            self.viewToHighlight.alpha = 1
        })
    }
}

0
投票

Swift 4 及以上版本

final class HighlightingTapRecognizer: UITapGestureRecognizer {
    private(set) var viewToHighlight: UIView

    init(target: Any?, action: Selector?, viewToHighlight: UIView) {
        self.viewToHighlight = viewToHighlight
        super.init(target: target, action: action)
    }


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

        let xPosition = touches.first?.location(in: viewToHighlight).x
        let yPosition = touches.first?.location(in: viewToHighlight).y

        let width = 30
        let height = 30

        let view11 = UIView(frame: CGRect(x: Int(xPosition!), y: Int(yPosition!), width: width,     height: height))
        view11.isHidden = true
        view11.backgroundColor = .darkGray.withAlphaComponent(0.5)
        view11.layer.cornerRadius = view11.frame.height / 2
        view11.layer.masksToBounds = true

        self.viewToHighlight.addSubview(view11)

        view11.isHidden = false
        Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false, block: { _ in
            view11.removeFromSuperview()
        })

    }
}

用途:

let tap = HighlightingTapRecognizer(target: self, action: #selector(self.YourActionFunc(_:)), viewToHighlight: previewView)
previewView.addGestureRecognizer(tap)
© www.soinside.com 2019 - 2024. All rights reserved.