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

iOS實(shí)現(xiàn)逐幀動畫做loading視圖

 更新時(shí)間:2021年05月18日 14:24:45   作者:shannonchou  
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)逐幀動畫做loading視圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了iOS實(shí)現(xiàn)逐幀動畫做loading視圖的具體代碼,供大家參考,具體內(nèi)容如下

我封裝了一個(gè)可復(fù)用的loading視圖組件,用于按照一定周期逐幀播放加載動畫。代碼如下:

.h文件

#import <UIKit/UIKit.h>
 
//加載狀態(tài)
typedef enum {
    FZImageSequenceLoadingStatusStop = 1,          // 停止
    FZImageSequenceLoadingStatusLoading,         // 加載中
    FZImageSequenceLoadingStatusError   //發(fā)生錯(cuò)誤
} FZImageSequenceLoadingStatus;
 
@interface FZImageSequenceLoadingView : UIView {
    UIImageView *_imageView;
    UILabel *_lblMsg;
    NSTimer *timer;
    int currentImageIndex;
}
 
@property(strong) NSArray *imageArray;  //動畫序列的圖片數(shù)組
 
@property(strong, nonatomic) UIImage *errorImage;
 
@property(nonatomic, strong) NSString *errorMsg;
 
@property(nonatomic, strong) NSString *loadingMsg; //提示文字
 
@property(nonatomic) CGRect imageFrame; //圖片的Frame
 
@property(nonatomic) CGRect msgFrame;   //文字內(nèi)容的Frame
 
@property(nonatomic) float timerInterval; //切換圖片的周期
 
/**
 切換狀態(tài)
 */
- (void)switchToStatus:(FZImageSequenceLoadingStatus)status;
 
/**
 通過圖片名字和數(shù)量設(shè)置圖片數(shù)組,如給定名字"name"、“.png”和數(shù)量4,則會去加載“name_1.png”到"name_4.png"的圖片
 */
- (void)setImageArrayByName:(NSString *)name andExtName:(NSString *)extName andCount:(int)count;
 
@end

.m文件

#import "FZImageSequenceLoadingView.h"
 
@implementation FZImageSequenceLoadingView
 
@synthesize errorImage;
@synthesize errorMsg;
@synthesize imageArray;
@synthesize loadingMsg;
@synthesize timerInterval;
 
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        timerInterval = 1;
        currentImageIndex = -1;
    }
    return self;
}
 
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */
 
- (void)setupSubviews {
//    self.backgroundColor = [UIColor redColor];
    if (self.imageArray && self.imageArray.count > 0) {
        if (!_imageView) {
            _imageView = [[UIImageView alloc] init];
            [self addSubview:_imageView];
        }
        //讓圖片view為本view的頂部居中,大小為圖片數(shù)組中的第一張
        UIImage *firstImg = [self.imageArray objectAtIndex:0];
        _imageView.size = firstImg.size;
        _imageView.top = 0;
        _imageView.left = (self.size.width - _imageView.size.width) / 2;
    }
    
    
    if (self.loadingMsg) {
        CGSize labelSize = [self.loadingMsg sizeWithFont:[UIFont systemFontOfSize:11]];
        if (!_lblMsg) {
            _lblMsg = [[UILabel alloc] initWithFrame:CGRectZero];
            _lblMsg.textAlignment = NSTextAlignmentCenter;
            [self addSubview:_lblMsg];
        }
        _lblMsg.font = [UIFont systemFontOfSize:11];
        _lblMsg.size = labelSize;
        _lblMsg.textColor = [UIColor darkGrayColor];
        _lblMsg.backgroundColor = [UIColor clearColor];
        _lblMsg.bottom = self.height;
        _lblMsg.left = (self.width - _lblMsg.width) / 2;
    }
}
 
- (void)switchToStatus:(FZImageSequenceLoadingStatus)status {
    if (!_lblMsg || !_imageView) {
        [self setupSubviews];
    }
    switch (status) {
        case FZImageSequenceLoadingStatusError:
            [self switchToError];
            break;
        case FZImageSequenceLoadingStatusLoading:
            [self switchToLoading];
            break;
        case FZImageSequenceLoadingStatusStop:
            [self switchToStop];
            break;
    }
}
 
- (void)switchToStop {
    [timer invalidate];
    timer = nil;
    if (self.imageArray && self.imageArray.count > 0) {
        _imageView.image = [self.imageArray objectAtIndex:0];
    }
}
 
- (void)switchToError {
    [timer invalidate];
    timer = nil;
    //如果有錯(cuò)誤狀態(tài)的圖
    if (self.errorImage) {
        _imageView.image = self.errorImage;
        //如果沒有就用第一張動畫圖
    } else if (self.imageArray && self.imageArray.count > 0) {
        _imageView.image = [self.imageArray objectAtIndex:0];
    }
    
    if (self.errorMsg) {
        _lblMsg.text = self.errorMsg;
    }
}
 
- (void)switchToLoading {
    if (self.loadingMsg) {
        _lblMsg.text = self.loadingMsg;
    }
    if (!timer) {
        timer = [NSTimer scheduledTimerWithTimeInterval:self.timerInterval target:self selector:@selector(showNextImage) userInfo:nil repeats:YES];
    }
}
 
- (void)showNextImage {
    if (!imageArray || imageArray.count < 1) {
        return;
    }
    currentImageIndex = (currentImageIndex + 1) % self.imageArray.count;
    // 主線程執(zhí)行:
    dispatch_async(dispatch_get_main_queue(), ^{
        _imageView.image = [imageArray objectAtIndex:currentImageIndex];
    });
}
 
- (void)setImageArrayByName:(NSString *)name andExtName:(NSString *)extName andCount:(int)count {
    NSAssert((name && extName && (count > 0)), @"圖片名字和數(shù)量錯(cuò)誤");
    NSMutableArray *imgs = [NSMutableArray arrayWithCapacity:count];
    for (int i = 1; i <= count; i++) {
        NSString *imgName = [NSString stringWithFormat:@"%@_%i%@", name, i, extName];
        UIImage *image = [UIImage imageNamed:imgName];
        NSLog(@"%@", image);
        if (!image) {
            continue;
        }
        [imgs addObject:image];
    }
    self.imageArray = imgs;
}
 
@end

使用示例,在uiwebview中使用如下:

初始化視圖:

//設(shè)置loading視圖
- (void)setupLoadingView {
    if (!_loadingView) {
        _loadingView = [[FZImageSequenceLoadingView alloc] initWithFrame:CGRectMake(0, 0, 170, 70)];
        _loadingView.center = self.view.center;
        [_loadingView setImageArrayByName:@"loading" andExtName:@".png" andCount:10];
        _loadingView.loadingMsg = @"努力加載中,請稍候";
        _loadingView.errorMsg = @"加載失敗";
        _loadingView.timerInterval = 0.1;
        _loadingView.hidden = YES;
        [self.view addSubview:_loadingView];
    }
}

在uiwebview的代理方法中切換狀態(tài):

#pragma mark - webview delegate
- (void)webViewDidStartLoad:(UIWebView *)webView {
    if (_loadingView.hidden) {
        _loadingView.hidden = NO;
        [_loadingView switchToStatus:FZImageSequenceLoadingStatusLoading];        
    }
}
 
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    if (!_loadingView.hidden) {
        [_loadingView switchToStatus:FZImageSequenceLoadingStatusStop];
        _loadingView.hidden = YES;
    }
    
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    NSLog(@"load page error:%@", [error description]);
    if (!_loadingView.hidden) {
        [_loadingView switchToStatus:FZImageSequenceLoadingStatusError];
    }
}

目前該組件功能還不夠完善,但是能滿足目前我自己的需求,后續(xù)再繼續(xù)豐富。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS中判斷Emoji表情問題

    iOS中判斷Emoji表情問題

    在項(xiàng)目中遇到ios判斷emoji表情的問題,下面小編把我的解決方案分享到腳本之家平臺供大家參考
    2016-06-06
  • iOS項(xiàng)目的開發(fā)命名規(guī)范教程

    iOS項(xiàng)目的開發(fā)命名規(guī)范教程

    為了團(tuán)隊(duì)各成員之間代碼的互通、可讀、易維護(hù)性,特制訂此開發(fā)規(guī)范。下面這篇文章主要給大家介紹了關(guān)于iOS項(xiàng)目的開發(fā)命名規(guī)范的相關(guān)資料,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11
  • iOS App開發(fā)中通過UIDevice類獲取設(shè)備信息的方法

    iOS App開發(fā)中通過UIDevice類獲取設(shè)備信息的方法

    UIDevice最常見的用法就是用來監(jiān)測iOS設(shè)備的電量了,然后再實(shí)現(xiàn)電池狀態(tài)通知非常方便,除此之外還有傳感器等信息的獲取,這里我們就來總結(jié)一下iOS App開發(fā)中通過UIDevice類獲取設(shè)備信息的方法:
    2016-07-07
  • iOS實(shí)現(xiàn)文件上傳功能

    iOS實(shí)現(xiàn)文件上傳功能

    這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)文件上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • IOS定制屬于自己的個(gè)性頭像

    IOS定制屬于自己的個(gè)性頭像

    這篇文章主要為大家介紹了IOS定制屬于自己的個(gè)性頭像,實(shí)現(xiàn)方法很簡單,感興趣的小伙伴們可以參考一下
    2016-01-01
  • IOS獲取系統(tǒng)相冊中照片的示例代碼

    IOS獲取系統(tǒng)相冊中照片的示例代碼

    在大家的日常開發(fā)中,經(jīng)常會遇到有的app需要從系統(tǒng)相冊中獲取圖片,如設(shè)置用戶頭像等,下面這篇文章給大家分享這個(gè)功能的實(shí)現(xiàn),有需要的可以參考借鑒。
    2016-09-09
  • iOS底層實(shí)例解析Swift閉包及OC閉包

    iOS底層實(shí)例解析Swift閉包及OC閉包

    這篇文章主要為大家介紹了iOS底層實(shí)例解析Swift閉包及OC閉包,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • iOS?Swift?Lazy?var?View失效問題解決

    iOS?Swift?Lazy?var?View失效問題解決

    這篇文章主要為大家介紹了iOS?Swift?Lazy?var?View失效問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • 替代pod update速度慢的lg_pod_plugin安裝使用詳解

    替代pod update速度慢的lg_pod_plugin安裝使用詳解

    這篇文章主要介紹了替代pod update速度慢lg_pod_plugin安裝使用方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • iOS開發(fā)————詳解適配iOS10問題

    iOS開發(fā)————詳解適配iOS10問題

    ios10已經(jīng)推出一段時(shí)間了,這篇文章主要介紹了iOS開發(fā)————詳解適配iOS10,有興趣的可以了解一下。
    2016-12-12

最新評論

祁连县| 梨树县| 浮梁县| 枣庄市| 广东省| 重庆市| 长白| 大荔县| 大港区| 永登县| 托克逊县| 安庆市| 清苑县| 眉山市| 仪陇县| 修水县| 黎平县| 昌图县| 荔浦县| 亳州市| 天全县| 临西县| 保康县| 西乌珠穆沁旗| 绥宁县| 溧水县| 浦东新区| 潜江市| 那坡县| 岳池县| 东源县| 潢川县| 金塔县| 林西县| 宣化县| 屏边| 望都县| 葵青区| 丘北县| 新丰县| 石狮市|