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

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

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

2019-10-21 18:44:00
字體:
供稿:網(wǎng)友

前言

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

GIF示例:

ios,uibezierpath動(dòng)畫,ios心跳動(dòng)畫效果,caanimation,動(dòng)畫

核心代碼

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添加心跳動(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ù)為無限大 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à)值,如果有疑問大家可以留言交流,謝謝大家對(duì)VEVB武林網(wǎng)的支持。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到IOS開發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 伊金霍洛旗| 东兰县| 彩票| 松桃| 囊谦县| 亚东县| 项城市| 伊宁县| 成都市| 张家口市| 娄底市| 沙坪坝区| 德江县| 清镇市| 灵台县| 鲁甸县| 盐边县| 北京市| 长顺县| 大丰市| 龙江县| 增城市| 长子县| 益阳市| 塘沽区| 怀仁县| 江安县| 柳河县| 汝南县| 儋州市| 合水县| 湛江市| 永登县| 宣化县| 皋兰县| 富锦市| 平舆县| 崇明县| 寿阳县| 依兰县| 沙洋县|