开发者

iPhone animations - Moving a view in a circle

Is there a way to move a view around a center point without开发者_Go百科 rotating it using the view animations?

   A

A  C  A

   A


    #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);
 }

with animation something like this:

    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];

also look at

CGPathAddQuadCurveToPoint, 
CGPathAddCurveToPoint, 
CGPathAddArcToPoint, 
CGPathAddArc 

and others


Its not that big a deal to put an animation thread in and move it manually every frame so thats what I went with.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜