iOS中利用UIBezierPath + CAAnimation實現(xiàn)心跳動畫效果
更新時間:2017年10月26日 10:16:21 作者:Mazy_ma
這篇文章主要給大家介紹了關(guān)于iOS中利用UIBezierPath + CAAnimation實現(xiàn)心跳動畫效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的日常開發(fā)具有一定的參考學習,需要的朋友們下面隨著小編來一起學習學習吧。
前言
最近在開發(fā)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)];
// 設(shè)置填充色
[[UIColor redColor] setFill];
// 填充
[heartPath fill];
// 設(shè)置邊線
heartPath.lineWidth = 2;
heartPath.lineCapStyle = kCGLineCapRound;
heartPath.lineJoinStyle = kCGLineJoinRound;
// 設(shè)置描邊色
[[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]; // 倒轉(zhuǎn)動畫 pulseAnimation.autoreverses = YES; // 設(shè)置重復次數(shù)為無限大 pulseAnimation.repeatCount = FLT_MAX; // 添加動畫到layer [heartView.layer addAnimation:pulseAnimation forKey:@"transform.scale"];
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
淺談IOS中AFNetworking網(wǎng)絡(luò)請求的get和post步驟
本篇文章主要介紹了淺談IOS中AFNetworking網(wǎng)絡(luò)請求的get和post步驟的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-02-02
iOS開發(fā)中使用NSURLConnection類處理網(wǎng)絡(luò)請求的方法
這篇文章主要介紹了iOS開發(fā)中使用NSURLConnection類處理網(wǎng)絡(luò)請求的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12

