3

我想知道如何在通过单击时为 UIButton 设置动画

-(IBAction)

提前致谢!

4

1 回答 1

8

在你的里面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;

}
于 2013-07-23T20:29:54.253 回答