你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS期待時動畫後果的完成

IOS期待時動畫後果的完成

編輯:IOS開發綜合

查詢時光或長或短,為了晉升用戶體驗,今朝用的比擬多的手腕之一就是查詢期待時添加一個靜態期待後果。當我們在要求收集時加載頁面時有個舉措後果,後果圖以下:

源代碼可以網上找開源項目Coding.net,下面的後果道理為兩張圖片組合,裡面誰人則為動畫遷移轉變,外面的圖標則是通明度的變更;重要代碼以下:

1:把它封裝在EaseLoadingView外面

@interface EaseLoadingView : UIView
@property (strong, nonatomic) UIImageView *loopView, *monkeyView;
@property (assign, nonatomic, readonly) BOOL isLoading;
- (void)startAnimating;
- (void)stopAnimating;
@end
@interface EaseLoadingView ()
@property (nonatomic, assign) CGFloat loopAngle, monkeyAlpha, angleStep, alphaStep;
@end

@implementation EaseLoadingView
- (instancetype)initWithFrame:(CGRect)frame{
 self = [super initWithFrame:frame];
 if (self) {
  self.backgroundColor = [UIColor clearColor];
  _loopView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loading_loop"]];
  _monkeyView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loading_monkey"]];
  [_loopView setCenter:self.center];
  [_monkeyView setCenter:self.center];
  [self addSubview:_loopView];
  [self addSubview:_monkeyView];
  [_loopView mas_makeConstraints:^(MASConstraintMaker *make) {
   make.center.equalTo(self);
  }];
  [_monkeyView mas_makeConstraints:^(MASConstraintMaker *make) {
   make.center.equalTo(self);
  }];
  _loopAngle = 0.0;
  _monkeyAlpha = 1.0;
  _angleStep = 360/3;
  _alphaStep = 1.0/3.0;
 }
 return self;
}
- (void)startAnimating{
 self.hidden = NO;
 if (_isLoading) {
  return;
 }
 _isLoading = YES;
 [self loadingAnimation];
}
- (void)stopAnimating{
 self.hidden = YES;
 _isLoading = NO;
}
- (void)loadingAnimation{
 static CGFloat duration = 0.25f;
 _loopAngle += _angleStep;
 if (_monkeyAlpha >= 1.0 || _monkeyAlpha <= 0.0) {
  _alphaStep = -_alphaStep;
 }
 _monkeyAlpha += _alphaStep;
 [UIView animateWithDuration:duration delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
  CGAff.netransform loopAngleTransform = CGAffineTransformMakeRotation(_loopAngle * (M_PI / 180.0f));
  _loopView.transform = loopAngleTransform;
  _monkeyView.alpha = _monkeyAlpha;
 } completion:^(BOOL finished) {
  if (_isLoading && [self superview] != nil) {
   [self loadingAnimation];
  }else{
   [self removeFromSuperview];
   _loopAngle = 0.0;
   _monkeyAlpha = 1,0;
   _alphaStep = ABS(_alphaStep);
   CGAffineTransform loopAngleTransform = CGAffineTransformMakeRotation(_loopAngle * (M_PI / 180.0f));
   _loopView.transform = loopAngleTransform;
   _monkeyView.alpha = _monkeyAlpha;
  }
 }];
}
@end

留意loadingAnimation這外面有舉措的處置及通明度的處置,當停滯加載後把它自個從以後的視圖去除;

2:UIView (Common)在UIView擴大類裡

#pragma mark LoadingView
@property (strong, nonatomic) EaseLoadingView *loadingView;
- (void)beginLoading;
- (void)endLoading;
@end
- (void)setLoadingView:(EaseLoadingView *)loadingView{
 [self willChangeValueForKey:@"LoadingViewKey"];
 objc_setAssociatedObject(self, &LoadingViewKey,
        loadingView,
        OBJC_ASSOCIATION_RETAIN_NONATOMIC);
 [self didChangeValueForKey:@"LoadingViewKey"];
}
- (EaseLoadingView *)loadingView{
 return objc_getAssociatedObject(self, &LoadingViewKey);
}
- (void)beginLoading{
 for (UIView *aView in [self.blankPageContainer subviews]) {
  if ([aView isKindOfClass:[EaseBlankPageView class]] && !aView.hidden) {
   return;
  }
 }
 if (!self.loadingView) { //初始化LoadingView
  self.loadingView = [[EaseLoadingView alloc] initWithFrame:self.bounds];
 }
 [self addSubview:self.loadingView];
 [self.loadingView mas_makeConstraints:^(MASConstraintMaker *make) {
  make.self.edges.equalTo(self);
 }];
 [self.loadingView startAnimating];
}
- (void)endLoading{
 if (self.loadingView) {
  [self.loadingView stopAnimating];
 }
}

留意:cocoa的KVO模子中,有兩種告訴不雅察者的方法,主動告訴和手動告訴。望文生義,主動告訴由cocoa在屬性值變更時主動告訴不雅察者,而手動告訴須要在值變更時挪用 willChangeValueForKey:和didChangeValueForKey: 辦法告訴挪用者。

3:應用頁面挪用

- (void)sendRequest{
 [self.view beginLoading];
 __weak typeof(self) weakSelf = self;
 [[Coding_NetAPIManager sharedManager] request_CodeFile:_myCodeFile withPro:_myProject andBlock:^(id data, NSError *error) {
  [weakSelf.view endLoading];
  if (data) {
   weakSelf.myCodeFile = data;
   [weakSelf refreshCodeViewData];
  }
  [weakSelf.view configBlankPage:EaseBlankPageTypeView hasData:(data != nil) hasError:(error != nil) reloadButtonBlock:^(id sender) {
   [weakSelf sendRequest];
  }];
 }];
}

個中[self.view beginLoading]跟[weakSelf.view endLoading]便可以挪用動畫後果;

彌補:另外一種是有許多分歧的圖片構成的動畫後果,可以用每張圖片然後FOR遍歷構成出舉措後果;

//設置通俗狀況的動繪圖片
 NSMutableArray *idleImages = [NSMutableArray array];
 for (NSUInteger i = 1; i<=60; ++i) {
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_anim__000%zd",i]];
    [idleImages addObject:image];
  [idleImages addObject:image];
 }

以上內容是本文經由過程IOS期待時動畫後果的完成,願望對年夜家有所贊助。

【IOS期待時動畫後果的完成】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved