iOS 基本動(dòng)畫、關(guān)鍵幀動(dòng)畫、利用緩動(dòng)函數(shù)實(shí)現(xiàn)物理動(dòng)畫效果
iOS基本動(dòng)畫/關(guān)鍵幀動(dòng)畫/利用緩動(dòng)函數(shù)實(shí)現(xiàn)物理動(dòng)畫效果
先說(shuō)下基本動(dòng)畫部分
基本動(dòng)畫部分比較簡(jiǎn)單, 但能實(shí)現(xiàn)的動(dòng)畫效果也很局限
使用方法大致為:
#1. 創(chuàng)建原始UI或者畫面
#2. 創(chuàng)建CABasicAnimation實(shí)例, 并設(shè)置keypart/duration/fromValue/toValue
#3. 設(shè)置動(dòng)畫最終停留的位置
#4. 將配置好的動(dòng)畫添加到layer層中
舉個(gè)例子, 比如實(shí)現(xiàn)一個(gè)圓形從上往下移動(dòng), 上代碼:
//設(shè)置原始畫面 UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; showView.layer.masksToBounds = YES; showView.layer.cornerRadius = 50.f; showView.layer.backgroundColor = [UIColor redColor].CGColor; [self.view addSubview:showView]; //創(chuàng)建基本動(dòng)畫 CABasicAnimation *basicAnimation = [CABasicAnimation animation]; //設(shè)置屬性 basicAnimation.keyPath = @"position"; basicAnimation.duration = 4.0f; basicAnimation.fromValue = [NSValue valueWithCGPoint:showView.center]; basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(50, 300)]; //設(shè)置動(dòng)畫結(jié)束位置 showView.center = CGPointMake(50, 300); //添加動(dòng)畫到layer層 [showView.layer addAnimation:basicAnimation forKey:nil];
接下來(lái)說(shuō)下關(guān)鍵幀動(dòng)畫
其實(shí)跟基本動(dòng)畫差不多, 只是能設(shè)置多個(gè)動(dòng)畫路徑 使用方法也類似, 大致為
#1. 創(chuàng)建原始UI或者畫面
#2. 創(chuàng)建CAKeyframeAnimation實(shí)例, 并設(shè)置keypart/duration/values 相比基本動(dòng)畫只能設(shè)置開(kāi)始和結(jié)束點(diǎn), 關(guān)鍵幀動(dòng)畫能添加多個(gè)動(dòng)畫路徑點(diǎn)
#3. 設(shè)置動(dòng)畫最終停留的位置
#4. 將配置好的動(dòng)畫添加到layer層中
舉個(gè)例子, 紅色圓形左右晃動(dòng)往下墜落 上代碼:
//設(shè)置原始畫面
UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
showView.layer.masksToBounds = YES;
showView.layer.cornerRadius = 50.f;
showView.layer.backgroundColor = [UIColor redColor].CGColor;
[self.view addSubview:showView];
//創(chuàng)建關(guān)鍵幀動(dòng)畫
CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animation];
//設(shè)置動(dòng)畫屬性
keyFrameAnimation.keyPath = @"position";
keyFrameAnimation.duration = 4.0f;
keyFrameAnimation.values = @[[NSValue valueWithCGPoint:showView.center],
[NSValue valueWithCGPoint:CGPointMake(100, 100)],
[NSValue valueWithCGPoint:CGPointMake(50, 150)],
[NSValue valueWithCGPoint:CGPointMake(200, 200)]];
//設(shè)置動(dòng)畫結(jié)束位置
showView.center = CGPointMake(200, 200);
//添加動(dòng)畫到layer層
[showView.layer addAnimation:keyFrameAnimation forKey:nil];
最后是利用緩動(dòng)函數(shù)配合關(guān)鍵幀動(dòng)畫實(shí)現(xiàn)比較復(fù)雜的物理性動(dòng)畫
先說(shuō)說(shuō)什么是緩動(dòng)函數(shù), 就是有高人寫了一個(gè)庫(kù)可以計(jì)算出模擬物理性動(dòng)畫(比如彈簧效果)所要的路徑
Github地址: https://github.com/YouXianMing/EasingAnimation
具體有哪些動(dòng)畫效果可看庫(kù)中的緩動(dòng)函數(shù)查詢表, 簡(jiǎn)單舉個(gè)小球落地的效果
上代碼:
//設(shè)置原始畫面
UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
showView.layer.masksToBounds = YES;
showView.layer.cornerRadius = 50.f;
showView.layer.backgroundColor = [UIColor redColor].CGColor;
[self.view addSubview:showView];
//創(chuàng)建關(guān)鍵幀動(dòng)畫
CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animation];
//設(shè)置動(dòng)畫屬性
keyFrameAnimation.keyPath = @"position";
keyFrameAnimation.duration = 4.0f;
//關(guān)鍵處, 在這里使用的緩動(dòng)函數(shù)計(jì)算點(diǎn)路徑
keyFrameAnimation.values = [YXEasing calculateFrameFromPoint:showView.center
toPoint:CGPointMake(50, 300)
func:BounceEaseOut
frameCount:4.0f * 30];
//設(shè)置動(dòng)畫結(jié)束位置
showView.center = CGPointMake(50, 300);
//添加動(dòng)畫到layer層
[showView.layer addAnimation:keyFrameAnimation forKey:nil];
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- 緩動(dòng)函數(shù)requestAnimationFrame 更好的實(shí)現(xiàn)瀏覽器經(jīng)動(dòng)畫
- js 基礎(chǔ)篇必看(點(diǎn)擊事件輪播圖的簡(jiǎn)單實(shí)現(xiàn))
- 完美實(shí)現(xiàn)八種js焦點(diǎn)輪播圖(上篇)
- js實(shí)現(xiàn)支持手機(jī)滑動(dòng)切換的輪播圖片效果實(shí)例
- js實(shí)現(xiàn)點(diǎn)擊左右按鈕輪播圖片效果實(shí)例
- zepto中使用swipe.js制作輪播圖附swipeUp,swipeDown不起效果問(wèn)題
- 原生js實(shí)現(xiàn)移動(dòng)開(kāi)發(fā)輪播圖、相冊(cè)滑動(dòng)特效
- JS實(shí)現(xiàn)左右無(wú)縫輪播圖代碼
- 簡(jiǎn)單的JS輪播圖代碼
- JS輪播圖中緩動(dòng)函數(shù)的封裝
相關(guān)文章
iOS 水波紋動(dòng)畫的實(shí)現(xiàn)效果
本篇文章主要介紹了iOS 水波紋的實(shí)現(xiàn)的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01
iOS如何保持程序在后臺(tái)長(zhǎng)時(shí)間運(yùn)行
這篇文章主要為大家詳細(xì)介紹了iOS如何保持程序在后臺(tái)長(zhǎng)時(shí)間運(yùn)行,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
禁止iPhone Safari video標(biāo)簽視頻自動(dòng)全屏的辦法
本篇文章給大家分析有沒(méi)有辦法禁止iPhone Safari video標(biāo)簽視頻自動(dòng)全屏,以下給出好多種情況分享,感興趣的朋友可以參考下2015-09-09
iOS中無(wú)限循環(huán)滾動(dòng)簡(jiǎn)單處理實(shí)現(xiàn)原理分析
這篇文章主要介紹了iOS中無(wú)限循環(huán)滾動(dòng)簡(jiǎn)單處理實(shí)現(xiàn)原理分析,需要的朋友可以參考下2017-12-12
iOS實(shí)現(xiàn)封裝一個(gè)獲取通訊錄的工具類詳解
這篇文章主要給大家介紹了關(guān)于iOS如何實(shí)現(xiàn)封裝一個(gè)獲取通訊錄的工具類的相關(guān)資料,這是自己平時(shí)封裝的一個(gè)工具類,使用非常方便,文中給出了詳細(xì)的示例代碼,需要的朋友們可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
iOS自定義collectionView實(shí)現(xiàn)毛玻璃效果
不知道大家發(fā)現(xiàn)沒(méi)有蘋果在iOS7.0之后,很多系統(tǒng)界面都使用了毛玻璃效果,增加了界面的美觀性,所以這篇文章跟大家分享個(gè)iOS自定義collectionView實(shí)現(xiàn)毛玻璃效果的方法,有需要的可以參考借鑒,下面來(lái)一起看看。2016-09-09
iOS實(shí)現(xiàn)萌貨貓頭鷹登錄界面動(dòng)畫
本文介紹的動(dòng)畫效果仿自國(guó)外網(wǎng)站readme.io的登錄界面,超萌可愛(ài)的貓頭鷹,感興趣的朋友們可以參考學(xué)習(xí)。2016-08-08

