xcode CGAffineTransformMakeRotation 总是逆时针旋转
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13791582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CGAffineTransformMakeRotation counter clockwise always
提问by Sam B
I an trying to animate a UIImageView
to rotate counter clockwise when a user touches it and moves its finger on it in clockwise direction.
Basically I want the UIImageView to go back to its original position once the touch ends and it should move in counter clockwise direction.
UIImageView
当用户触摸它并沿顺时针方向移动手指时,我试图将 a 设置为逆时针旋转。
基本上我希望 UIImageView 在触摸结束后返回其原始位置,并且它应该逆时针方向移动。
The problem I have is that every time angle (in degrees) is greater than 180 then the image rotates clockwise back to its original position. Its apparently taking the shortest path back. As for angles 179 degrees or less the image rotate counter clockwise (what i need).
我遇到的问题是每次角度(以度为单位)大于 180 时,图像都会顺时针旋转回其原始位置。它显然采取了最短的路径。至于 179 度或更小的角度,图像逆时针旋转(我需要)。
I tried both these pieces of code and they behave the same way. Any suggestions?
我尝试了这两段代码,它们的行为方式相同。有什么建议?
NOTE: here angle variable is a double and its initialized to be zero and it remains 0 throughout my code
注意:这里的角度变量是一个双精度值,它初始化为零,在我的代码中它保持为 0
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self handleObject:touches withEvent:event isLast:YES];
/* this doesn't make rotation counter clockwise for rotations greater than 180 */
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve: UIViewAnimationCurveLinear];
[UIView setAnimationDuration:1.0];
[UIView setAnimationRepeatCount:1];
CGAffineTransform transform = CGAffineTransformMakeRotation(angle);
circle1ImgVw.transform = CGAffineTransformRotate(transform, angle);
// ends animation
[UIView commitAnimations];
/* this doesn't make rotation counter clockwise for rotations greater than 180 */
[UIView animateWithDuration:1.0 animations:^{
circle1ImgVw.transform = CGAffineTransformMakeRotation(angle);
}];
}
I tried looking into this postand there are couple of problems with it
我尝试查看这篇文章,但有几个问题
- I cannot touch and rotate the image anymore after the animation is done
- It always animates in clockwise direction and I haven't been able to figure out how to make it go counter clockwise smoothly from the point where the user ended rotating the image.
- 动画完成后,我无法再触摸和旋转图像
- 它总是以顺时针方向动画,我一直无法弄清楚如何让它从用户结束旋转图像的点开始逆时针平滑地移动。
回答by Darren
I had a similar issue, check the accepted answer out, it might help you:
我有一个类似的问题,检查接受的答案,它可能对你有帮助:
Rotate a UIView clockwise for an angle greater than 180 degrees
In response to comments, maybe this will help:
回应评论,也许这会有所帮助:
In my code I actually use
在我的代码中我实际使用
rotationAnimation.fromValue
rotationAnimation.toValue
instead of
代替
rotationAnimation.byValue.
The rotation is always going to be counter clockwise if the toValue is less than the fromValue. It doesn't matter if your values are positive or negative, only the relation between them.
如果 toValue 小于 fromValue,则旋转总是逆时针旋转。你的价值观是积极的还是消极的并不重要,重要的是它们之间的关系。
Figure out what your starting angle is, figure out which direction you want to rotate and figure out what your ending angle is. Make sure that it's less than your starting angle if you want to go counter clockwise. Here is the code I use for animating a clock hand clockwise from 12:00 to the current time.
弄清楚你的起始角度是多少,弄清楚你想旋转哪个方向,并弄清楚你的结束角度是什么。如果你想逆时针走,确保它小于你的起始角度。这是我用于从 12:00 到当前时间顺时针设置时钟指针动画的代码。
-(void) viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self updateClockArmAngle];
}
- (void)updateClockArmAngle
{
// Get the time
NSDate *date = [NSDate date];
NSCalendar* calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
[calendar setTimeZone:[NSTimeZone timeZoneWithName:@"America/Toronto"]];
NSDateComponents* components = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:date];
CGFloat hour = [components hour];
CGFloat angle = (hour/24.0)*(2*M_PI);
CGFloat minute = [components minute];
angle += (minute/(24*60))*(2*M_PI);
[self rotateViewAnimated:self.clockArm withDuration:1.5 byAngle:angle];
}
- (void) rotateViewAnimated:(UIView*)view
withDuration:(CFTimeInterval)duration
byAngle:(CGFloat)angle
{
CABasicAnimation *rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.fromValue = 0;
rotationAnimation.toValue = [NSNumber numberWithFloat:angle];
rotationAnimation.duration = duration;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[rotationAnimation setRemovedOnCompletion:NO];
[rotationAnimation setFillMode:kCAFillModeForwards];
[view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
回答by Hot Licks
The technique I've used is to divide the rotation angle by 2 and separate the rotate into two separate animations, linked by the completion:
parameter of animateWithDuration:delay:options:animations:completion:
. But, since the view has been rotated to its current position, you need to start with that position and "unrotate" back to zero, vs attempting to rotate CCW from zero.
我已经使用的技术是除以2的旋转角度及旋转分离成两个单独的动画,通过链接completion:
的参数animateWithDuration:delay:options:animations:completion:
。但是,由于视图已旋转到其当前位置,因此您需要从该位置开始并“不旋转”回零,而不是尝试从零开始逆时针旋转。