【问题标题】:CGAffineTransformMakeRotation not working when called for the second time第二次调用时 CGAffineTransform Rotation 不起作用
【发布时间】:2012-09-20 09:47:40
【问题描述】:

我有这个功能可以将 UIButtons 旋转 45 度。但是一旦旋转,调用相同的方法不会再旋转,并且按钮在第一次旋转后卡在其旋转位置。有任何想法吗?

- (void)rotateImage:(UIButton *)image duration:(NSTimeInterval)duration 
              curve:(int)curve degrees:(CGFloat)degrees
{
    // Setup the animation
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:duration];
    [UIView setAnimationCurve:curve];
    [UIView setAnimationBeginsFromCurrentState:YES];

    // The transform matrix
    CGAffineTransform transform = 
    CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(degrees));
    image.transform = transform;

    // Commit the changes
    [UIView commitAnimations];
}

【问题讨论】:

    标签: rotation transform cgaffinetransform


    【解决方案1】:

    transform 属性是绝对的。如果您想从当前位置进行相对旋转,则需要跟踪按钮的绝对旋转并使用现有方法(可能会更快),或者将新旋转连接到现有旋转中。下面是连接旋转矩阵的代码。

    我没有使用我的 Obj-C(我使用 MonoTouch),但它在 C 中可能看起来像这样:

    image.transform = image.transform.Rotate(DEGREES_TO_RADIANS(degrees));
    

    或者这个:

    image.transform = CGAffineTransformationConcat( 
        image.transform, 
        CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(degrees));
    )
    

    请随意编辑这篇文章以使其成为正确的 Obj-C。

    【讨论】:

    • 使用这个定义来完成这项工作:#define DEGREES_TO_RADIANS(x) (M_PI * (x) / 180.0)
    猜你喜欢
    • 1970-01-01
    • 2018-06-15
    • 2016-03-30
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多