xcode 如何为按钮设置动画?(目标 c)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11766827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:06:02  来源:igfitidea点击:

How to animate a button? (objective c)

objective-cxcodeanimationbuttontranslation

提问by Fernando Pérez Guzmán

I'm new at Xcode and Objectice C and would preciate some help with my app. I want to add a translation animation to a button. What would be the code to, for example, move "button" from X:30 Y:50 to X:10 Y:70? Thanks :).

我是 Xcode 和 Objectice C 的新手,希望对我的应用程序有一些帮助。我想为按钮添加翻译动画。例如,将“按钮”从 X:30 Y:50 移动到 X:10 Y:70 的代码是什么?谢谢 :)。

回答by

For old compilers/iOSes:

对于旧的编译器/iOS:

// set the original frame
button.frame = CGRectMake(30, 50, 100, 100);

// animate to the new one
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.75];
button.frame = CGRectMake(10, 70, 100, 100);
[UIView commitAnimations];

For newer compilers/iOSes:

对于较新的编译器/iOS:

// set the original frame
button.frame = CGRectMake(30, 50, 100, 100);

// animate
[UIView animateWithDuration:0.75 animations:^{
    button.frame = CGRectMake(10, 70, 100, 100);
}];

回答by Jeremy1026

UIView (a super class of UIButton) has the ability to be animated, use something like the following.

UIView(UIButton 的超类)具有动画的能力,使用类似下面的东西。

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2];
CGAffineTransform transform = CGAffineTransformMakeTranslation(-20, 20);
[aButton setTransform:transform];
[UIView commitAnimations];