【问题标题】:Sway Animation in iOSiOS 中的摇摆动画
【发布时间】:2011-06-18 06:25:25
【问题描述】:

我想知道是否可以在 iOS 中制作某种摇摆动画。例如,摇摆动画就像随风飘扬的商店招牌。想想西部片。我想在按下按钮时做这种动画。我想要一种西方的感觉,那真的会把它放在首位。如果您知道如何执行此类操作,请提前致谢。

【问题讨论】:

  • 您现在已经问了 6 个问题。我认为是时候看看答案旁边的小复选标记了。通过单击最能解决您的问题的答案旁边的这个,您给回答者 15 分和一个微笑,您将获得 2 分和所有其他用户的善意。回头看看其他 5 个,看看是否没有有价值的答案。这可能会大大有助于让更多人解决您未来的问题。
  • 好的。好吧,大多数答案并没有真正充分地回答我的问题。
  • 我想通了,我通过将按钮层的锚点设置在顶部中间然后使用3D旋转来做到这一点。
  • 您可以将此作为答案提交并接受。这样看到这篇文章的其他人可以很容易地找到答案。

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


【解决方案1】:

为此,您必须事先做几件事,例如将图层锚点设置为 (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];

}

然后就可以了。您应该有一个动画,可以像悬挂标志一样来回摆动物体。

【讨论】:

  • 您好,这是一个很好的例子。你能帮我从左到右摇摆动画安装前向后摇摆动画吗? @Pete42
  • 这真的很难帮助你,因为我不知道你想要它看起来像什么。你想让它向左旋转一点然后向右旋转吗?我建议只设置锚点并为此旋转。我还建议查看 CALayer 和 CATransform3D 文档以获得您真正想要的。我想要一个来回的3d,所以我这样做了。还可以帮助您找出要使用的内容github.com/honcheng/CATransform3D-Test
猜你喜欢
  • 1970-01-01
  • 2014-04-19
  • 1970-01-01
  • 2012-03-15
  • 2013-01-30
  • 1970-01-01
  • 1970-01-01
  • 2013-01-29
  • 1970-01-01
相关资源
最近更新 更多