我想知道如何通过编程使一个UIButton在被点击时有动画效果。
-(IBAction)
提前感谢您!
-(IBAction)
提前感谢您!
在你的 IBAction
中
UIButton *button = (UIButton*)sender;
//animates button 25 pixels right and 25 pixels down. Customize
CGRect newFrame = CGRectMake(button.frame.origin.x + 25, button.frame.origin.y + 25, button.frame.size.width, button.frame.size.height);
[UIView animateWithDuration:0.3f
delay:0.0f
options: UIViewAnimationOptionCurveLinear
animations:^{
[button setFrame:newFrame];
}
completion:nil];
编辑 - 在框架之间切换
在某个地方初始化布尔变量clicked
为NO
if (!clicked){
//animates button 25 pixels right and 25 pixels down. Customize
CGRect newFrame = CGRectMake(button.frame.origin.x + 25, button.frame.origin.y + 25, button.frame.size.width, button.frame.size.height);
[UIView animateWithDuration:0.3f
delay:0.0f
options: UIViewAnimationOptionCurveLinear
animations:^{
[button setFrame:newFrame];
}
completion:nil];
clicked = YES;
} else {
//animates button 25 pixels left and 25 pixels up. Customize
CGRect newFrame = CGRectMake(button.frame.origin.x - 25, button.frame.origin.y - 25, button.frame.size.width, button.frame.size.height);
[UIView animateWithDuration:0.3f
delay:0.0f
options: UIViewAnimationOptionCurveLinear
animations:^{
[button setFrame:newFrame];
}
completion:nil];
clicked = NO;
}
yourViewController()
еђҺйқұж·»еЉ {}
括号пәЊе№¶ењЁиү™дғ›ж‹¬еЏ·дёж”ңзҢ®BOOL clicked;
。然еђҺењЁviewDidLoad
дёж·»еЉ иҰЊclicked = NO;
гЂ‚ - Chris Tetreault