iOS中的摇摆动画

时间:2011-06-18 06:25:25

标签: ios animation graphics core-animation core-graphics

我想知道是否可以在iOS中进行某种摇摆动画。例如,摇摆动画就像一个在风中飘扬的商店标志。想想西部片。按下按钮时我想做这种动画。我想要一种西方式的感觉,这真的会让它超越顶级。如果您知道如何做这样的事情,请提前感谢。

1 个答案:

答案 0 :(得分:6)

为了做到这一点,你必须事先做一些事情,比如将图层锚点设置为(0.5,0.0)。如下:

swaybtn.layer.anchorPoint = CGPointMake(0.5, 0.0);

然后创建一个函数,当按下按钮时会调用它来启动向后摇摆动画,另一个向前摇摆动画,最后一个向后移动到原始位置。你指定的度数是摇摆的距离,而rotationAndPerspectiveTransform中的第二个数字是摇摆的方向。 功能如下:

-(void)sway:(id)sender{

CGFloat degrees = 50.0;

CALayer *layer;
layer = swaybtn.layer;

CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(moveForward)];

rotationAndPerspectiveTransform.m34 = 1.0 / -400;
rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, degrees * M_PI / 180.0f, -1.0f, 0.0f, 0.0f);
layer.transform = rotationAndPerspectiveTransform;

[UIView commitAnimations];

}

-(void)moveForward{

CALayer *layer;
layer = swaybtn.layer;

CGFloat degrees = 50.0;

CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;

[UIView beginAnimations:@"swayforward" context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(moveBack)];

rotationAndPerspectiveTransform.m34 = 1.0 / -400;
rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, degrees * M_PI / 180.0f, 1.0f, 0.0f, 0.0f);
layer.transform = rotationAndPerspectiveTransform;

[UIView commitAnimations];

}

-(void)moveBack{

CALayer *layer;
layer = swaybtn.layer;

CGFloat degrees = 50.0;

CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;

[UIView beginAnimations:@"moveback" context:NULL];
[UIView setAnimationDuration:0.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

rotationAndPerspectiveTransform.m34 = 1.0 / -400;
rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, degrees * M_PI / 180.0f, 0.0f, 0.0f, 0.0f);
layer.transform = rotationAndPerspectiveTransform;

[UIView commitAnimations];

}

你去吧。你应该有一个动画来回摆动一个物体,就像悬挂的标志一样。