简单描述一下
使用CAShapeLayer与UIBezierPath可以实现不在view的drawRect方法中就画出一些想要的图形 。
1:UIBezierPath: UIBezierPath是在 UIKit 中的一个类,继承于NSObject,可以创建基于矢量的路径.此类是Core Graphics框架关于path的一个OC封装。使用此类可以定义常见的圆形、多边形等形状 。我们使用直线、弧(arc)来创建复杂的曲线形状。每一个直线段或者曲线段的结束的地方是下一个的开始的地方。每一个连接的直线或者曲线段的集合成为subpath。一个UIBezierPath对象定义一个完整的路径包括一个或者多个subpaths。
2:CAShapeLayer: CAShapeLayer顾名思义,继承于CALayer。 每个CAShapeLayer对象都代表着将要被渲染到屏幕上的一个任意的形状(shape)。具体的形状由其path(类型为CGPathRef)属性指定。 普通的CALayer是矩形,所以需要frame属性。CAShapeLayer初始化时也需要指定frame值,但 它本身没有形状,它的形状来源于其属性path 。CAShapeLayer有不同于CALayer的属性,它从CALayer继承而来的属性在绘制时是不起作用的。
在网上摘的一段。
CAShapeLayer+UIBezierPath画一个红包的样子
先画一个深色的背景
<pre>
//深色背景
_redLayer = [CAShapeLayer alloc] init];
UIBezierPath *pathFang = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 80, APP_WIDTH-40, APP_HEIGHT-160) cornerRadius:4];
_redLayer.path = pathFang.CGPath;
_redLayer.zPosition = 1;
[self.view.layer addSublayer:_redLayer];
[_redLayer setFillColor:[UIColor colorWithRed:0.7968 green:0.2186 blue:0.204 alpha:1.0].CGColor];//深色背景
</pre>
浅色的红包封口
<pre>
//浅色红包口
_lineLayer = [[CAShapeLayer alloc] init];
UIBezierPath path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 80, APP_WIDTH-40, APP_HEIGHT-320) byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(4, 4)];
CGPoint startPoint = CGPointMake(20, APP_HEIGHT-240);
CGPoint endPoint = CGPointMake(APP_WIDTH-20, APP_HEIGHT-240);
CGPoint controlPoint = CGPointMake(APP_WIDTH0.5, APP_HEIGHT-180);
//曲线起点
[path moveToPoint:startPoint];
//曲线终点和控制基点
[path addQuadCurveToPoint:endPoint controlPoint:controlPoint];
//曲线部分颜色和阴影
[_lineLayer setFillColor:[UIColor colorWithRed:0.851 green:0.3216 blue:0.2784 alpha:1.0].CGColor];
[_lineLayer setStrokeColor:[UIColor colorWithRed:0.9401 green:0.0 blue:0.0247 alpha:0.02].CGColor];
[_lineLayer setShadowColor:[UIColor blackColor].CGColor];
[_lineLayer setLineWidth:0.1];
[_lineLayer setShadowOffset:CGSizeMake(6, 6)];
[_lineLayer setShadowOpacity:0.2];
[_lineLayer setShadowOffset:CGSizeMake(1, 1)];
_lineLayer.path = path.CGPath;
_lineLayer.zPosition = 1;
[self.view.layer addSublayer:_lineLayer];
</pre>
添加点击按钮
<pre>
//发红包按钮
UIButton *sendBtn = [[UIButton alloc] initWithFrame:CGRectMake((APP_WIDTH-100)/2, APP_HEIGHT-240-20, 100, 100)];
sendBtn.layer.masksToBounds = YES;
sendBtn.layer.cornerRadius = sendBtn.bounds.size.height/2;
[sendBtn setTitle:@"开红包" forState:UIControlStateNormal];
[sendBtn addTarget:self action:@selector(moveAnimation:) forControlEvents:UIControlEventTouchUpInside];
[sendBtn setBackgroundColor:[UIColor brownColor]];
sendBtn.layer.zPosition = 3;
[self.view addSubview:sendBtn];
</pre>
添加 按钮旋转动画 需给按钮设置层级,否则旋转超过XY面时,会穿透背景_lineLayer和_redLayer
<pre>
CABasicAnimation *transformAnima = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
// transformAnima.fromValue = @(M_PI_2);
transformAnima.toValue = [NSNumber numberWithFloat: M_PI];
transformAnima.duration = 0.5;
transformAnima.cumulative = YES;
transformAnima.autoreverses = NO;
transformAnima.repeatCount = HUGE_VALF;
transformAnima.fillMode = kCAFillModeForwards;
transformAnima.removedOnCompletion = NO;
transformAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
sender.layer.zPosition = 5;
sender.layer.zPosition = sender.layer.frame.size.width/2.f;
[sender.layer addAnimation:transformAnima forKey:@"rotationAnimationY"];
</pre>
当旋转动画执行2秒后,将浅色开口移动
<pre>
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[sender removeFromSuperview];
UIBezierPath *newPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 80, APP_WIDTH-40, APP_HEIGHT-620) byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(4, 4)];
CGPoint startPoint = CGPointMake(20, APP_HEIGHT-540);
CGPoint endPoint = CGPointMake(APP_WIDTH-20, APP_HEIGHT-540);
CGPoint controlPoint = CGPointMake(APP_WIDTH*0.5, APP_HEIGHT-480);
//曲线起点
[newPath moveToPoint:startPoint];
//曲线终点和控制基点
[newPath addQuadCurveToPoint:endPoint controlPoint:controlPoint];
CGRect newFrame = CGRectMake(20, 80, APP_WIDTH-40, APP_HEIGHT-620);
CABasicAnimation* pathAnim = [CABasicAnimation animationWithKeyPath: @"path"];
pathAnim.toValue = (id)newPath.CGPath;
CABasicAnimation* boundsAnim = [CABasicAnimation animationWithKeyPath: @"frame"];
boundsAnim.toValue = [NSValue valueWithCGRect:newFrame];
CAAnimationGroup *anims = [CAAnimationGroup animation];
anims.animations = [NSArray arrayWithObjects:pathAnim, boundsAnim, nil];
anims.removedOnCompletion = NO;
anims.duration = 0.1f;
anims.fillMode = kCAFillModeForwards;
[self.lineLayer addAnimation:anims forKey:nil];
});
</pre>
一个简单的效果。