国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > iOS > 正文

iOS中利用UIBezierPath + CAAnimation實(shí)現(xiàn)心跳動(dòng)畫效果

2020-07-26 02:38:46
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

前言

最近在開發(fā)ios項(xiàng)目空閑之余,決定練習(xí)下UIBezierPath進(jìn)行繪圖和CAAnimation動(dòng)畫的使用,制作了一個(gè)心跳的動(dòng)畫,很簡(jiǎn)單的示例,下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹:

GIF示例:

核心代碼

1-首先通過(guò) 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添加心跳動(dòng)畫

// 給心視圖添加心跳動(dòng)畫 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)動(dòng)畫 pulseAnimation.autoreverses = YES; // 設(shè)置重復(fù)次數(shù)為無(wú)限大 pulseAnimation.repeatCount = FLT_MAX; // 添加動(dòng)畫到layer [heartView.layer addAnimation:pulseAnimation forKey:@"transform.scale"];

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)武林網(wǎng)的支持。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 玉龙| 都安| 丰顺县| 新疆| 长寿区| 宜阳县| 嘉荫县| 德阳市| 云浮市| 兴业县| 明星| 外汇| 兴城市| 砚山县| 石家庄市| 岑溪市| 土默特左旗| 长兴县| 左贡县| 公安县| 新泰市| 阿克| 凤冈县| 太仆寺旗| 阿拉善右旗| 巴彦淖尔市| 深圳市| 三原县| 桂阳县| 建水县| 富源县| 都匀市| 冷水江市| 名山县| 隆回县| 宜丰县| 芒康县| 霍山县| 汨罗市| 金华市| 宜都市|