【问题标题】:Rotate Button Or Image From One Point To another将按钮或图像从一个点旋转到另一个点
【发布时间】:2014-09-01 10:40:07
【问题描述】:

我想创建一个可以从给定点旋转的旋转按钮 我试过了,但它给出了角度,我想给出点

self.theImageView.transform=CGAffineTransformMakeRotation (angle);
       angle=30;

我也试过了,但是有同样的问题

CABasicAnimation *halfTurn;
    halfTurn = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    halfTurn.fromValue = [NSNumber numberWithFloat:0];
    halfTurn.toValue = [NSNumber numberWithFloat:((90*M_PI)/360)];
    halfTurn.duration = 1;
    halfTurn.repeatCount = false;
    [[button layer] addAnimation:halfTurn forKey:@"180"];

任何人都可以建议或提供最简洁的代码吗 提前致谢

【问题讨论】:

    标签: ios animation rotation points image-rotation


    【解决方案1】:

    如果您想给出两个点并围绕这两个点旋转您的视图,则有一个问题。您必须确保这两个点与旋转锚点等距。您可以计算这种旋转的弧角并进行相应的设置。

    你可以在这里找到弧的长度: http://math.about.com/od/formulas/ss/surfaceareavol_9.htm

    你可以找到求圆弧角的公式,在这个链接:http://www.regentsprep.org/Regents/math/algtrig/ATM1/arclengthlesson.htm

    编辑

    我现在假设有 7 个按钮围绕一个锚点循环排列。您现在要做的是维护一个名为 currentAngle 的实例级变量并将其初始化为 0。

    CGFloat nextAngle = currentAngle + (360/7);
    CABasicAnimation *halfTurn;
        halfTurn = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
        halfTurn.fromValue = [NSNumber numberWithFloat:((currentAngle*M_PI)/360)];
        halfTurn.toValue = [NSNumber numberWithFloat:((nextAngle*M_PI)/360)];
        halfTurn.duration = 1;
        halfTurn.repeatCount = false;
        [[button layer] addAnimation:halfTurn forKey:@"clock_wise_rotation"];
    currentAngle = nextAngle;
    

    以上代码仅适用于顺时针旋转。如果要执行逆时针动画,则必须减去 360/7 并执行动画。

    【讨论】:

    • 我有 7 个圆形按钮,在中心有一个指针。我想用单击的动画将指针移动到按钮。在这种情况下,我应该使用哪种方法。
    • 每个点之间的角度差为 360/7。制作动画,从一个角度移动到另一个角度。维护一个告诉您当前角度位置的变量。并根据运动方向增加或减少 360/7 度。
    • 如何在这个旋转中添加动画。如果可能的话,请提供一些最简洁的代码
    • 您的代码本身具有旋转动画的正确实现。
    • 一切正常,但动画完成后会回到初始位置
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-29
    • 2010-10-21
    • 2012-11-21
    • 2011-07-28
    • 2014-12-26
    • 1970-01-01
    相关资源
    最近更新 更多