PerhapYs的OC学习日记iOS动画刷新

自定义下拉刷新上拉加载

2015-12-04  本文已影响1716人  SPIREJ
#import "MJRefreshHeader.h"
@interface XSHeaderRefresh : MJRefreshHeader
@end```

- XSHeaderRefresh.m(下面代码全部为.m文件内容)
1)自定义视图用到哪些UI元素在这里声明

import "XSHeaderRefresh.h"

@interface XSHeaderRefresh()
@property(weak, nonatomic) UIView * headerFreshView;
@property(weak, nonatomic)UILabel * label;
@property(nonatomic, strong)UIImageView * imageView;
@end```
2)这里做控件的初始化配置
@implementation XSHeaderRefresh
- (void)prepare {
[super prepare];
// 设置控件的高度
self.mj_h = 75;
UIView * view = [[UIView alloc] init];
[self addSubview:view];
self.headerFreshView = view;
UILabel *label = [[UILabel alloc] init];
label.textColor = [UIColor colorWithRed:1.0 green:0.5 blue:0.0 alpha:1.0];
label.font = [UIFont boldSystemFontOfSize:12];
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
[self.headerFreshView addSubview:label];
self.label = label;
UIImageView * imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:@"arrow"];
[self.headerFreshView addSubview:imageView];
self.imageView = imageView;
}

3)设置子控件的位置和尺寸
- (void)placeSubviews
{
[super placeSubviews];
self.headerFreshView.bounds = CGRectMake(0, 0, 110, 20);
self.headerFreshView.center = CGPointMake(self.mj_w*0.5, self.mj_h-20);
self.label.frame = CGRectMake(30, 0, 80, 20);
self.imageView.frame = CGRectMake(10, 5, 10, 10);
}
4)重写父类方法
#pragma mark 监听scrollView的contentOffset改变
- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change
{
[super scrollViewContentOffsetDidChange:change];
}

  #pragma mark 监听scrollView的contentSize改变
  - (void)scrollViewContentSizeDidChange:(NSDictionary *)change
  {
      [super scrollViewContentSizeDidChange:change];   
  }

  #pragma mark 监听scrollView的拖拽状态改变
  - (void)scrollViewPanStateDidChange:(NSDictionary *)change
  {
      [super scrollViewPanStateDidChange:change];
  }

   #pragma mark 监听控件的刷新状态
   - (void)setState:(MJRefreshState)state
   {
   MJRefreshCheckState;

   switch (state) {
   case MJRefreshStateIdle:
           self.label.text = @"下拉刷新";
           [self endAnimation];
           break;
   case MJRefreshStatePulling:
           self.label.text = @"松开加载更多";
           [self endAnimation];
           break;
   case MJRefreshStateRefreshing:
           self.label.text = @"加载中...";
           [self startAnimation];
   break;
   default:
   break;
   }
  }
  #pragma mark 监听拖拽比例(控件被拖出来的比例)
  - (void)setPullingPercent:(CGFloat)pullingPercent
  {
      [super setPullingPercent:pullingPercent];

      self.label.textColor = [UIColor blackColor];
  }

 5)图片旋转(自定义)
  -(void)startAnimation
  {
      CABasicAnimation *basicAni= [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
      basicAni.duration = 1;
      basicAni.repeatCount = MAXFLOAT;
      //    basicAni.repeatDuration = 3;
      basicAni.toValue = @(M_PI * 2);
      [self.imageView.layer addAnimation:basicAni forKey:nil];
  }
  - (void)endAnimation
  {
      [self.imageView.layer removeAllAnimations];
  }
self.productListTabView.mj_header = [XSHeaderRefresh headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
self.productListTabView.mj_footer = [XSFooterRefresh footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];

其中refreshloadMore是你刷新和加载的方法
上拉加载代码中也有示例就不多做描述了。

上一篇 下一篇

猜你喜欢

热点阅读