匹配虛線填充動畫分析實現(xiàn)

設(shè)計師給了一個交互動畫需要實現(xiàn),是項目房間模塊匹配中的一個交互動效:圓球隨著實線轉(zhuǎn)绎狭,實線填充虛線灌侣,圓球轉(zhuǎn)到頂端開始減速,越過頂端開始加速卖陵,如圖:

一開始會想如何去讓一個圓球繞著圓心去轉(zhuǎn)遭顶,難道是需要套什么數(shù)學(xué)公式么?思考了下泪蔫,iOS 動畫庫中并沒有這種操作棒旗,然后思考了一下上面的部件層級。

部件層級

  1. 一個外環(huán)圓撩荣,很好實現(xiàn)
  2. 外環(huán)圓包著內(nèi)環(huán)虛線
  3. 一個做圓環(huán)動效的實線
  4. 一個固定的圓球
  5. 一個隨圓環(huán)滾動的圓球

過程拆分

外環(huán)圓和內(nèi)環(huán)虛線都非常好實現(xiàn)铣揉,第一反應(yīng)就是 CAShapeLayer 去畫各種圖形,并且 ShapeLayer 在性能上是優(yōu)化的餐曹。

查了很久沒看見有圓球繞著錨點做圓周運動的 API逛拱,于是我拿 Sketch 嘗試分解了一下,如圖所示:

如圖所示台猴,換了個思路朽合,雖然沒有圓球繞著錨點做圓周運動的 API,但是可以把它放在父 layer 上饱狂,然父 layer 圍繞自己的圓心自轉(zhuǎn)曹步,那么這個圓球也就繞著圓心運動了。

實現(xiàn)

首先從最簡單的開始

外圈大圓和內(nèi)圈虛線圓環(huán)

確定它們的貝塞爾曲線 path 直接繪制:

CGFloat bigLayerWidth = self.bounds.size.width;
CGFloat bigLayerHeight = self.bounds.size.height;

CGPoint position = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);

UIBezierPath *bigLayerPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, bigLayerWidth, bigLayerHeight)];


_bigLayer = [CAShapeLayer layer];
_bigLayer.frame = CGRectMake(0, 0, bigLayerWidth, bigLayerHeight);
_bigLayer.path = bigLayerPath.CGPath;
_bigLayer.lineWidth = 1.f;
_bigLayer.strokeColor = Color(whiteColor).CGColor;
_bigLayer.fillColor = Color(clearColor).CGColor;
_bigLayer.position = position;
[self.layer addSublayer:_bigLayer];

CGFloat contentLayerWidth = bigLayerWidth - 32;
CGFloat contentLayerHeight = bigLayerHeight - 32;
CGRect contentLayerRect = CGRectMake(0, 0, contentLayerWidth, contentLayerHeight);

UIBezierPath *centralCirclePath = [UIBezierPath bezierPathWithOvalInRect:contentLayerRect];

_dashCircleLayer = [CAShapeLayer layer];
_dashCircleLayer.bounds = contentLayerRect;
_dashCircleLayer.path = centralCirclePath.CGPath;
_dashCircleLayer.fillColor = Color(clearColor).CGColor;
_dashCircleLayer.strokeColor =Color(whiteColor).CGColor;
_dashCircleLayer.lineDashPattern = @[@1, @1];
_dashCircleLayer.lineWidth = 0.5f;
_dashCircleLayer.position = position;
[self.layer addSublayer:_dashCircleLayer];

此時的效果如圖所示:

內(nèi)圈動畫層的繪制

要畫的有三個部分:

  • 需要旋轉(zhuǎn)的圓環(huán)
  • 需要旋轉(zhuǎn)的圓球父 layer
  • 需要旋轉(zhuǎn)的圓球
// 要做動畫的圓環(huán)
_dynamicCircleLayer = [CAShapeLayer layer];
_dynamicCircleLayer.bounds = contentLayerRect;
_dynamicCircleLayer.path = centralCirclePath.CGPath;
_dynamicCircleLayer.fillColor = Color(clearColor).CGColor;
_dynamicCircleLayer.lineWidth = 1.0f;
_dynamicCircleLayer.strokeColor = Color(whiteColor).CGColor;
_dynamicCircleLayer.strokeStart = 0;
_dynamicCircleLayer.strokeEnd = 1;
_dynamicCircleLayer.affineTransform = CGAffineTransformMakeRotation(M_PI + M_PI_2);
_dynamicCircleLayer.position = position;
[self.layer addSublayer:_dynamicCircleLayer];

// 要做動畫的父 layer
UIBezierPath *rectPath = [UIBezierPath bezierPathWithRect:contentLayerRect];
_dynamicContentLayer = [CAShapeLayer layer];
_dynamicContentLayer.bounds = contentLayerRect;
_dynamicContentLayer.path = rectPath.CGPath;
_dynamicContentLayer.fillColor = Color(clearColor).CGColor;
_dynamicContentLayer.strokeColor= Color(redColor).CGColor;
_dynamicContentLayer.position = position;
[self.layer addSublayer:_dynamicContentLayer];

// 父 layer 上的圓球
CGFloat ballLayerRadius = 4;

_dynamicBallLayer = [CAShapeLayer layer];
_dynamicBallLayer.frame = CGRectMake(0, 0, 2 * ballLayerRadius, 2 * ballLayerRadius);
_dynamicBallLayer.cornerRadius = ballLayerRadius;
_dynamicBallLayer.backgroundColor = Color(whiteColor).CGColor;
_dynamicBallLayer.position = CGPointMake(contentLayerWidth / 2.0, 0);
[_dynamicContentLayer addSublayer:_dynamicBallLayer];

此時效果如圖所示:

不動圓球的繪制

_staticContentLayer = [CAShapeLayer layer];
_staticContentLayer.bounds = contentLayerRect;
_staticContentLayer.fillColor = Color(clearColor).CGColor;
_staticContentLayer.strokeColor= Color(whiteColor).CGColor;
_staticContentLayer.position = position;

[self.layer addSublayer:_staticContentLayer];

_staticBallLayer = [CAShapeLayer layer];
_staticBallLayer.frame = CGRectMake(0, 0, 2 * ballLayerRadius, 2 * ballLayerRadius);
_staticBallLayer.cornerRadius = ballLayerRadius;
_staticBallLayer.backgroundColor = Color(whiteColor).CGColor;
_staticBallLayer.position = CGPointMake(contentLayerWidth / 2.0, 0);
[_staticContentLayer addSublayer:_staticBallLayer];

隱藏圓球父試圖開始動畫

重力加速度動畫嗡官,如果使用系統(tǒng)的 dynamic 感應(yīng)系統(tǒng)就太麻煩了箭窜,我選擇用動畫選項淡入淡出模擬:

- (void)makeLayersAnimated {

 // 圓環(huán)動畫
 CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
 pathAnimation.duration = 3.0f;
 // 模擬重力加速度動畫
 pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
 pathAnimation.fromValue = [NSNumber numberWithFloat:0.00f];
 pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
 pathAnimation.fillMode = kCAFillModeForwards;
 pathAnimation.removedOnCompletion = NO;
 pathAnimation.repeatCount = INFINITY;
 [_dynamicCircleLayer addAnimation:pathAnimation forKey:@"strokeEndAnimation"];

 // 圓球動畫
 CABasicAnimation *animation = [CABasicAnimation animation];
 animation.keyPath = @"transform.rotation";
 animation.duration = 3.0f;
 animation.repeatCount = INFINITY;
 animation.byValue = @(M_PI * 2);
 animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];;
 [_dynamicContentLayer addAnimation:animation forKey:animation.keyPath];
}

最后在父控制器的 View 上添加這個 matching view:

CPMatchingView *matchingView = [[CPMatchingView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
[self.view addSubview:matchingView];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市衍腥,隨后出現(xiàn)的幾起案子磺樱,更是在濱河造成了極大的恐慌纳猫,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,284評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件竹捉,死亡現(xiàn)場離奇詭異芜辕,居然都是意外死亡,警方通過查閱死者的電腦和手機块差,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評論 3 395
  • 文/潘曉璐 我一進店門侵续,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人憨闰,你說我怎么就攤上這事状蜗。” “怎么了鹉动?”我有些...
    開封第一講書人閱讀 164,614評論 0 354
  • 文/不壞的土叔 我叫張陵轧坎,是天一觀的道長。 經(jīng)常有香客問我泽示,道長缸血,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,671評論 1 293
  • 正文 為了忘掉前任械筛,我火速辦了婚禮捎泻,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘埋哟。我一直安慰自己笆豁,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,699評論 6 392
  • 文/花漫 我一把揭開白布赤赊。 她就那樣靜靜地躺著渔呵,像睡著了一般。 火紅的嫁衣襯著肌膚如雪砍鸠。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,562評論 1 305
  • 那天耕驰,我揣著相機與錄音爷辱,去河邊找鬼。 笑死朦肘,一個胖子當(dāng)著我的面吹牛饭弓,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播媒抠,決...
    沈念sama閱讀 40,309評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼弟断,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了趴生?” 一聲冷哼從身側(cè)響起阀趴,我...
    開封第一講書人閱讀 39,223評論 0 276
  • 序言:老撾萬榮一對情侶失蹤昏翰,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后刘急,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體棚菊,經(jīng)...
    沈念sama閱讀 45,668評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,859評論 3 336
  • 正文 我和宋清朗相戀三年叔汁,在試婚紗的時候發(fā)現(xiàn)自己被綠了统求。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,981評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡据块,死狀恐怖码邻,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情另假,我是刑警寧澤像屋,帶...
    沈念sama閱讀 35,705評論 5 347
  • 正文 年R本政府宣布,位于F島的核電站浪谴,受9級特大地震影響开睡,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜苟耻,卻給世界環(huán)境...
    茶點故事閱讀 41,310評論 3 330
  • 文/蒙蒙 一篇恒、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧凶杖,春花似錦胁艰、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至杈湾,卻和暖如春解虱,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背漆撞。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評論 1 270
  • 我被黑心中介騙來泰國打工殴泰, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人浮驳。 一個月前我還...
    沈念sama閱讀 48,146評論 3 370
  • 正文 我出身青樓悍汛,卻偏偏與公主長得像,于是被迫代替她去往敵國和親至会。 傳聞我的和親對象是個殘疾皇子离咐,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,933評論 2 355