这篇文章主要为大家展示了“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实现心跳动画效果”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。