iOS中利用UIBezierPath + CAAnimation實(shí)現(xiàn)心跳動畫效果
前言
最近在開發(fā)ios項(xiàng)目空閑之余,決定練習(xí)下UIBezierPath進(jìn)行繪圖和CAAnimation動畫的使用,制作了一個(gè)心跳的動畫,很簡單的示例,下面話不多說了,來一起看看詳細(xì)的介紹:
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];
// 起點(diǎn)(圓的第一個(gè)點(diǎn))
CGPoint tipLocation = CGPointMake(rect.size.width/2, rect.size.height-padding);
// 從起點(diǎn)開始畫
[heartPath moveToPoint:tipLocation];
// (左圓的第二個(gè)點(diǎn))
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];
// (左圓的第二個(gè)點(diǎn))
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];
// 右上角控制點(diǎn)
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è)置重復(fù)次數(shù)為無限大 pulseAnimation.repeatCount = FLT_MAX; // 添加動畫到layer [heartView.layer addAnimation:pulseAnimation forKey:@"transform.scale"];
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
iOS中TableView如何統(tǒng)一數(shù)據(jù)源代理詳解
這篇文章主要給大家介紹了關(guān)于iOS中TableView如何統(tǒng)一數(shù)據(jù)源代理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
iOS中只讓textField使用鍵盤通知的實(shí)例代碼
本文通過實(shí)例代碼給大家介紹了OS中只讓textField使用鍵盤通知的操作方法,代碼簡單易懂,非常不錯,具有參考借鑒加載,需要的的朋友參考下吧2017-07-07
iOS App開發(fā)中UISearchBar搜索欄組件的基本用法整理
iOS開發(fā)組件中自帶的UISearchBar提供了很多基礎(chǔ)和好用的搜索欄UI功能,下面就來總結(jié)一下iOS App開發(fā)中UISearchBar搜索欄組件的基本用法整理,需要的朋友可以參考下2016-05-05

