最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

你可以在這里下載demo

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

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

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

@interface WKProgressBarLayer : CAShapeLayer
@end

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

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

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

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

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

- (void)startAnimtionWithBeginProgress:(CGFloat)beginProgress duration:(NSTimeInterval)duration
{
 [self reset];//重置動畫
 //設(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)建動畫
 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)度動畫
 animation.delegate = self; //用于判斷動畫結(jié)束
 [self addAnimation:animation forKey:@"progressAnimation"];
 self.path = toPath.CGPath;
}

 然后呢,需要在動畫的delegate與暫停、恢復(fù)動畫的方法中分別修改動畫的狀態(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)度動畫
  self.animatingStatus = WKAnimationStatusAnimating;
 }
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
 if ([anim valueForKey:@"progress"] && flag == YES) {//判斷進(jìn)度動畫
  self.animatingStatus = WKAnimationStatusComplete;
 }
}

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

首先在storyBoard擺上兩個按鈕,分別是開始與重置動畫(界面搭建很簡單,詳情見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;
}

 接下來,就是動畫開始與重置響應(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實現(xiàn)一個時間的進(jìn)度條,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • iOS開發(fā)使用XML解析網(wǎng)絡(luò)數(shù)據(jù)

    iOS開發(fā)使用XML解析網(wǎng)絡(luò)數(shù)據(jù)

    XML解析其實這個概念出現(xiàn)了算夠久了,以前javaweb什么到處都在用。這邊我們主要大致介紹下,然后在在ios編程如何使用。
    2016-02-02
  • iOS App開發(fā)中Objective-C使用正則表達(dá)式進(jìn)行匹配的方法

    iOS App開發(fā)中Objective-C使用正則表達(dá)式進(jìn)行匹配的方法

    這篇文章主要介紹了iOS App開發(fā)中Objective-C使用正則表達(dá)式進(jìn)行匹配的方法,文中舉了在iOS中驗證用戶郵箱與手機(jī)號的例子,非常實用,匹配需要的朋友可以參考下
    2016-05-05
  • IOS實現(xiàn)展開二級列表效果

    IOS實現(xiàn)展開二級列表效果

    本文通過實例代碼向大家演示在IOS中如何實現(xiàn)展開二級列表的效果,這個功能效果很好,對于日常開發(fā)APP中很有幫助,下面一起來看看如何實現(xiàn)吧。
    2016-08-08
  • iOS如何利用一句話完成轉(zhuǎn)場動畫

    iOS如何利用一句話完成轉(zhuǎn)場動畫

    這篇文章主要給大家介紹了關(guān)于iOS如何利用一句話完成轉(zhuǎn)場動畫的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-08-08
  • IO實現(xiàn)計算器功能

    IO實現(xiàn)計算器功能

    這篇文章主要為大家詳細(xì)介紹了IOS基礎(chǔ)之計算器的編寫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • iOS中常用設(shè)置返回按鈕

    iOS中常用設(shè)置返回按鈕

    本文給大家分享一段代碼關(guān)于iOS中常用設(shè)置返回按鈕的實現(xiàn)方法,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友參考下
    2016-12-12
  • 深入淺析IOS中UIControl

    深入淺析IOS中UIControl

    UIControl,相信大家對其并不陌生吧,比如平常最常用的UIButton就是繼承自UIControl的。下面通過本篇文章給大家介紹ios中UIControl,感興趣的朋友一起學(xué)習(xí)吧
    2015-10-10
  • IOS與網(wǎng)頁JS交互詳解及實例

    IOS與網(wǎng)頁JS交互詳解及實例

    這篇文章主要介紹了 IOS與網(wǎng)頁JS交互詳解及實例的相關(guān)資料,需要的朋友可以參考下
    2016-12-12
  • iOS中仿QQ側(cè)滑菜單功能

    iOS中仿QQ側(cè)滑菜單功能

    這篇文章主要介紹了iOS中仿QQ側(cè)滑菜單功能,在實現(xiàn)此功能之前,需要先了解UITabBarController的層級結(jié)構(gòu),具體實現(xiàn)思路大家可以參考下本文
    2017-07-07
  • iOS對數(shù)組進(jìn)行排序的實例代碼

    iOS對數(shù)組進(jìn)行排序的實例代碼

    本文通過實例代碼給大家講解了ios對數(shù)組進(jìn)行排序的實例方法,非常不錯,具有參考借鑒價值,需要的的朋友參考下吧
    2017-08-08

最新評論

郎溪县| 手机| 怀仁县| 莲花县| 井陉县| 南丹县| 斗六市| 平果县| 上思县| 米泉市| 常州市| 铁岭县| 德钦县| 镇赉县| 石家庄市| 虎林市| 仙游县| 永和县| 浦县| 清涧县| 岑溪市| 孟连| 会理县| 和田市| 望奎县| 汤阴县| 延庆县| 玉田县| 古田县| 闽清县| 青阳县| 广东省| 五河县| 灵丘县| 遵化市| 英吉沙县| 斗六市| 乌拉特后旗| 讷河市| 威宁| 溧水县|