IOS等待时动画效果的实现

   2015-08-13 0
核心提示:查询时间有长有短,为了增强用户体验度,目前用的比较多的手段之一是查询等待时添加一个动态等待效果,这篇文章主要介绍IOS等待时动画效果的实现,有需要的朋友可以参考下

查询时间或长或短,为了提升用户体验,目前用的比较多的手段之一就是查询等待时添加一个动态等待效果。当我们在请求网络时加载页面时有个动作效果,效果图如下:

IOS等待时动画效果的实现

源代码可以网上找开源项目Coding.net,上面的效果原理为两张图片组合,外面那个则为动画转动,里面的图标则是透明度的变化;主要代码如下:

1:把它封装在EaseLoadingView里面

@interface EaseLoadingView : UIView
@property (strong, nonatomic) UIImageView *loopView, *mon
keyView;
@property (assign, nonatomic, readonly) BOOL isLoading;
- (void)startAnimating;
- (void)stopAnimating;
@end
@interface EaseLoadingView ()
@property (nonatomic, assign) CGFloat loopAngle, mon
keyAlpha, 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"]];
  _mon
keyView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loading_mon
key"]];
  [_loopView setCenter:self.center];
  [_mon
keyView setCenter:self.center];
  [self addSubview:_loopView];
  [self addSubview:_mon
keyView];
  [_loopView mas_makeConstraints:^(MASConstraintMaker *make) {
   make.center.equalTo(self);
  }];
  [_mon
keyView mas_makeConstraints:^(MASConstraintMaker *make) {
   make.center.equalTo(self);
  }];
  _loopAngle = 0.0;
  _mon
keyAlpha = 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 (_mon
keyAlpha >= 1.0 || _mon
keyAlpha <= 0.0) {
  _alphaStep = -_alphaStep;
 }
 _mon
keyAlpha += _alphaStep;
 [UIView animateWithDuration:duration delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
  CGAffineTransform loopAngleTransform = CGAffineTransformMakeRotation(_loopAngle * (M_PI / 180.0f));
  _loopView.transform = loopAngleTransform;
  _mon
keyView.alpha = _mon
keyAlpha;
 } completion:^(BOOL finished) {
  if (_isLoading && [self superview] != nil) {
   [self loadingAnimation];
  }else{
   [self removeFromSuperview];
   _loopAngle = 0.0;
   _mon
keyAlpha = 1,0;
   _alphaStep = ABS(_alphaStep);
   CGAffineTransform loopAngleTransform = CGAffineTransformMakeRotation(_loopAngle * (M_PI / 180.0f));
   _loopView.transform = loopAngleTransform;
   _mon
keyView.alpha = _mon
keyAlpha;
  }
 }];
}
@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 动画效果
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

点击排行