Reputation: 6940
I created UIButton programmatically, it appearance is ok, but when i tap button there is no "highlight" effect, when u usually tap button. How to add this?
_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];
Upvotes: 1
Views: 2078
Reputation: 4096
You can use default methods to highlight a button:
[self.button showsTouchWhenHighlighted];
If you want some kind of touch effect like setting background color from highlighted to normal you can use CABasicAnimation
class with key backgroundColor
for for background appearance:
- (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"];
}
If you need to show the effect over button's shadow you can do like by changing the key value:
// inside viewDidLoad
self.button.layer.shadowOpacity = 1.0f;'
// inside didTapPayBtn action
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"shadowColor"];
[self.button.layer addAnimation:animation forKey:@"shadow"];
Upvotes: 1
Reputation: 380
Try this :
[_paybtn setHighlighted:YES];
[_paybtn sendActionsForControlEvents:UIControlEventTouchUpInside];
Upvotes: 1