我正在开发一个带有两个 UIButton 的应用程序,它们在按下时必须改变大小和颜色。我的代码是:
UIButton *Answer1Button = [UIButton buttonWithType:UIButtonTypeCustom];
UIButton *Answer2Button = [UIButton buttonWithType:UIButtonTypeCustom];
[Answer1Button addTarget:self action:@selector(Answer1Action) forControlEvents:UIControlEventTouchUpInside];
[Answer2Button addTarget:self action:@selector(Answer2Action) forControlEvents:UIControlEventTouchUpInside];
[Answer1Button setBackgroundColor:[UIColor redColor]];
[Answer2Button setBackgroundColor:[UIColor yellowColor]];
Answer1Button.frame = CGRectMake(0, 0, 320, 45);
Answer2Button.frame = CGRectMake(0, 50, 320, 45);
以及我为它创建的函数:
-(void)Answer1Action{
[UIButton beginAnimations:nil context:nil];
[UIButton setAnimationDuration:0.5];
self.backgroundColor = [UIColor greenColor];
self.alpha = 0.5;
[UIButton commitAnimations];
}
我现在遇到的问题是,当我按下 UIButton 时,函数被调用,但 self.alpha 影响按钮所在的整个 UIView。
我仍然是 Objective-C 的新手,所以我认为这可能是我忘记的简单内容。
最佳答案
[Answer1Button setBackgroundColor:[UIColor greenColor]];
[Answer1Button setAlpha:0.5];
//Include QuartzCore framework and import it to your viewController.h,and write the below line along with the above code to change the size and to do many animations.
Answer1Button.layer.affineTransform=CGAffineTransformScale(Answer1Button.transform, 1.2, 1.2);
关于ios - UIButton 动画 ontarget self,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19949659/