前言
上次給大家介紹了ios利用uibezierpath + caanimation實現心跳動畫效果的相關內容,今天實現一個根據心跳路徑實現一個路徑動畫,讓某一視圖沿著路徑進行運動.。
效果圖如下:
核心代碼
1-首先通過 drawrect 繪制心形路徑
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
- ( void )drawrect:(cgrect)rect { // drawing code // 初始化uibezierpath uibezierpath *path = [uibezierpath bezierpath]; // 首先設置一個起始點 cgpoint startpoint = cgpointmake(rect.size.width/2, 120); // 以起始點為路徑的起點 [path movetopoint:startpoint]; // 設置一個終點 cgpoint endpoint = cgpointmake(rect.size.width/2, rect.size.height-40); // 設置第一個控制點 cgpoint controlpoint1 = cgpointmake(100, 20); // 設置第二個控制點 cgpoint controlpoint2 = cgpointmake(0, 180); // 添加三次貝塞爾曲線 [path addcurvetopoint:endpoint controlpoint1:controlpoint1 controlpoint2:controlpoint2]; // 設置另一個起始點 [path movetopoint:endpoint]; // 設置第三個控制點 cgpoint controlpoint3 = cgpointmake(rect.size.width-100, 20); // 設置第四個控制點 cgpoint controlpoint4 = cgpointmake(rect.size.width, 180); // 添加三次貝塞爾曲線 [path addcurvetopoint:startpoint controlpoint1:controlpoint4 controlpoint2:controlpoint3]; // 設置線寬 path.linewidth = 3; // 設置線斷面類型 path.linecapstyle = kcglinecapround; // 設置連接類型 path.linejoinstyle = kcglinejoinround; // 設置畫筆顏色 [[uicolor redcolor] set]; [path stroke]; } |
2-添加心形路徑view到主視圖
1
2
3
|
heartview *heart = [[heartview alloc] init]; heart.frame = cgrectmake(0, 0, screen_width, screen_height-screen_height); [self.view addsubview:heart]; |
3-給動畫視圖(紅色圓形視圖)添加軌跡路徑動畫
1
2
3
4
5
6
7
8
9
10
11
|
cakeyframeanimation *animation = [cakeyframeanimation animationwithkeypath:@ "position" ]; // 設置動畫的路徑為心形路徑 animation.path = self.path.cgpath; // 動畫時間間隔 animation.duration = 3.0f; // 重復次數為最大值 animation.repeatcount = flt_max; animation.removedoncompletion = no; animation.fillmode = kcafillmodeforwards; // 將動畫添加到動畫視圖上 [_demoview.layer addanimation:animation forkey:nil]; |
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:http://blog.csdn.net/mazy_ma/article/details/55252787