我想左/約10度旋轉UIImageView
正確的,但有一個平滑的動畫,而不是一個急轉彎,我看到使用:動畫旋轉的UIImageView
player.transform = CGAffineTransformMakeRotation(angle)
讚賞任何幫助,謝謝。
我想左/約10度旋轉UIImageView
正確的,但有一個平滑的動畫,而不是一個急轉彎,我看到使用:動畫旋轉的UIImageView
player.transform = CGAffineTransformMakeRotation(angle)
讚賞任何幫助,謝謝。
我使用一些這樣的代碼,以實現類似的效果(雖然我通過旋轉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));
+1,喜歡額外的評論。 – eladleb 2012-12-26 01:52:59
我有一些代碼旋轉360度:
- (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"];
}
調用它:如果你想旋轉
- (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];
}
:[self runSpinAnimationOnView:imgView duration:0.1 rotations:M_PI_4 repeat:10];
和動畫旋轉一定程度,再大約10度的左右視圖 致電:[self rotateImage:imgView duration:0.7 delay:0.0 curve:UIViewAnimationCurveEaseIn rotations:(M_PI/18)];
類似,一些輩分
現代Swift
解決方案,使用NSTimer
和CGAffineTransformMakeRotation
:
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)r選擇角度以獲得理想的結果。這組變量對我有用。
轉換爲斯威夫特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沒有聲明,這是什麼?還說CABasicAnimation沒有聲明。 – 2010-08-27 21:17:57
是的,你需要弧度值; deg2rad只是一個佔位符,可將您的10度轉換爲弧度。 – jer 2010-08-27 21:35:32