【问题标题】:CGAffineTransformMakeRotation counter clockwise alwaysCGAffineTransform 总是逆时针旋转
【发布时间】:2012-11-27 07:31:16
【问题描述】:

我试图为UIImageView 设置动画,使其在用户触摸它并在其上顺时针方向移动手指时逆时针旋转。
基本上,一旦触摸结束,我希望 UIImageView 回到其原始位置,并且它应该以逆时针方向移动。

我遇到的问题是每次角度(以度为单位)大于 180 时,图像都会顺时针旋转回到其原始位置。它显然采取了最短的路径。至于 179 度或更小的角度,图像逆时针旋转(我需要)。

我尝试了这两段代码,它们的行为方式相同。有什么建议吗?

注意:这里的角度变量是一个双精度变量,它初始化为零,并且在我的代码中保持为 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,但有几个问题

  • 动画完成后,我无法再触摸和旋转图像
  • 它总是以顺时针方向动画,我无法弄清楚如何让它从用户结束旋转图像的点顺时针顺时针旋转。

【问题讨论】:

  • ACB - 我查看了那个帖子,它不是重复的。我无法让它逆时针移动!

标签: iphone ios xcode ipad


【解决方案1】:

我有类似的问题,检查接受的答案,它可能会帮助你:

Rotate a UIView clockwise for an angle greater than 180 degrees


针对cmets,也许这会有所帮助:

在我的代码中我实际使用了

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;”在那个 void 函数中,它确实逆时针旋转,但对于大于 180 的角度,它会再次回到顺时针旋转。
  • 将角度设置为负,逆时针旋转。
  • 在你的上方看我的cmets
  • 嗯,对我来说,对于 180 以上的负角,它仍然是逆时针方向的。(即角度
【解决方案2】:

我使用的技术是将旋转角度除以 2 并将旋转分成两个单独的动画,由 animateWithDuration:delay:options:animations:completion:completion: 参数链接。但是,由于视图已旋转到其当前位置,因此您需要从该位置开始并“取消旋转”回零,而不是尝试从零开始逆时针旋转。

【讨论】:

  • 我试过这个,但我从来没有得到一个流畅的动画,即使使用 UIViewAnimationOptionCurveLinear。你能通过这种方式获得流畅的动画吗?
  • 看起来非常流畅(事实上,我很惊讶)。确保不要在第二次旋转中包含任何延迟,并预先计算您的变换。
  • 我按照你说的做,但如果它对你有用,我一定是做错了什么。很高兴知道这可以工作。
  • @Darren - 您可能不想做的一件事是将整个第二个动画放在第一个动画的completion: 子句中。而是让它调用一个方法来完成后半部分。
  • 也许这就是我做错了,因为我就是这样。为什么会有不同?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多