欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS中利用CoreAnimation實(shí)現(xiàn)一個(gè)時(shí)間的進(jìn)度條效果

 更新時(shí)間:2017年09月01日 10:42:59   作者:pretty guy  
在iOS中實(shí)現(xiàn)進(jìn)度條通常都是通過不停的設(shè)置progress來完成的,這樣的進(jìn)度條適用于網(wǎng)絡(luò)加載(上傳下載文件、圖片等)。下面通過本文給大家介紹iOS中利用CoreAnimation實(shí)現(xiàn)一個(gè)時(shí)間的進(jìn)度條,需要的的朋友參考下吧

在iOS中實(shí)現(xiàn)進(jìn)度條通常都是通過不停的設(shè)置progress來完成的,這樣的進(jìn)度條適用于網(wǎng)絡(luò)加載(上傳下載文件、圖片等)。但是對于錄制視頻這樣的需求的話,如果是按照每秒來設(shè)置進(jìn)度的話,顯得有點(diǎn)麻煩,于是我就想直接用CoreAnimation來按時(shí)間做動(dòng)畫,只要設(shè)置最大時(shí)間,其他的就不用管了,然后在視頻暫停與繼續(xù)錄制時(shí),對動(dòng)畫進(jìn)行暫停和恢復(fù)即可。錄制視頻的效果如下:

你可以在這里下載demo

那么接下來就是如何用CoreAnimation實(shí)現(xiàn)一個(gè)進(jìn)度條控件了。

首先呢,讓我們創(chuàng)建一個(gè)繼承自CAShapeLayer的WKProgressBarLayer。

WKProgressBarLayer默認(rèn)自身的bounds就是整個(gè)進(jìn)度條的大小。

@interface WKProgressBarLayer : CAShapeLayer
@end

 為了方便外部調(diào)用,首先在WKProgressBarLayer.h中定義枚舉來表明動(dòng)畫的四個(gè)狀態(tài)

typedef NS_ENUM(NSInteger, WKAnimationStatus) {
 WKAnimationStatusIdle,//空閑
 WKAnimationStatusAnimating,//動(dòng)畫中
 WKAnimationStatusPause,//暫停
 WKAnimationStatusComplete//完成
};

 接下來,定義外部調(diào)用的動(dòng)畫接口

@interface WKProgressBarLayer : CAShapeLayer
@property (nonatomic, assign, readonly) WKAnimationStatus animatingStatus;//狀態(tài)
/**
 開始動(dòng)畫
 @param duration 動(dòng)畫最大時(shí)長
 */
- (void)beginAnimationWithDuration:(CGFloat)duration;
/**
 暫停
 */
- (void)pauseAnimation;
/**
 恢復(fù)
 */
- (void)resumeAnimation;
/**
 重新開始動(dòng)畫
 @param progress 從哪個(gè)進(jìn)度開始
 @param duration 動(dòng)畫最大時(shí)長
 */
- (void)restartAnimationWithProgress:(CGFloat)progress duration:(NSTimeInterval)duration;
@end

 然后,我們在.m實(shí)現(xiàn)核心的動(dòng)畫開始方法startAnimtionWithBeginProgress:duration:,詳細(xì)解釋見代碼

- (void)startAnimtionWithBeginProgress:(CGFloat)beginProgress duration:(NSTimeInterval)duration
{
 [self reset];//重置動(dòng)畫
 //設(shè)置path
 UIBezierPath *fromPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, beginProgress * self.bounds.size.width, self.bounds.size.height)];;
 UIBezierPath *toPath = [UIBezierPath bezierPathWithRect:self.bounds];
 self.path = fromPath.CGPath;
 //創(chuàng)建動(dòng)畫
 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
 animation.fromValue = (id)fromPath.CGPath;
 animation.toValue = (id)toPath.CGPath;
 animation.duration = duration;
 [animation setValue:@1 forKey:@"progress"];//用于判斷是否是進(jìn)度動(dòng)畫
 animation.delegate = self; //用于判斷動(dòng)畫結(jié)束
 [self addAnimation:animation forKey:@"progressAnimation"];
 self.path = toPath.CGPath;
}

 然后呢,需要在動(dòng)畫的delegate與暫停、恢復(fù)動(dòng)畫的方法中分別修改動(dòng)畫的狀態(tài)

- (void)pauseAnimation
{
 CFTimeInterval pausedTime = [self convertTime:CACurrentMediaTime() fromLayer:nil];
 self.speed = 0.0;
 self.timeOffset = pausedTime;
 self.animatingStatus = WKAnimationStatusPause;
}
- (void)resumeAnimation
{
 CFTimeInterval pausedTime = [self timeOffset];
 self.speed = 1.0;
 self.timeOffset = 0.0;
 self.beginTime = 0.0;
 CFTimeInterval timeSincePause = [self convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
 self.beginTime = timeSincePause;
 self.animatingStatus = WKAnimationStatusAnimating;
}
#pragma mark - CAAnimationDelegate
/* Called when the animation begins its active duration. */
- (void)animationDidStart:(CAAnimation *)anim
{
 if (anim == [self animationForKey:@"progressAnimation"]) {//判斷進(jìn)度動(dòng)畫
  self.animatingStatus = WKAnimationStatusAnimating;
 }
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
 if ([anim valueForKey:@"progress"] && flag == YES) {//判斷進(jìn)度動(dòng)畫
  self.animatingStatus = WKAnimationStatusComplete;
 }
}

 至此,進(jìn)度條layer就完成了,現(xiàn)在創(chuàng)建一個(gè)控制器來做測試

首先在storyBoard擺上兩個(gè)按鈕,分別是開始與重置動(dòng)畫(界面搭建很簡單,詳情見demo)

然后在ViewDidLoad中添加progressLayer

- (void)viewDidLoad {
 [super viewDidLoad];
  
 WKProgressBarLayer *progressLayer = [[WKProgressBarLayer alloc] init];
 progressLayer.frame = CGRectMake(100, 100, 200, 10);
  
 [self.view.layer addSublayer:progressLayer];
  
 self.progressLayer = progressLayer;
}

 接下來,就是動(dòng)畫開始與重置響應(yīng)

- (IBAction)startOrPauseAction:(UIButton *)sender {
  switch (self.progressLayer.animatingStatus) {
   case WKAnimationStatusIdle:{
    [self.progressLayer beginAnimationWithDuration:10];
   }
    break;
   case WKAnimationStatusAnimating:{
    [self.progressLayer pauseAnimation];
   }
    break;
   case WKAnimationStatusPause:{
    [self.progressLayer resumeAnimation];
   }
    break;
   case WKAnimationStatusComplete:{
    [self.progressLayer restartAnimationWithProgress:0 duration:10];
   }
    break;
   default:
    break;
 }
 sender.selected = !sender.selected;
}
- (IBAction)resetAction:(UIButton *)sender {
 [self.progressLayer restartAnimationWithProgress:0 duration:10];
 self.startOrPauseButton.selected = YES;
}

 以上就是代碼主體了,接下來,讓我們看看效果

你可以在這里下載demo

總結(jié)

以上所述是小編給大家介紹的iOS中利用CoreAnimation實(shí)現(xiàn)一個(gè)時(shí)間的進(jìn)度條,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • 通過UIKit坐標(biāo)系來全面掌握iOS中的UIScrollView組件

    通過UIKit坐標(biāo)系來全面掌握iOS中的UIScrollView組件

    iOS開發(fā)套件中的UIScrollView組件十分強(qiáng)大,不僅是滾動(dòng),縮放操作也能夠控制自如,其核心當(dāng)然是坐標(biāo)軸上的控制,下面就通過UIKit坐標(biāo)系來全面掌握iOS中的UIScrollView組件
    2016-05-05
  • iOS實(shí)現(xiàn)通過按鈕添加和刪除控件的方法

    iOS實(shí)現(xiàn)通過按鈕添加和刪除控件的方法

    這篇文章主要為大家詳細(xì)介紹了iOS通過按鈕添加和刪除控件的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • 談?wù)刬OS開發(fā)之JSON格式數(shù)據(jù)的生成與解析

    談?wù)刬OS開發(fā)之JSON格式數(shù)據(jù)的生成與解析

    JSON格式取代了xml給網(wǎng)絡(luò)傳輸帶來了很大的便利,本篇文章主要介紹了iOS開發(fā):對象直接轉(zhuǎn)化成JSON詳解,具有一定的參考價(jià)值,有興趣的可以了解一下。
    2017-01-01
  • 實(shí)例解析iOS應(yīng)用多線程開發(fā)中NSthread類的用法

    實(shí)例解析iOS應(yīng)用多線程開發(fā)中NSthread類的用法

    這篇文章主要介紹了iOS應(yīng)用多線程開發(fā)中NSthread類的用法,代碼基于傳統(tǒng)的Objective-C,NSthread類需要的朋友可以參考下
    2016-02-02
  • IOS NSTimeInterval使用案例詳解

    IOS NSTimeInterval使用案例詳解

    這篇文章主要介紹了IOS NSTimeInterval使用案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • IOS中多手勢之間的沖突和解決辦法

    IOS中多手勢之間的沖突和解決辦法

    這篇文章主要介紹了IOS中多手勢之間的沖突和解決辦法的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • iOS下一鍵調(diào)試Push的方法詳解

    iOS下一鍵調(diào)試Push的方法詳解

    這篇文章主要給大家介紹了關(guān)于iOS下如何一鍵調(diào)試Push的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-03-03
  • iOS 原生地圖地理編碼與反地理編碼(詳解)

    iOS 原生地圖地理編碼與反地理編碼(詳解)

    下面小編就為大家?guī)硪黄猧OS 原生地圖地理編碼與反地理編碼(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04
  • 詳解iOS中跨頁面狀態(tài)同步方案比較

    詳解iOS中跨頁面狀態(tài)同步方案比較

    這篇文章主要介紹了詳解iOS中跨頁面狀態(tài)同步方案比較,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • iOS實(shí)現(xiàn)設(shè)備判斷是否安裝相關(guān)地圖(百度、高德等)

    iOS實(shí)現(xiàn)設(shè)備判斷是否安裝相關(guān)地圖(百度、高德等)

    這篇文章主要給大家介紹了關(guān)于iOS如何實(shí)現(xiàn)設(shè)備判斷是否安裝相關(guān)地圖,比如百度、高德等,其實(shí)實(shí)現(xiàn)的方法還是很簡單,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友下面來一起看看吧。
    2018-01-01

最新評論