runaction 旋转_如何让轮盘转起来?

答:Brad Larson

(最佳答案)

我不知道如何在cocos2d上插入动画,但是你可以用CALayers或者UIViews创建核心动画。或许最简答的方法就是创建一个包含轮盘图样的UIImageView,然后编成动画。

首先,新建UIImageView,将轮盘图案设定为它的初始值,如果想让轮盘转动,使用如下代码:

CATransform3D rotationTransform = CATransform3DMakeRotation(1.0f * M_PI, 0, 0, 1.0);

CABasicAnimation* rotationAnimation;

rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];

rotationAnimation.toValue = [NSValue valueWithCATransform3D:rotationTransform];

rotationAnimation.duration = 0.25f;

rotationAnimation.cumulative = YES;

rotationAnimation.repeatCount = 10;

[rotatingImage.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

假设rotatingImage就是你需要的UIImageView。

在这个实例中,转盘会转5次,每次耗时0.5秒,转动会在半圈时候停止,因为核心动画(Core Animation)在半圈的时候变方向,所以在转动改变方向之前,最多能让转盘转半圈。也就是说,如果你设定旋转角度为(1.5f * pi),它只能转九十度。如果你设定弧度值为(0.999f * pi),转盘会顺时针旋转。

如果想要实现加速或者减速的效果,最好不要用CABasicAnimation,CAKeyframeAnimation的效果会更好些。

答:Stanislav

在cocos2d中,一种简便的方法是:设定

[sprite runAction:[RotateBy actionWithDuration:dur angle:360]];

完成旋转动作,如果需要持续性的旋转,你可以设定

id action = [RepeatForever actionWithAction: [RotateBy actionWithDuration:dur angle:360]];

[sprite runAction:action];

[sprite stopAction:action];

在此之前,一定要设定sprite transformAnchor,让image处于居中位置。

答:mahboudz

你可以设定不同的弧度参数来完成轮盘的旋转,不用考虑是否可以转满一周,也不需要把整个转动过程分成几个部分来设计。比如,下面的代码可以完成旋转的效果,每秒转一次:

- (void) runSpinAnimationWithDuration:(CGFloat) duration;

{

CABasicAnimation* rotationAnimation;

rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];

rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * rotations * duration ];

rotationAnimation.duration = duration;

rotationAnimation.cumulative = YES;

rotationAnimation.repeatCount = 1.0;

rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];

[myView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

}

答:EightyEight

有很多方法可以完成动画的效果,如果你要给转盘设定一种frame-by-frame模式的动画效果,可以借助AtlasDemo达到这个目的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值