gpt4 book ai didi

ios - 如何像 Apple 一样突出显示 UIView

转载 作者:行者123 更新时间:2023-12-02 06:58:56 25 4
gpt4 key购买 nike

当用户触摸 UIButton 时,它会变灰一点。苹果到底做了什么才能达到这样的效果呢?当我的自定义 UIButton 突出显示时,我需要相同的效果。

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
AppDelegate* appDelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
if ([keyPath isEqualToString:@"highlighted"]){
UIButton *button = object;
if (button.isHighlighted) {
self.backgroundColor=[UIColor colorWithRed:36.0/255.0 green:153.0/255.0 blue:116.0/255.0 alpha:1];
}else{
self.backgroundColor=appDelegate.currentAppColor;
}
}
}

我正在使用此代码,但更改背景颜色不会影响任何 subview 。我也需要它们变灰。

最佳答案

您可以使用自定义 View ,例如:

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 值,因此它看起来像一个突出显示。

关于ios - 如何像 Apple 一样突出显示 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21210514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com