iOS-利用UIBezierPath和CAAnimation制作心跳动画
2017-02-15 18:17
381 查看
空闲之余,练习下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制作路径动画
- iOS中利用UIBezierPath + CAAnimation实现心跳动画效果
- iOS利用UIBezierPath + CAAnimation实现路径动画效果
- iOS利用仿射变换(CGAffineTransform)制作动画效果
- iOS-利用粒子发射器(CAEmitterLayer) 制作发射动画
- 如何在ios中的UI界面利用相框制作gif动画
- IOS利用UIBezierPath画动画圆
- IOS利用UIBezierPath画动画圆
- ios scrollView中增加动画效果,自动滚动UIScrollView,利用了NSTimer
- 【百度地图API】如何利用地图API制作汽车沿道路行驶的动画
- 【百度地图API】如何利用地图API制作汽车沿道路行驶的动画?——如何获得道路层数据
- iOS 下类似UITableView的可循环列表利用UIScrollView制作
- cocos2d或者iOS制作视频+虚列动画做游戏时候,视频播放和结束时顿闪问题解决方案
- 【iOS-Cocos2d游戏开发之二十】精灵的基础知识点总汇(位图操作/贴图更换/重排z轴等)以及利用CCSprite与CCLayerColor制作简单遮盖层!
- IOS制作动画效果
- 【百度地图API】如何利用地图API制作汽车沿道路行驶的动画
- iOS 下类似UITableView的可循环列表利用UIScrollView制作可以控制另一个UITableView(2)
- 利用JQuery动画制作滑动菜单项效果
- 利用CSS3制作淡入淡出动画效果
- 原创:利用JS制作GIF动画图(及图片轮转显示效果)