有这么一个需求,需要在UILabel Text上点击之后添加划线效果,就是类似todo done那种。我知道简单的划线就是起点加重点坐标,连接成为一条线,但是如何用代码控制看到由点到线的效果呢?CGContextSetLineWidth(context, 1);CGContextMoveToPoint(context, x, y);CGContextAddLineToPoint(context, x, y);CGContextStrokePath(context);谢谢!
1 回答
莫回无
TA贡献1865条经验 获得超7个赞
主要是这两个方法:
- (void)setupLayers
{ if (animationLayer != nil) {
[animationLayer removeFromSuperlayer];
}
animationLayer = [CALayer layer];
animationLayer.frame = self.bounds;
[self.layer addSublayer:animationLayer];
CGPoint midLeft = CGPointMake(CGRectGetMinX(animationLayer.bounds), CGRectGetMidY(animationLayer.bounds)); CGPoint midRight = CGPointMake(CGRectGetMaxX(animationLayer.bounds), CGRectGetMidY(animationLayer.bounds)); UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:midLeft];
[path addLineToPoint:midRight];
pathLayer = [CAShapeLayer layer];
pathLayer.frame = animationLayer.bounds;
pathLayer.geometryFlipped = YES;
pathLayer.path = path.CGPath;
pathLayer.strokeColor = [UIColor blackColor].CGColor;
pathLayer.lineWidth = 2;
[animationLayer addSublayer:pathLayer];
}
- (void)startAnimation
{
[animationLayer removeAllAnimations];
[pathLayer removeAllAnimations];
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = 3.0;
pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
[pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"];
}
- 1 回答
- 0 关注
- 276 浏览
添加回答
举报
0/150
提交
取消
