点击uibutton没有突出效果

时间:2016-12-16 06:25:21

标签: ios objective-c 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];

3 个答案:

答案 0 :(得分:1)

是的,你可以使用

_payBtn.showsTouchWhenHighlighted = YES;

答案 1 :(得分:1)

试试这个:

[_paybtn setHighlighted:YES];
[_paybtn sendActionsForControlEvents:UIControlEventTouchUpInside];

答案 2 :(得分:1)

您可以使用默认方法突出显示按钮:

 [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"];