2014-04-01 105 views
1

我想旋转控制360度。我现在就这样做ios如何旋转控制360度

#define degreesToRadians(x) (M_PI * x/180.0) 

[UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{ 
    [self.crossButton setTransform:CGAffineTransformRotate(self.crossButton.transform, degreesToRadians(360))]; 
} completion:nil]; 

但是没有发生。你知道我做错了什么吗?

回答

3

通过360度的旋转变换为恒等变换,因此 对象不是动画的。

可以实现具有“基本属性动画”期望的效果:

[CATransaction begin]; 
CABasicAnimation *rotationAnimation; 
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 
rotationAnimation.byValue = @(2 * M_PI); // 360 degrees 
rotationAnimation.duration = 0.25; 
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 
[self.crossButton.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; 
[CATransaction commit]; 

更新:如果旋转量不是360度(使得最终的位置是不一样的 作为初始位置),那么以下应该工作:

[CATransaction begin]; 
CGFloat angle = 45.0 * M_PI/180.0; 
CABasicAnimation *rotationAnimation; 
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 
rotationAnimation.byValue = @(angle); 
rotationAnimation.duration = 0.25; 
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 
rotationAnimation.removedOnCompletion = YES; 
[CATransaction setCompletionBlock:^{ 
    self.crossButton.transform = CGAffineTransformRotate(self.button.transform, angle); 
}]; 
[self.crossButton.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; 
[CATransaction commit]; 
+0

但是,当我旋转45度它有一个讨厌的跳转到90度 – AYMADA

+0

@AYMADA:查看更新的答案。 –

1

如果您正在旋转360度,则将对象放回到相同的位置。如果你想看动画,增加延迟并检查。你会发现整个物体移动一个完美的圆。

1

请尝试以下代码。

[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{ 

     self.crossButton.transform = CGAffineTransformMakeRotation(M_PI); 

    } completion:nil]; 
0

夫特3:

使用CABasicAnimation

var rotationAnimation = CABasicAnimation() 
rotationAnimation = CABasicAnimation.init(keyPath: "transform.rotation.z") 
rotationAnimation.toValue = NSNumber(value: (Double.pi * 2.0)) 
rotationAnimation.duration = 1.0 
rotationAnimation.isCumulative = true 
rotationAnimation.repeatCount = 100.0 
view.layer.add(rotationAnimation, forKey: "rotationAnimation") 


这里是UIView的一个扩展功能,处理开始&停止旋转操作:

extension UIView { 

    func startRotation() { 
     let rotation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z") 
     rotation.fromValue = 0 
     rotation.toValue = NSNumber(value: Double.pi * 2.0) 
     rotation.duration = 1.0 
     rotation.isCumulative = true 
     rotation.repeatCount = FLT_MAX 
     self.layer.add(rotation, forKey: "rotationAnimation") 
    } 

    func stopRotation() { 
     self.layer.removeAnimation(forKey: "rotationAnimation") 
    } 
} 


现在使用,UIView.animation关闭:

UIView.animate(withDuration: 0.5, animations: { 
     button.transform = CGAffineTransform(rotationAngle: (CGFloat(Double.pi)) 
}) { (isHalfAnimationComplete) in 
    UIView.animate(withDuration: 0.5) { 
     button.transform = CGAffineTransform(rotationAngle: (CGFloat(Double.pi * 2)) 
    }  
}