[英]No highlight effect on tapping uibutton
我以编程方式创建了UIButton,它的外观还可以,但是当我点击按钮时,通常没有点击按钮时就没有“突出显示”效果。 如何添加呢?
_payBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_payBtn setTitle:@" Оплатить " forState:UIControlStateNormal];
[_payBtn setTitleColor:[UIColor lightGreen] forState:UIControlStateNormal];
[_payBtn setContentMode:UIViewContentModeCenter];
_payBtn.tintColor = [UIColor lightGreen];
[[_payBtn layer] setBorderWidth:1.0f];
[[_payBtn layer] setBorderColor:[UIColor lightGreen].CGColor];
[_payBtn addTarget:self action:@selector(didTapPayBtn) forControlEvents:UIControlEventTouchUpInside];
[self.payContainerView addSubview:_payBtn];
是的,您可以使用
_payBtn.showsTouchWhenHighlighted = YES;
尝试这个 :
[_paybtn setHighlighted:YES];
[_paybtn sendActionsForControlEvents:UIControlEventTouchUpInside];
您可以使用默认方法突出显示一个按钮:
[self.button showsTouchWhenHighlighted];
如果您想要某种触摸效果,例如将背景颜色从突出显示设置为正常,则可以将CABasicAnimation
类与关键backgroundColor
以获取背景外观:
- (IBAction) didTapPayBtn:(UIButton *)sender{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
animation.fromValue = (id)[[UIColor lightGrayColor] CGColor];
// animation.toValue = (id)[[UIColor lightGrayColor] CGColor]; // optional
animation.duration = 1.0f; // animation duration
// animation.autoreverses = YES; // optional
// animation.repeatCount = NSIntegerMax; // optional
[self.button.layer addAnimation:animation forKey:@"ColorPulse"];
}
如果需要在按钮阴影上方显示效果,可以通过更改键值来实现:
// inside viewDidLoad
self.button.layer.shadowOpacity = 1.0f;'
// inside didTapPayBtn action
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"shadowColor"];
[self.button.layer addAnimation:animation forKey:@"shadow"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.