我以编程方式创建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];
您可以使用默认方法突出显示一个按钮:
[self.button showsTouchWhenHighlighted];
如果您想要某种触摸效果,例如将背景颜色从突出显示到正常状态,则可以使用键backgroundColor
使用CABasicAnimation
类,以供背景出现:
- (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"];
是的,您可以使用
_payBtn.showsTouchWhenHighlighted = YES;
尝试以下:
[_paybtn setHighlighted:YES];
[_paybtn sendActionsForControlEvents:UIControlEventTouchUpInside];