github:https://github.com/potato512/SYAnimation
使用貝賽爾曲線UIBezierPath、陰影對象CAShapeLayer、基礎類動畫CABasicAnimation實現。
效果圖如下:

示例代碼:
- (void)lineAnimation{    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10.0, 340.0, (self.view.frame.size.width - 10.0 * 2), 100.0)];    [self.view addSubview:view];    view.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.2];    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:view.bounds];     CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];    shapeLayer.strokeColor = [UIColor purpleColor].CGColor;    shapeLayer.fillColor = [UIColor clearColor].CGColor;    shapeLayer.lineWidth = 2;    shapeLayer.lineJoin = kCALineJoinRound;    shapeLayer.lineCap = kCALineCaPRound;    shapeLayer.path = bezierPath.CGPath;    [view.layer addSublayer:shapeLayer];        CABasicAnimation *pathAnim = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];    pathAnim.duration = 5.0;    pathAnim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaSEOut];    pathAnim.fromValue = @(0);    pathAnim.toValue = @(1);    pathAnim.autoreverses = true;    pathAnim.fillMode = kCAFillModeForwards;    pathAnim.removedOnCompletion = NO;    pathAnim.repeatCount = MAXFLOAT;    [shapeLayer addAnimation:pathAnim forKey:@"strokeEndAnim"];}
新聞熱點
疑難解答