最近在开发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.line; 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"];
茶杯头甜蜜终章dlc 官方手机版v1.0.0.3
下载火柴人传说暗影格斗内置菜单 最新版v3.0.1
下载荒野乱斗测试服 安卓版v61.10.3
下载荒野乱斗彩虹服 安卓版v61.10.3
下载寒霜启示录 安卓版v1.25.10
寒霜启示录是一款生存模拟游戏,不少玩家可能对于末日都有着自己
末日城堡免广告版 安卓最新版v0.7.1
末日城堡免广告版是一款非常好玩的模拟经营类游戏,内部可以不看
甜蜜人生模拟器 最新版v1.4.5
甜蜜人生模拟器是一款非常好玩的模拟恋爱手游,玩家在这里能够对
武器锻造师内置功能菜单 v10.4
武器锻造师内置菜单版是游戏的破解版本,在该版本中为玩家提供了
开放空间overfield 安卓版v1.0.5
开放空间Overfield是一款箱庭养成经营手游,让你在广阔