如何在核心动画中强制执行特定的旋转方向(即顺时针)?

How can I enforce an specific direction (i.e. clockwise) of rotation in Core Animation?(如何在核心动画中强制执行特定的旋转方向(即顺时针)?)

本文介绍了如何在核心动画中强制执行特定的旋转方向(即顺时针)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用这个旋转视图:

I am rotating a view with this:

CGAffineTransform rotatedTransform = CGAffineTransformRotate(CGAffineTransformIdentity, rotationValue);

我有一个物体,我想旋转大约 320 度.现在 Core Animation 很聪明,可以根据需要旋转它,通过旋转 -40 度来做到这一点.因此对象以更短的移动量反向旋转.

I have an object which I want to spin around for about 320 degrees. Now Core Animation is clever and just rotates it as much as needed, doing that by rotating it with -40 degrees. So the object rotates the other way around with a shorter amount of movement.

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

I want to constrain it to rotate clockwise. Would I have to do that by changing animations in little steps, or is there an more elegant way?

推荐答案

以下代码片段通过使用关键帧动画来旋转名为 someView 的视图.动画由 3 帧组成,分布在 1 秒内,第一帧、第二帧和最后一帧的视图分别旋转为 0º、180º 和 360º.代码如下:

The following snippet rotates a view called someView by using key-framed animation. The animation consists of 3 frames spread over 1 second, with the view rotated to 0º, 180º and 360º in the first, second and last frames respectively. Code follows:

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"];

如果你是逆时针动画,你应该使用负值.对于稍微基础的动画,可以使用 CABasicAnimation:

If you're after counterclockwise animation, you should use negative values. For a slightly more basic animation, you can use 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"];

这篇关于如何在核心动画中强制执行特定的旋转方向(即顺时针)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在核心动画中强制执行特定的旋转方向(即顺时针)?

基础教程推荐