【问题标题】:CoreAnimation rotation transformation animation directionCoreAnimation 旋转变换动画方向
【发布时间】:2012-02-05 19:38:44
【问题描述】:

有没有一种简单的方法来指定图像在物镜 C 中是顺时针还是逆时针旋转?我有一个带指针的速度表(一种反映汽车价格的设计)——它工作正常,除非指针向下旋转并从屏幕外移到另一侧更近。我希望它总是逆时针旋转到较低的数字,顺时针旋转到更高的数字。这是我的代码段:

// figure out the minimum and maximum prices shown on the 
// speedometer
float min = [_a.text floatValue] * 1000;
float max = [_g.text floatValue] * 1000;

// set calibration between number labels to be small or large
int cal = SMALL_CALIBRATION;
if ([_stickerPriceSetting floatValue] >= TRIGGERPRICE)
    cal = LARGE_CALIBRATION;

// set an animation duration of 1.5 seconds for the needle rotation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5];

// low prices (off speedometer)
if (price < (min - cal)) 
    _needle.transform = CGAffineTransformMakeRotation(- M_PI/12); 

// price too high (off speedometer)
else if (price  > (max + cal)) 
    _needle.transform = CGAffineTransformMakeRotation(M_PI * 13/12); 

// need to have needle point to the price
else {
    float delta = (max - min);
    float price_point = price - min;
    _needle.transform = CGAffineTransformMakeRotation(price_point/delta * M_PI);
}

 [UIView commitAnimations];

【问题讨论】:

    标签: objective-c core-animation cgaffinetransform image-rotation


    【解决方案1】:

    你可以通过动画层属性transform.rotation来做到这一点:

    // figure out the minimum and maximum prices shown on the 
    // speedometer
    float min = [_a.text floatValue] * 1000;
    float max = [_g.text floatValue] * 1000;
    
    // set calibration between number labels to be small or large
    int cal = SMALL_CALIBRATION;
    if ([_stickerPriceSetting floatValue] >= TRIGGERPRICE)
        cal = LARGE_CALIBRATION;
    
    CGFloat angle;
    if (price < min - cal)
        angle = -M_PI / 12;
    else if (price > max + cal)
        angle = M_PI * 13 / 12;
    else
        angle = M_PI * (price - min) / (max - min);
    
    [UIView animateWithDuration:1.5 animations:^{
        [(id)_needle.layer setValue:[NSNumber numberWithFloat:angle]
            forKeyPath:@"transform.rotation"];
    }];
    

    如果您导入 QuartzCore/QuartzCore.h,则不需要 (id) 演员表。

    【讨论】:

      【解决方案2】:

      我的车速表也有同样的问题。动画变换属性时不能指定旋转方向。它总是走最短的路。如果角度大于 180 度,我通过做 2 个动画解决了这个问题(第 1 个完成后开始第 2 个动画)。您必须注意正确的动画时间,以使其动画流畅。

      【讨论】:

      • 这对我有点帮助 - 除非我无法知道从原始点到新点的角度是否会大于 180 度 - 除非有简单的方法首先找出针(UIView)当前处于什么角度?我猜我将不得不编写一堆代码来节省旧价格并将其整合到针的运动中。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-06
      • 1970-01-01
      相关资源
      最近更新 更多