如何在Core Animation中强制执行特定的旋转方向(即顺时针方向)?

Tha*_*nks 9 iphone cocoa-touch core-animation uikit

我用这个旋转一个视图:

CGAffineTransform rotatedTransform = CGAffineTransformRotate(CGAffineTransformIdentity, rotationValue);
Run Code Online (Sandbox Code Playgroud)

我有一个物体,我想旋转大约320度.现在Core Animation非常聪明,只需根据需要旋转它,通过旋转-40度来实现.因此,物体以相反的方式旋转,移动量较小.

我想限制它顺时针旋转.我是否必须通过稍微改变动画来做到这一点,还是有更优雅的方式?

Nat*_*ies 18

以下代码段someView使用关键帧动画旋转调用的视图.动画包含3帧,分布在1秒内,视图分别在第一帧,第二帧和最后一帧中旋转到0º,180º和360º.代码如下:

CALayer* layer = someView.layer;
CAKeyframeAnimation* animation;
animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];

animation.duration = 1.0;
animation.cumulative = YES;
animation.repeatCount = 1;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;

animation.values = [NSArray arrayWithObjects:
    [NSNumber numberWithFloat:0.0 * M_PI],
    [NSNumber numberWithFloat:0.5 * M_PI],
    [NSNumber numberWithFloat:1.0 * M_PI], nil];

animation.keyTimes = [NSArray arrayWithObjects:
    [NSNumber numberWithFloat:0.0],
    [NSNumber numberWithFloat:0.5],
    [NSNumber numberWithFloat:1.0], nil];

animation.timingFunctions = [NSArray arrayWithObjects:
    [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],
    [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear], nil];

[layer addAnimation:animation forKey:@"transform.rotation.z"];
Run Code Online (Sandbox Code Playgroud)

如果您使用逆时针动画,则应使用负值.对于稍微更基本的动画,您可以使用CABasicAnimation:

CALayer* layer = someView.layer;
CABasicAnimation* animation;
animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];

animation.fromValue = [NSNumber numberWithFloat:0.0 * M_PI];
animation.toValue = [NSNumber numberWithFloat:1.0 * M_PI];

animation.duration = 1.0;
animation.cumulative = YES;
animation.repeatCount = 1;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;

[layer addAnimation:rotationAnimation forKey:@"transform.rotation.z"];
Run Code Online (Sandbox Code Playgroud)

  • 每当您遇到无法找到的问题时,您应该在文档中搜索它以查看它的定义位置.在这种情况下,这些常量在CAMediaTiming和CAMediaTimingFunction中定义,它们是QuartzCore框架的一部分.如果你还没有添加QuartzCore.framework,那可能就是你的问题. (2认同)