iOS 프레임별 애니메이션loading 보기 구현

본고는 iOS가 프레임마다 애니메이션을loading보기로 실현하는 구체적인 코드를 공유하여 참고하도록 하였으며 구체적인 내용은 다음과 같다.
나는 일정한 주기에 따라 프레임별로 불러오는 애니메이션을 재생할 수 있는loading 보기 구성 요소를 봉인했다.코드는 다음과 같습니다.
.h 파일

#import <UIKit/UIKit.h>
 
// 
typedef enum {
    FZImageSequenceLoadingStatusStop = 1,          //  
    FZImageSequenceLoadingStatusLoading,         //  
    FZImageSequenceLoadingStatusError   // 
} FZImageSequenceLoadingStatus;
 
@interface FZImageSequenceLoadingView : UIView {
    UIImageView *_imageView;
    UILabel *_lblMsg;
    NSTimer *timer;
    int currentImageIndex;
}
 
@property(strong) NSArray *imageArray;  // 
 
@property(strong, nonatomic) UIImage *errorImage;
 
@property(nonatomic, strong) NSString *errorMsg;
 
@property(nonatomic, strong) NSString *loadingMsg; // 
 
@property(nonatomic) CGRect imageFrame; // Frame
 
@property(nonatomic) CGRect msgFrame;   // Frame
 
@property(nonatomic) float timerInterval; // 
 
/**
  
 */
- (void)switchToStatus:(FZImageSequenceLoadingStatus)status;
 
/**
  , "name"、“.png” 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 , 
        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;
    // 
    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;
    //  :
    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)), @" ");
    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에서 다음과 같이 사용합니다.
뷰 초기화:

// 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 프록시 방법에서 상태 전환:

#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];
    }
}
현재 이 구성 요소의 기능은 아직 완벽하지 않지만 현재 나의 수요를 만족시킬 수 있고 후속적으로 계속 풍부하게 할 수 있다.
이상은 본문의 전체 내용입니다. 여러분의 학습에 도움이 되고 저희를 많이 응원해 주십시오.

좋은 웹페이지 즐겨찾기