iOS動畫案例(1) 類似於qq賬號信息里的一個動畫

来源:http://www.cnblogs.com/doujiangyoutiao/archive/2017/01/20/6323108.html
-Advertisement-
Play Games

   受人所托,做一個類似於qq賬號信息里的一個動畫,感覺挺有意思,也沒感覺有多難,就開始做了,結果才發現學的數學知識都還給體育老師了,研究了大半天才做出來。    先看一下 "動畫效果" : 用到的知識點: (1)三角函數 (2)CALayer (3)CAT ...


   受人所托,做一個類似於qq賬號信息里的一個動畫,感覺挺有意思,也沒感覺有多難,就開始做了,結果才發現學的數學知識都還給體育老師了,研究了大半天才做出來。
   先看一下動畫效果

  用到的知識點:
(1)三角函數
(2)CALayer
(3)CATransaction
(4)UIBezierPath
(5)CAKeyframeAnimation
(6)CAAnimationGroup


   如圖,這明顯是一段圓弧,那麼要確定這段一段圓弧的位置,就得確定這段圓弧的圓心和圓心角。我規定圓心在手機屏幕的左頂點,也就是(0,0),圓心角為60°。別問我為什麼這麼確定,我也是一點點嘗試的。我們先設手機屏幕的寬度為 ScreenWidth,圓弧半徑為R;那麼R = ScreenWidth/cos(60°);知道了這些開始畫圓弧。

    // 屏幕的寬度
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    // 圓半徑 
    float r = 2 * width / sqrt(3);
    // 畫曲線
    UIColor *color = [UIColor redColor];
    [color set];
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0, 0) radius:r startAngle:M_PI / 2 endAngle:M_PI / 6 clockwise:NO];
    path.lineWidth = 1.0;
    path.lineCapStyle = kCGLineCapRound;
    path.lineJoinStyle = kCGLineJoinRound;
    [path stroke];

   確定了圓心角和半徑就要確定ABCD四個點的坐標了,分別作為四張圖片的圓心。圓弧SA和圓弧DE的圓心角一樣,設定為7.5°,那麼弧AB、弧BC、弧CD的圓心角設定為相等,分別為(60 - 7.5 * 2)/ 3 = 15°。那麼A點的坐標就等於(R * sin7.5,R * cos7.5°);B,C,D點的坐標一樣用三角函數求,分別為(R * sin22.5,R * cos22.5°),(R * sin37.5,R * cos37.5°),(R * sin52.5,R * cos52.5°)。ABCD其實都是一個按鈕,下麵開始放按鈕。

// 放圖片
    for (int i = 0; i < 4; i++) {
    
        // 一共四個按鈕 從左到右index分別為0,1,2,3
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = [self getButtonFrame:i];
        button.tag = i + 1;
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d",i + 1]] forState:UIControlStateNormal];
        // 設置按鈕為圓
        button.layer.cornerRadius = 25;
        button.layer.borderColor = [UIColor greenColor].CGColor;
        button.layer.masksToBounds = YES;
        button.layer.borderWidth = 2.0f;
        [self addSubview:button];
    }
    // 根據Index確定按鈕的坐標
    - (CGRect)getButtonFrame: (int) index {
    
    float radians = M_PI * (7.5 + 15 * index) / 180;
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    float r = 2 * width / sqrt(3);
    CGRect frame = CGRectMake(sin(radians) * r, cos(radians) * r, 50, 50);
    frame.origin.x = frame.origin.x - 25;
    frame.origin.y = frame.origin.y - 25;
    return frame;
 }

   頭像預設放第一個。

    self.head = [[UIImageView alloc] initWithFrame:[self getButtonFrame:0]];
    self.head.image = [UIImage imageNamed:@"myHead"];
    self.head.layer.borderColor = [UIColor greenColor].CGColor;
    self.head.layer.masksToBounds = YES;
    self.head.layer.cornerRadius = 25;
    self.head.layer.borderWidth = 2.0f;
    [self addSubview:self.head];

   之後按鈕點擊之後,頭像移動到按鈕點擊的地方。

// 按鈕點擊事件
- (void)buttonClick:(UIButton *)button {
    
    // 原來圖片所在按鈕的index
    int preIndex = [self getPreviousIndexByFrame:self.head.frame];
    int buttonIndex = (int)button.tag - 1;
    // 點擊圖片所在按鈕 不做任何操作
    if (preIndex == buttonIndex) {
        return;
    }
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    float r = 2 * width / sqrt(3);
    //加入動畫效果
    CALayer *transitionLayer = [[CALayer alloc] init];
    //顯式事務預設開啟動畫效果,kCFBooleanTrue關閉 保證begin和commit 之間的屬性修改同時進行
    transitionLayer.contents = self.head.layer.contents;
    transitionLayer.borderColor = [UIColor greenColor].CGColor;
    transitionLayer.masksToBounds = YES;
    transitionLayer.cornerRadius = 25;
    transitionLayer.borderWidth = 2.0f;
    transitionLayer.frame = self.head.frame;
    transitionLayer.backgroundColor=[UIColor blueColor].CGColor;
    [self.layer addSublayer:transitionLayer];
    
    self.head.hidden = YES;
    
    UIBezierPath *movePath;
    //路徑曲線 貝塞爾曲線
    if (buttonIndex > preIndex) {
        // 向上滑 逆時針
        movePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0, 0) radius:r startAngle:[self getAnticlockwiseByIndex:preIndex] endAngle:[self getAnticlockwiseByIndex:buttonIndex] clockwise:NO];
        [movePath moveToPoint:transitionLayer.position];
    }else {
        // 向下滑 順時針
        movePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0, 0) radius:r startAngle:[self getClockwiseAngleByIndex:preIndex] endAngle:[self getClockwiseAngleByIndex:buttonIndex] clockwise:YES];
        [movePath moveToPoint:transitionLayer.position];
    }
    //關鍵幀動畫效果
    CAKeyframeAnimation *positionAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    // 動畫軌跡
    positionAnimation.path = movePath.CGPath;
    // 動畫完成之後是否刪除動畫效果
    positionAnimation.removedOnCompletion = NO;
    // 設置開始的時間
    positionAnimation.beginTime = CACurrentMediaTime();
    CGFloat time =  0.7;
    if (labs(buttonIndex - preIndex) > 1) {
        time = 0.4 * labs(buttonIndex - preIndex);

    }
    //動畫總時間
    positionAnimation.duration = time;
    // 動畫的方式 淡入淡出
    positionAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    // 執行完之後保存最新的狀態
    positionAnimation.fillMode = kCAFillModeForwards;
    // 動畫完成之後,是否回到原來的地方
    positionAnimation.autoreverses= NO;
    
    [transitionLayer addAnimation:positionAnimation forKey:@"opacity"];
    [CATransaction setCompletionBlock:^{
        [NSThread sleepForTimeInterval:time];
        self.head.hidden = NO;
        self.head.frame = button.frame;
        [transitionLayer removeFromSuperlayer];
    }];
}
// 根據Index獲得順時針的弧度
- (float)getAnticlockwiseByIndex: (NSInteger)index {
    
    return M_PI * (0.5  - (7.5 + 15 * index) / 180);
}
// 根據Index獲得逆時針的弧度
- (float)getClockwiseAngleByIndex: (NSInteger)index {
    
    index = 3 - index;
    return M_PI * (30 + 7.5 + 15 * index) / 180;
}

   這個動畫的難點其實是確定四個按鈕的坐標以及圓弧的半徑,主要是學的數學都忘的差不多了,還好重新撿起來還算不難。



您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • css快速佈局必弄清的幾件事:塊級元素&內聯元素概念釐清、盒模型、定位元素顯示優先順序總結、居中對齊方法總結、樣式繼承等。 ...
  • 我們在安裝環境的時候安裝了NDK,可以在eclipse下直接生成so文件。NDK的壓縮包裡面自帶了一些sample工程,NDK的文件直接解壓到某個目錄下即可。 第一次生成so文件的時候,我們先使用NDK的sample下的hello-jni的例子。 1、啟動eclipse,通過Create proje ...
  • 之前忘了把這些整理出來,現在補充一下,應該放在前面學習的 知識點: 1.UI的初步認識 2.UIWindow 3.UIView 4.UIlabel UI的初步認識 1.什麼是UI(*) UI即User Interface(用戶界面)的簡稱。UI設計則是指對軟 件的人機交互、操作邏輯、界面美觀的整體設 ...
  • 知識點: 1.UIView的簡單動畫 2.UIView層次關係 3.UIImageView的使用 4.UIView 停靠模式 UIView的簡單動畫 1.UIView坐標系統 1)UIView相對於父視圖的坐標系統 2.UIView的frame,center,bounds關係 frame: 該vie ...
  • 前幾天剛完工的一個定製單,需要用到分享,第三方登錄,微信支付功能。因為一直都是用友盟去集成分享和第三方登錄,所以項目初期就使用cocopads導入了友盟庫。 上個月開始做支付功能,支付寶支付沒有什麼問題,按照官方文檔順利實現。到微信支付時候,下載了微信包,導入項目,順利完成支付功能。然後開始做第三方 ...
  • 微軟新出UWP手機端調試利器App File Explorer簡介。通過瀏覽器就可以安裝卸載UWP應用,下載上傳手機端應用文件,查看應用運行記憶體等情況。 ...
  • 伴著6S的發佈,iOS 9.0開始支持3D Touch功能。使用場景來分一共有三種情況。 一、基於UIViewController的擴展 1. 首先要註冊需要監聽重按手勢的 source view: 2. 重按手勢識別出來之後需要按照 協議去處理 peek 和 pop 事件,這個協議有兩個方法 3. ...
  • 作者:Antonio Leiva 時間:Jan 19, 2017 原文鏈接:https://antonioleiva.com/anko-background-kotlin-android/ Anko是由Jetbrains用Kotlin開發的Android庫,它可以用於很多不同的方面。它的主要特性是使 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...