普通CALayer在被初始化时是需要给一个frame值的,这个frame值一般都与给定view的bounds值一致,它本身是有形状的,而且是矩形.
CAShapeLayer在初始化时也需要给一个frame值,但是,它本身没有形状,它的形状来源于你给定的一个path,然后它去取CGPath值,它与CALayer有着很大的区别
CAShapeLayer有着几点很重要:
1.它依附于一个给定的path,必须给与path,而且,即使path不完整也会自动首尾相接
2. strokeStart以及strokeEnd代表着在这个path中所占用的百分比
3.CAShapeLayer动画仅仅限于沿着边缘的动画效果,它实现不了填充效果
UIView*showView = [[UIViewalloc]initWithFrame:CGRectMake(100,100,100,100)];
[self.viewaddSubview:showView];
showView.backgroundColor= [UIColorredColor];
showView.alpha=0.5;
UIBezierPath*path = [UIBezierPathbezierPathWithArcCenter:CGPointMake(100/2.f,100/2.f)
radius:100/2.f
startAngle:0
endAngle:M_PI*2
clockwise:YES];
//创建一个shapeLayer
CAShapeLayer*layer = [CAShapeLayerlayer];
layer.frame= showView.bounds;//与showView的frame一致
layer.strokeColor= [UIColorgreenColor].CGColor;//边缘线的颜色
layer.fillColor= [UIColorclearColor].CGColor;//闭环填充的颜色
layer.lineCap=kCALineCapSquare;//边缘线的类型
layer.path= path.CGPath;//从贝塞尔曲线获取到形状
layer.lineWidth=4.0f;//线条宽度
layer.strokeStart=0.0f;
layer.strokeEnd=0.1f;
//将layer添加进图层
[showView.layeraddSublayer:layer];
// 3s后执行动画操作(直接赋值就能产生动画效果)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3*NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
layer.speed=0.1;
layer.strokeStart=0.5;
layer.strokeEnd=0.9f;
layer.lineWidth=1.0f;
});
//给这个layer添加动画效果
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1*NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
CABasicAnimation*pathAnimation = [CABasicAnimationanimationWithKeyPath:@"strokeEnd"];
pathAnimation.duration=2.0;
pathAnimation.fromValue= [NSNumbernumberWithFloat:0.5f];
pathAnimation.toValue= [NSNumbernumberWithFloat:0.8f];
pathAnimation.removedOnCompletion=NO;
pathAnimation.fillMode=kCAFillModeForwards;
[layeraddAnimation:pathAnimationforKey:nil];
});