码迷,mamicode.com
首页 > 其他好文 > 详细

【axc】简单的线性动画绘制

时间:2016-07-06 14:34:13      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

在一个View上绘制一条直线  然后做出相应的动画效果  可以这样封装三个方法:

 

/**

 *  划线工具

 *

 *  @param lineArray   线段的点数组 NSValue 类型  默认第一个点为起点

 *  @param time        划线时间

 *  @param strokeColor 线段颜色

 */

- (void)axcBaseAniamtionWithLinePointArray:(NSArray <NSValue *> *)lineArray time:(CGFloat )time strokeColor:(UIColor *)strokeColor{

    if (lineArray.count <= 1) {

        if (_axcBasePrintLog) {

            NSLog(@"%@:\n你的数组对象少于一个,无法划线!",self);

        }

        return;

    }

    if (_axcBasePrintLog) {

        NSLog(@"%@:\n当前正在划线\t方法名:axcBaseAniamtionWithLinePointArray\t起点:%@\t划线时间:%.2f,颜色:%@\n",self,NSStringFromCGPoint([lineArray[0] CGPointValue]),time,strokeColor);

    }

    UIBezierPath *_path=[UIBezierPath bezierPath];

    

    CGPoint point1 = [lineArray[0] CGPointValue];

    [_path moveToPoint:point1];

    for (int i = 1; i < lineArray.count; i ++) {

        [_path addLineToPoint: [lineArray[i] CGPointValue]];

        if (_axcBasePrintLog) {

            NSLog(@"%@:遍历划线点(%d):%@\n",self,i + 1,NSStringFromCGPoint([lineArray[i] CGPointValue]));

        }

    }

    CAShapeLayer *shapeLayer=[CAShapeLayer layer];

    shapeLayer.path=_path.CGPath;

    shapeLayer.fillColor=[UIColor clearColor].CGColor;//填充颜色

    shapeLayer.strokeColor=strokeColor.CGColor;//边框颜色

    [self.view.layer addSublayer:shapeLayer];

    // 动画

    CABasicAnimation *pathAniamtion = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];

    // 时间

    pathAniamtion.duration = time;

    pathAniamtion.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    pathAniamtion.fromValue = [NSNumber numberWithFloat:0.0f];

    // 划线段的百分之多少

    pathAniamtion.toValue = [NSNumber numberWithFloat:1];

    pathAniamtion.autoreverses = NO;

    [shapeLayer addAnimation:pathAniamtion forKey:nil];

}

 

/**

 *  圆规工具

 *

 *  @param Center      中心点

 *  @param radius      半径

 *  @param startAngle  开始角度

 *  @param endAngle    结束角度

 *  @param clockwise   是否顺时针

 *  @param time        作图时间

 *  @param strokeColor 颜色

 */

- (void)axcBaseAniamtionArcWithCenter:(CGPoint )Center

                               radius:(CGFloat )radius

                           startAngle:(CGFloat )startAngle

                             endAngle:(CGFloat )endAngle

                            clockwise:(BOOL )clockwise

                                 time:(CGFloat )time

                          strokeColor:(UIColor *)strokeColor{

    

    UIBezierPath *_path=[UIBezierPath bezierPath];

    

    [_path addArcWithCenter:Center radius:radius startAngle:startAngle endAngle:endAngle clockwise:clockwise];

    

    CAShapeLayer *shapeLayer=[CAShapeLayer layer];

    shapeLayer.path=_path.CGPath;

    shapeLayer.fillColor=[UIColor clearColor].CGColor;//填充颜色

    shapeLayer.strokeColor=strokeColor.CGColor;//边框颜色

    [self.view.layer addSublayer:shapeLayer];

    // 动画

    CABasicAnimation *pathAniamtion = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];

    // 时间

    pathAniamtion.duration = time;

    pathAniamtion.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    pathAniamtion.fromValue = [NSNumber numberWithFloat:0.0f];

    // 划线段的百分之多少

    pathAniamtion.toValue = [NSNumber numberWithFloat:1];

    pathAniamtion.autoreverses = NO;

    [shapeLayer addAnimation:pathAniamtion forKey:nil];

}

/**

 *  清除所有划线

 */

- (void)axcBaseClearAllShapeLayer{

    if (_axcBasePrintLog) {

        NSLog(@"%@:\n当前执行清除该界面上的所有划线!",self);

    }

    NSArray *shapeLayerArray = self.view.layer.sublayers;

    for (int i = 0; i < shapeLayerArray.count; i ++) {

        if ([shapeLayerArray[i] isKindOfClass:[CAShapeLayer class]]) {

            [shapeLayerArray[i] removeFromSuperlayer];

        }

    }

}

【axc】简单的线性动画绘制

标签:

原文地址:http://www.cnblogs.com/axclogo/p/5646673.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!