iOS 环绕旋转动画实现

// 调用

DiyAnimation *circle = [[DiyAnimationalloc] initWithFrame:CGRectMake(0,120, 320, 300)];

    [circle setDiyAnimationBackgroundColor:[UIColororangeColor]];

    [self.viewaddSubview:circle];



//DiyAnimation.h

-(void)setDiyAnimationBackgroundColor:(UIColor *)aColor;


//DiyAnimation.m

//创建环绕动画,传入两个个属性分别是 :运动开始的角度(右侧90度为0),运动结束的角度

-(void)createCircle:(float)startAngle andEndAngle:(float)endAngle

{

    

    //创建运动的轨迹动画

    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimationanimationWithKeyPath:@"position"];

    pathAnimation.calculationMode =kCAAnimationPaced;

    pathAnimation.fillMode =kCAFillModeForwards;

    pathAnimation.removedOnCompletion =NO;

    pathAnimation.duration =5.0;

    pathAnimation.repeatCount =MAXFLOAT;

    

    float x =20;

    // (1 1)  z   (1 0) y   (0 1) x  (0 0) nil

    float radiuscaleOne = 1;

    float radiuscaleTwo = 1;

    // 中心

    CGFloat origin_x =self.frame.size.width /2  ;

    CGFloat origin_y =self.frame.size.height /2 ;

    // 半径

    CGFloat radiusX =50;

    

    CGMutablePathRef ovalfromarc =CGPathCreateMutable();

    CGAffineTransform t2 =CGAffineTransformConcat(CGAffineTransformConcat(

                                                                          CGAffineTransformMakeTranslation(-origin_x, -origin_y),

                                                                          CGAffineTransformMakeScale(radiuscaleOne, radiuscaleTwo)),

                                                   CGAffineTransformMakeTranslation(origin_x, origin_y));

    

    CGPathAddArc(ovalfromarc, &t2, origin_x, origin_y, radiusX,startAngle,endAngle,1);// 1逆时针 0顺时针

    pathAnimation.path = ovalfromarc;

    CGPathRelease(ovalfromarc);

    // 圆形

    UIView * circleView1 = [[UIImageViewalloc] init];

    [selfaddSubview:circleView1];

    circleView1.frame =CGRectMake(160,130, x, x);

    [circleView1.layersetCornerRadius:x/2];

    circleView1.backgroundColor = [UIColoryellowColor];

    //设置运转的动画

    [circleView1.layeraddAnimation:pathAnimationforKey:@"moveTheCircleOne"];

    

    

}

//贝塞尔画出路径

- (void)drawRect:(CGRect)rect {

    CGFloat origin_x =self.frame.size.width/2;

    CGFloat origin_y =self.frame.size.height/2;


    CGContextRef context =UIGraphicsGetCurrentContext();

    CGContextSaveGState(context);

    

        //整个圆

    UIBezierPath *arc = [UIBezierPathbezierPathWithOvalInRect:CGRectMake(origin_x -100, origin_y - 100,200, 200)];

    [[UIColorwhiteColor] setStroke];

    [arc stroke];

    CGContextRestoreGState(context);

}


-(void)setDiyAnimationBackgroundColor:(UIColor *)aColor

{

    self.backgroundColor = aColor;

    [selfcreateCircle: M_PI /6 andEndAngle:M_PI /6 + 2 *M_PI];


}

你可能感兴趣的:(iOS实用技巧)