当我尝试动画UIImageView
时,当用户触摸它并沿顺时针方向将其手指移动时,它会逆时针旋转。
基本上我希望UIImageView一旦触摸结束就回到原来的位置,它应该以逆时针方向移动。
我遇到的问题是,每当角度(以度为单位)大于180时,图像就会顺时针旋转回原位。它显然走回了最短路径。对于179度或更小的角度,图像逆时针旋转(我需要的)。
我尝试了这两段代码,它们的行为方式相同。有什么建议吗?
注意:这里的角度变量是一个double,它的初始值为零,在我的代码中它保持为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);
}];
}
我试过调查this post并且有几个问题
答案 0 :(得分:3)
我有一个类似的问题,检查接受的答案,它可能会帮助你:
Rotate a UIView clockwise for an angle greater than 180 degrees
在回应评论时,这可能会有所帮助:
在我的代码中我实际使用
rotationAnimation.fromValue
rotationAnimation.toValue
而不是
rotationAnimation.byValue.
如果toValue小于fromValue,则旋转总是逆时针旋转。如果你的价值观是正面的还是负面的,只要它们之间的关系无关紧要。
弄清楚你的起始角度是什么,找出你想要旋转的方向并找出你的结束角度是什么。如果你想逆时针方向,请确保它小于你的起始角度。这是我用于从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"];
}
答案 1 :(得分:0)
我使用的技术是将旋转角度除以2并将旋转分成两个单独的动画,由completion:
animateWithDuration:delay:options:animations:completion:
参数链接。但是,由于视图已旋转到当前位置,因此您需要从该位置开始并将“取消旋转”返回到零,而不是尝试从零开始旋转CCW。