【问题标题】:Animate UIButton Down - Xcode动画 UIButton Down - Xcode
【发布时间】:2013-07-23 18:37:30
【问题描述】:

我想知道当通过

点击时如何为 UIButton 设置动画
-(IBAction)

提前致谢!

【问题讨论】:

  • 是的。 ibaction 中的哪些内容会为按钮设置动画?
  • “动画 UIButton 向下”是什么意思?你的意思是把按钮的框架动画到一个新的位置吗?
  • 是的..这正是我要找的。​​span>

标签: ios uibutton jquery-animate ibaction


【解决方案1】:

在您的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 语句中,这样当第一次单击按钮时它会移动位置,然后再次单击时它会向上移动
  • 如果你只是来回切换,你可以检查条件中的按钮框架,或者设置一个布尔标志
  • 好的...我不知道如何把它放在布尔值中。能否请您编辑答案以包含它?
  • 在 .m 文件顶部的 yourViewController() 之后添加 {} 括号,并在这些括号中放置 BOOL clicked;。然后在 viewDidLoad 添加行 clicked = NO;
  • 布尔逻辑和布尔逻辑对于任何语言的编程和开发都非常重要,所以我建议你在尝试做任何过于复杂的事情之前阅读它和其他一些基本的东西,你'只会让自己感到沮丧。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-16
  • 2015-05-09
相关资源
最近更新 更多