有這么一個需求,需要在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 關注
- 242 瀏覽
添加回答
舉報
0/150
提交
取消
