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

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

iOS動(dòng)畫解析之支付寶支付打鉤動(dòng)畫的實(shí)現(xiàn)方法

2019-10-21 18:43:53
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

前言

我們平時(shí)在用支付寶付款時(shí),會(huì)有一個(gè)支付中的動(dòng)畫和一個(gè)支付完成的動(dòng)畫。這篇博客主要分析一下這種動(dòng)畫效果,效果如下:

ios,支付寶打鉤動(dòng)畫,支付寶首頁(yè)實(shí)現(xiàn),打鉤動(dòng)畫

支付寶支付動(dòng)畫

一、動(dòng)畫解析

為了方便觀察,放慢了動(dòng)畫的速度并添加輔助線:

ios,支付寶打鉤動(dòng)畫,支付寶首頁(yè)實(shí)現(xiàn),打鉤動(dòng)畫

放慢后的動(dòng)畫

從圖中可以看出:加載圓弧運(yùn)動(dòng)軌跡可分為前半段和后半段;并且圓弧的起始角度(StartAngle)和結(jié)束角度(EndAngle)在做有規(guī)律的變化;

前半段: 從-0.5π到π,這一段運(yùn)動(dòng)中速度較快;StartAngle不變,始終未-0.5π;EndAngle在勻速上升,一直到π;前半段中圓弧不斷變長(zhǎng),最后形成一個(gè)3/4的圓。

后半段: 從π到1.5π,這一段運(yùn)動(dòng)速度較慢;StartAngle開始變化,從-0.5π變化到1.5π;EndAngle從π變化到1.5π,最后StartAngle和EndAngle重合于1.5π;后半段中圓弧不斷變長(zhǎng),最后直至消失。

二、實(shí)現(xiàn)代碼

1、初始化一些全局屬性

{ //刷新工具 CADisplayLink *_link; //顯示圓環(huán) CAShapeLayer *_animationLayer; //起始角度 CGFloat _startAngle; //結(jié)束角度 CGFloat _endAngle; //當(dāng)前動(dòng)畫進(jìn)度 CGFloat _progress;}

2、界面刷新工作由CADisplayLink來(lái)完成

 _link = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkAction)]; [_link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; _link.paused = true;

為了實(shí)現(xiàn)前半段和后半段的速度區(qū)別,定義了一個(gè)速度方法:

-(CGFloat)speed{ if (_endAngle > M_PI) {  return 0.1/60.0f; } return 0.8/60.0f;}

通過CADisplayLink刷新進(jìn)度,進(jìn)度增長(zhǎng)的快慢有speed決定:

-(void)displayLinkAction{ _progress += [self speed]; if (_progress >= 1) {  _progress = 0; } [self updateAnimationLayer];}

刷新貝塞爾曲線的StartAngle和EndAngle實(shí)現(xiàn)曲線的運(yùn)動(dòng):

-(void)updateAnimationLayer{ _startAngle = -M_PI_2; _endAngle = -M_PI_2 +_progress * M_PI * 2; if (_endAngle > M_PI) {  CGFloat progress1 = 1 - (1 - _progress)/0.25;  _startAngle = -M_PI_2 + progress1 * M_PI * 2; } CGFloat radius = _animationLayer.bounds.size.width/2.0f - lineWidth/2.0f; CGFloat centerX = _animationLayer.bounds.size.width/2.0f; CGFloat centerY = _animationLayer.bounds.size.height/2.0f; UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(centerX, centerY) radius:radius startAngle:_startAngle endAngle:_endAngle clockwise:true]; path.lineCapStyle = kCGLineCapRound; _animationLayer.path = path.CGPath;}

支付完成動(dòng)畫解析

為了方便觀察,放慢了動(dòng)畫的速度,并添加輔助線:

ios,支付寶打鉤動(dòng)畫,支付寶首頁(yè)實(shí)現(xiàn),打鉤動(dòng)畫

原理分析

通過上圖可知,支付完成的動(dòng)畫由兩部分組成:圓環(huán)動(dòng)畫 + 對(duì)號(hào)動(dòng)畫

三、代碼實(shí)現(xiàn)

1、圓環(huán)動(dòng)畫

這個(gè)動(dòng)畫比較簡(jiǎn)單,是利用貝塞爾曲線畫弧的功能。再利用CAShapeLayer的strokeEnd屬性加上核心動(dòng)畫實(shí)現(xiàn)的圓環(huán)動(dòng)畫。

-(void)circleAnimation{ //顯示圖層 CAShapeLayer *circleLayer = [CAShapeLayer layer]; circleLayer.frame = _animationLayer.bounds; [_animationLayer addSublayer:circleLayer]; circleLayer.fillColor = [[UIColor clearColor] CGColor]; circleLayer.strokeColor = BlueColor.CGColor; circleLayer.lineWidth = lineWidth; circleLayer.lineCap = kCALineCapRound; //運(yùn)動(dòng)路徑 CGFloat lineWidth = 5.0f; CGFloat radius = _animationLayer.bounds.size.width/2.0f - lineWidth/2.0f; UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:circleLayer.position radius:radius startAngle:-M_PI/2 endAngle:M_PI*3/2 clockwise:true]; circleLayer.path = path.CGPath; //執(zhí)行動(dòng)畫 CABasicAnimation *checkAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; checkAnimation.duration = circleDuriation; checkAnimation.fromValue = @(0.0f); checkAnimation.toValue = @(1.0f); checkAnimation.delegate = self; [checkAnimation setValue:@"checkAnimation" forKey:@"animationName"]; [circleLayer addAnimation:checkAnimation forKey:nil];}

2、對(duì)號(hào)動(dòng)畫

對(duì)號(hào)動(dòng)畫是利用了貝塞爾曲線的畫線特性,設(shè)置了兩段曲線拼接成了一個(gè)對(duì)號(hào)。如上圖所示對(duì)號(hào)由線段AB和線段BC拼接完成,然后再利用核心動(dòng)畫和CAShapeLayer的strokeEnd屬性實(shí)現(xiàn)對(duì)號(hào)動(dòng)畫。

-(void)checkAnimation{ //外切圓的邊長(zhǎng) CGFloat a = _animationLayer.bounds.size.width; //設(shè)置三個(gè)點(diǎn) A、B、C UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:CGPointMake(a*2.7/10,a*5.4/10)]; [path addLineToPoint:CGPointMake(a*4.5/10,a*7/10)]; [path addLineToPoint:CGPointMake(a*7.8/10,a*3.8/10)]; //顯示圖層 CAShapeLayer *checkLayer = [CAShapeLayer layer]; checkLayer.path = path.CGPath; checkLayer.fillColor = [UIColor clearColor].CGColor; checkLayer.strokeColor = BlueColor.CGColor; checkLayer.lineWidth = lineWidth; checkLayer.lineCap = kCALineCapRound; checkLayer.lineJoin = kCALineJoinRound; [_animationLayer addSublayer:checkLayer]; //執(zhí)行動(dòng)畫 CABasicAnimation *checkAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; checkAnimation.duration = checkDuration; checkAnimation.fromValue = @(0.0f); checkAnimation.toValue = @(1.0f); checkAnimation.delegate = self; [checkAnimation setValue:@"checkAnimation" forKey:@"animationName"]; [checkLayer addAnimation:checkAnimation forKey:nil];}

源碼下載:

Github地址

本地下載

總結(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ā)表
主站蜘蛛池模板: 迭部县| 赤水市| 留坝县| 白银市| 霍林郭勒市| 柘城县| 石门县| 扶余县| 当雄县| 大洼县| 陕西省| 于都县| 商南县| 太仓市| 东乡族自治县| 信宜市| 天等县| 普兰县| 尉氏县| 洛阳市| 东乡族自治县| 茌平县| 平湖市| 杂多县| 安福县| 西昌市| 双城市| 会理县| 泊头市| 绩溪县| 祁东县| 绵阳市| 江永县| 惠安县| 台州市| 瑞丽市| 辰溪县| 平远县| 泸水县| 亚东县| 手游|