屏幕动画离屏到动画

pas*_*aya 3 iphone core-animation core-graphics objective-c ios

在我正在制作的应用程序中,我正在尝试按钮从屏幕开始并在屏幕上移动.我不知道如何从屏幕外的按钮开始,但我知道,一旦我弄明白,我可以做类似下面的代码:

[UIView beginAnimation:@"animate" context: nil];
[UIView setAnimationDuration:3];
self.myButton.frame = CGRectMake (20, 100, 40, 80);
//Other animations
[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)

另外,在行中[UIView beginAnimation:@"animate" context:nil];,上下文参数是否需要CGContextRef?

Cha*_*son 5

我假设您正在询问如何让按钮从屏幕外显示到屏幕上,只需将按钮原点位置设置为位于屏幕外的坐标即可.

self.myButton.frame = CGRectMake(-80, 100, 40, 80);
Run Code Online (Sandbox Code Playgroud)

完成此操作后,您只需使用您发布的代码即可在屏幕上显示动画效果.请记住,按钮将从第一个位置移动到第二个位置,这意味着如果您使用我使用的坐标,按钮将从左侧移动到屏幕上而不改变y位置.请记住,在setAnimationDuration中分配的时间内,您按下按钮的速度越快,移动到屏幕上的速度越快.

所以从左边动画到屏幕上

self.myButton.frame = CGRectMake(-80, 100, 40, 80);
// begin animation block    
[UIView beginAnimations:@"animate" context: nil];
[UIView setAnimationDuration:3];
self.myButton.frame = CGRectMake (20, 100, 40, 80);
// commit frame changes to be animated
[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)

此外,如果按钮之前在屏幕上,它似乎会传送出屏幕,然后在使用该代码时再次滑动.

哦,并回答你关于上下文的问题,不是它不必是CGContextRef,它可以是任何数据类型(只要它是一个对象而不是一个原语).它基本上是动画发生时传递给委托的数据.