Iphone 设置旋转图像视图的动画
我想将Iphone 设置旋转图像视图的动画,iphone,animation,uiimageview,rotation,Iphone,Animation,Uiimageview,Rotation,我想将UIImageView左右旋转大约10度,但要有平滑的动画,而不是使用以下方法看到的突然转向: player.transform = CGAffineTransformMakeRotation(angle) 非常感谢您的帮助。我使用一些类似的代码来实现类似的效果(尽管我将其旋转360度): 我使用与此非常类似的代码来旋转图像视图。我有一些代码可以360度旋转: CABasicAnimation *rotate; rotate = [CABasicAnimation animationWi
UIImageView
左右旋转大约10度,但要有平滑的动画,而不是使用以下方法看到的突然转向:
player.transform = CGAffineTransformMakeRotation(angle)
非常感谢您的帮助。我使用一些类似的代码来实现类似的效果(尽管我将其旋转360度):
我使用与此非常类似的代码来旋转图像视图。我有一些代码可以360度旋转:
CABasicAnimation *rotate;
rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
rotate.fromValue = [NSNumber numberWithFloat:0];
rotate.toValue = [NSNumber numberWithFloat:deg2rad(10)];
rotate.duration = 0.25;
rotate.repeatCount = 1;
[self.view.layer addAnimation:rotate forKey:@"10"];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.25]; // Set how long your animation goes for
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
player.transform = CGAffineTransformMakeRotation(angle); // if angle is in radians
// if you want to use degrees instead of radians add the following above your @implementation
// #define degreesToRadians(x)(x * M_PI / 180)
// and change the above code to: player.transform = CGAffineTransformMakeRotation(degreesToRadians(angle));
[UIView commitAnimations];
// The rotation code above will rotate your object to the angle and not rotate beyond that.
// If you want to rotate the object again but continue from the current angle, use this instead:
// player.transform = CGAffineTransformRotate(player.transform, degreesToRadians(angle));
- (void) runSpinAnimationOnView:(UIView*)view duration:(CGFloat)duration rotations:(CGFloat)rotations repeat:(float)repeat;
{
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: rotations * 2.0 /* full rotation*/ * rotations * duration ];
rotationAnimation.duration = duration;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = repeat;
[view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
称之为:[self-runspinimationview:imgView持续时间:0.1次旋转:M_PI_4次重复:10]代码>
和动画多次旋转若干度:
- (void)rotateImage:(UIImageView *)image duration:(NSTimeInterval)duration delay:(NSTimeInterval)delay
curve:(int)curve rotations:(CGFloat)rotations
{
[UIView animateWithDuration:duration
delay:delay
options:0
animations:^{
[UIView setAnimationCurve:curve];
image.transform = CGAffineTransformMakeRotation(rotations);
}
completion:^(BOOL finished){
[self rotateImage2:image duration:duration delay:delay curve:curve rotations:rotations];
;
}];
[UIView commitAnimations];
}
- (void)rotateImage2:(UIImageView *)image duration:(NSTimeInterval)duration delay:(NSTimeInterval)delay
curve:(int)curve rotations:(CGFloat)rotations
{
[UIView animateWithDuration:duration
delay:delay
options:0
animations:^{
[UIView setAnimationCurve:curve];
image.transform = CGAffineTransformMakeRotation(-rotations);
}
completion:^(BOOL finished){
[self rotateImage:image duration:duration delay:delay curve:curve rotations:rotations];
;
}];
[UIView commitAnimations];
}
如果要将uiimageview左右旋转大约10度
调用:[自旋转图像:imgView持续时间:0.7延迟:0.0曲线:UIViewAnimationCurveEaseIn旋转:(M_PI/18)]代码>
类似地,使用NSTimer
和CGAffineTransformMakeRotation
的现代Swift
解决方案也有一定程度的降低:
class Rotation: UIViewController {
var timer = NSTimer()
var rotAngle: CGFloat = 0.0
@IBOutlet weak var rotationImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
activateTimer()
}
func activateTimer() {
//(1)
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target:self, selector:#selector(Rotation.updateCounter), userInfo: nil, repeats: true)
}
func updateCounter() {
//(2)
var rotateLeft: Bool = true
if rotateLeft {
rotAngle -= 30.0
} else {
rotAngle += 30.0
}
//(3)
UIView.animateWithDuration(2.0, animations: {
self.rotationImage.transform = CGAffineTransformMakeRotation((self.rotAngle * CGFloat(M_PI)) / 180.0)
})
}
}
注意事项:
- 将插座连接到
UIImageView
- 播放(1)计时器配速、(2)动画配速和(3)旋转角度,以获得所需的结果。这组变量对我有效
转换为Swift 3/4:
let animation = CABasicAnimation(keyPath: "transform.rotation")
animation.fromValue = 0
animation.toValue = Double.pi * 2.0
animation.duration = 2
animation.repeatCount = .infinity
animation.isRemovedOnCompletion = false
imageView.layer.add(animation, forKey: "spin")
旋转角度是否需要使用弧度值?“我第一次使用它,”deg2rad说,“这是什么?”?另外,还未声明Cabasicanization。是的,您需要弧度值;deg2rad只是一个占位符,用于将10度转换为弧度。