使UIButton点击时有动画效果 - Xcode

3
我想知道如何通过编程使一个UIButton在被点击时有动画效果。
-(IBAction)

提前感谢您!


是的。在ibaction中放入什么内容可以使按钮向下动画? - Mina Dawoud
“将UIButton向下动画化”是什么意思?您是指将按钮的框架动画到新位置吗? - rmaddy
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;

}

我该如何将这个放入if else语句中,以便当按钮第一次被点击时它会移动位置,然后再次点击时它会向上移动? - Mina Dawoud
1
如果你只是来回切换,你可以在条件语句中检查按钮的框架,或者设置一个布尔标志。 - Chris Tetreault
好的...我不知道如何将它放入布尔值。你能否请编辑答案并包括它? - Mina Dawoud
1
ењЁж‚Ёзљ„.m文件йҰ¶йѓЁзљ„yourViewController()еђҺйқұж·»еЉ {}括号пәЊе№¶ењЁиү™дғ›ж‹¬еЏ·дё­ж”ңзҢ®BOOL clicked;。然еђҺењЁviewDidLoadдё­ж·»еЉ иҰЊclicked = NO;гЂ‚ - Chris Tetreault
1
同时,布尔代数和布尔逻辑对于任何语言的编程和开发都非常重要,因此我建议在尝试进行过于复杂的操作之前,先了解一些基本概念,否则你会不断地陷入挫败感中。 :) - Chris Tetreault
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接