这篇文章主要给大家介绍了关于iOS中利用UIBezierPath + CAAnimation实现心跳动画效果的相关资料,文中通过示例代码介绍的非常详细,对大家的日常开发具有一定的参考学习,需要的朋友们下面随着小编来一起学习学习吧。
前言
最近在开发ios项目空闲之余,决定练习下UIBezierPath进行绘图和CAAnimation动画的使用,制作了一个心跳的动画,很简单的示例,下面话不多说了,来一起看看详细的介绍:
GIF示例:
核心代码
1-首先通过 drawRect 绘制心形view
- (void)drawRect:(CGRect)rect {
// 间距
CGFloat padding = 4.0;
// 半径(小圆半径)
CGFloat curveRadius = (rect.size.width - 2 * padding)/4.0;
// 贝塞尔曲线
UIBezierPath *heartPath = [UIBezierPath bezierPath];
// 起点(圆的第一个点)
CGPoint tipLocation = CGPointMake(rect.size.width/2, rect.size.height-padding);
// 从起点开始画
[heartPath moveToPoint:tipLocation];
// (左圆的第二个点)
CGPoint topLeftCurveStart = CGPointMake(padding, rect.size.height/2.4);
// 添加二次曲线
[heartPath addQuadCurveToPoint:topLeftCurveStart controlPoint:CGPointMake(topLeftCurveStart.x, topLeftCurveStart.y + curveRadius)];
// 画圆
[heartPath addArcWithCenter:CGPointMake(topLeftCurveStart.x+curveRadius, topLeftCurveStart.y) radius:curveRadius startAngle:M_PI endAngle:0 clockwise:YES];
// (左圆的第二个点)
CGPoint topRightCurveStart = CGPointMake(topLeftCurveStart.x + 2*curveRadius, topLeftCurveStart.y);
// 画圆
[heartPath addArcWithCenter:CGPointMake(topRightCurveStart.x+curveRadius, topRightCurveStart.y) radius:curveRadius startAngle:M_PI endAngle:0 clockwise:YES];
// 右上角控制点
CGPoint topRightCurveEnd = CGPointMake(topLeftCurveStart.x + 4*curveRadius, topRightCurveStart.y);
// 添加二次曲线
[heartPath addQuadCurveToPoint:tipLocation controlPoint:CGPointMake(topRightCurveEnd.x, topRightCurveEnd.y+curveRadius)];
// 设置填充色
[[UIColor redColor] setFill];
// 填充
[heartPath fill];
// 设置边线
heartPath.lineWidth = 2;
heartPath.lineCapStyle = kCGLineCapRound;
heartPath.lineJoinStyle = kCGLineJoinRound;
// 设置描边色
[[UIColor yellowColor] setStroke];
[heartPath stroke];
}
2-添加心形view到主视图
XMHeartView *heartView = [[XMHeartView alloc] init];
heartView.frame = CGRectMake(100, 50, 200, 200);
[self.view addSubview:heartView];
3-给心形view添加心跳动画
// 给心视图添加心跳动画
float bigSize = 1.1;
CABasicAnimation *pulseAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
pulseAnimation.duration = 0.5;
pulseAnimation.toValue = [NSNumber numberWithFloat:bigSize];
pulseAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
// 倒转动画
pulseAnimation.autoreverses = YES;
// 设置重复次数为无限大
pulseAnimation.repeatCount = FLT_MAX;
// 添加动画到layer
[heartView.layer addAnimation:pulseAnimation forKey:@"transform.scale"];
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对编程学习网的支持。
沃梦达教程
本文标题为:iOS中利用UIBezierPath + CAAnimation实现心跳动画效果
猜你喜欢
- Android实现监听音量的变化 2023-03-30
- iOS 对当前webView进行截屏的方法 2023-03-01
- SurfaceView播放视频发送弹幕并实现滚动歌词 2023-01-02
- 最好用的ios数据恢复软件:PhoneRescue for Mac 2023-09-14
- Flutter实现底部和顶部导航栏 2022-08-31
- 详解flutter engine 那些没被释放的东西 2022-12-04
- Android MaterialButton使用实例详解(告别shape、selector) 2023-06-16
- Android studio实现动态背景页面 2023-05-23
- 作为iOS开发,这道面试题你能答出来,说明你基础很OK! 2023-09-14
- Android实现轮询的三种方式 2023-02-17
