iPhone动画 - 以圆圈形式移动视图

时间:2011-04-10 17:50:47

标签: iphone objective-c uiviewanimation

有没有办法围绕中心点移动视图而不使用视图动画旋转它?

   A

A  C  A

   A

2 个答案:

答案 0 :(得分:3)

    #import <QuartzCore/QuartzCore.h>
    #define degreesToRadians(deg) (deg / 180.0 * M_PI)

manualy:

-(CGPoint)setPointToAngle:(int)angle center:(CGPoint)centerPoint radius:(double)radius
 {
    return CGPointMake(radius*cos(degreesToRadians(angle)) + centerPoint.x, radius*sin(degreesToRadians(angle)) + centerPoint.y);
 }

动画像这样:

    int angle = 0; // start angle position 0-360 deg
    CGPoint center = self.view.center;
    CGPoint start = [self setPointToAngle:angle center:center radius:radius]; //point for start moving
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, start.x, start.y);

    for(int a =0;a<4;a++) //set path points for 90, 180, 270,360 deg form begining angle
    {
        angle+=45;
        expPoint = [self setPointToAngle:angle center:center radius:expRadius];
        angle+=45;
        start = [self setPointToAngle:angle center:center radius:radius];
        CGPathAddQuadCurveToPoint(path, NULL,expPoint.x, expPoint.y, start.x, start.y);
    }

    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pathAnimation.removedOnCompletion = NO;
    pathAnimation.path = path;
    [pathAnimation setCalculationMode:kCAAnimationCubic];
    [pathAnimation setFillMode:kCAFillModeForwards];
    pathAnimation.duration = 14.0; 

    [MY_VIEW.layer addAnimation:pathAnimation forKey:nil];

也看看

CGPathAddQuadCurveToPoint, 
CGPathAddCurveToPoint, 
CGPathAddArcToPoint, 
CGPathAddArc 

和其他人

答案 1 :(得分:0)

将动画线程放入并每帧都手动移动并不是那么重要,这就是我的目的。