我认为对此没有简单或明确的答案。
免责声明:
此答案中的所有代码都是从我的脑海中编写的,所以请原谅错误。
我建议这样做:
建议一:降低所有视图子视图的不透明度,这不会影响颜色...
目标 C:
-(void)buttonTouched{
for(UIView *subview in self.subviews){
subview.alpha = 0.5;
}
}
斯威夫特:
func buttonTouched() {
for subview in subviews {
subview.alpha = 0.5
}
}
建议 2(未测试):尝试通过手动操作来覆盖所有子视图(缺点:您还必须手动设置颜色,如果是这样,那将是疯狂的不是单色的):
目标 C:
-(void)buttonTouched{
for(UIView *subview in self.subviews){
if([subview respondsToSelector:@selector(setBackgroundColor:)]){
//for generic views - changes UILabel's backgroundColor too, though
subview.backgroundColor = [UIColor grayColor];
}
if([subview respondsToSelector:@selector(setTextColor:)]){
//reverse effect of upper if statement(if needed)
subview.backgroundColor = [UIColor clearColor];
subview.textColor = [UIColor grayColor];
}
}
}
斯威夫特:
func buttonTouched() {
for subview in subviews {
if subview.responds(to: #selector(setter: AVMutableVideoCompositionInstruction.backgroundColor)) {
//for generic views - changes UILabel's backgroundColor too, though
subview.backgroundColor = UIColor.gray
}
if subview.responds(to: #selector(setter: UILabel.textColor)) {
//reverse effect of upper if statement(if needed)
subview.backgroundColor = UIColor.clear
subview.textColor = UIColor.gray
}
}
}
这是一个非常糟糕的设计,可能会导致很多问题,但它可能会对您有所帮助。上面的例子需要很多改进,我只是想给你一个提示。您必须恢复 touchesEnded: 方法中的颜色。也许对你有帮助……
建议 3:用透明视图(如果是矩形)覆盖整个视图
目标 C:
-(void)buttonTouched{
UIView *overlay = [[UIView alloc]initWithFrame:self.bounds];
overlay.backgroundColor = [[UIColor grayColor]colorWithAlphaComponent:0.5];
[self addSubview: overlay];
}
斯威夫特:
func buttonTouched() {
let overlay = UIView(frame: bounds)
overlay.backgroundColor = UIColor.gray.withAlphaComponent(0.5)
addSubview(overlay)
}
当然,当用户松开手指时,您必须将其移除。
建议 4:另一种选择是创建当前视图的位图并根据自己的喜好修改像素,这是一项相当多的工作,因此我将在此处省略代码。
Apple 可能会混合使用后两者。当一个按钮被触摸时,它将越过像素并在每个具有 alpha 分量的像素上覆盖一个灰色像素。
我希望我能帮上忙。