利用MJRefresh定制自己的刷新动画

首先创建一个文件继承自MJRefreshGifHeader,因为做的是自定义动画,所以继承这个文件,如果只是自定义文字的话可以继承自MJRefreshHeader

下面是.m文件代码,注释很详细了

- (void)prepare
{
    [super prepare];
    //创建UIImageView
    UIImageView *logoView = [[UIImageView alloc] init];
    //添加需要选装的图片,如果可以提供gif图的话,可以省略这一步,直接将gif图设置为logoView
    _rotateImgView = [[UIImageView alloc] initWithImage:GHImage(@"GHS_Refresh1")];
    _rotateImgView.right = logoView.right;
    CGFloat logoViewCenterX = logoView.center.y;
    CGFloat rotateCenterX = _rotateImgView.center.y;
    rotateCenterX = logoViewCenterX;
    [logoView addSubview:_rotateImgView];
    
    //将该UIImageView添加到当前header中
    [self addSubview:logoView];
    
    self.gifView = logoView;
    
    //根据拖拽的情况自动切换透明度
    self.automaticallyChangeAlpha = YES;
    
    //隐藏时间
    self.lastUpdatedTimeLabel.hidden = YES;
    
    //根据需求设置文字颜色
    self.stateLabel.textColor = COLOR_WITH(102, 102, 102);
    self.stateLabel.font = GHFont(12);
    
    //根据需求设置刷新文字,不同状态的刷新文字都可以自定义
    [self setTitle:@"放开可以刷新" forState:MJRefreshStatePulling];
    [self setTitle:@"正在刷新" forState:MJRefreshStateRefreshing];
    [self setTitle:@"下拉刷新" forState:MJRefreshStateWillRefresh];
    
}
/**
 *  摆放子控件
 */

- (void)placeSubviews
{
    [super placeSubviews];
    self.gifView.frame = self.bounds;
    if (self.stateLabel.hidden && self.lastUpdatedTimeLabel.hidden) {
        self.gifView.contentMode = UIViewContentModeCenter;
    } else {
        self.gifView.contentMode = UIViewContentModeRight;
        //修改loading图片的位置
        self.gifView.mj_w = self.mj_w * 0.5 - 40;
        _rotateImgView.mj_x = self.gifView.mj_w - 20;
        _rotateImgView.mj_y = (self.gifView.mj_h - 18)/2;
    }
}
//如果是gif图的话,不需要重写这个方法
- (void)setPullingPercent:(CGFloat)pullingPercent
{
    [super setPullingPercent:pullingPercent];
    NSArray *images = self.stateImages[@(MJRefreshStateIdle)];
    if (self.state == MJRefreshStatePulling) {
        //当处于放开可以刷新的状态时,添加旋转刷新动画,当正在刷新时,父类方法中就return,所以这个方法检测不到MJRefreshStateRefreshing状态
        [self rotate360DegreeWithImageView];
    }
    if (pullingPercent == 0.0) {
        GLog(@"结束刷新");
        [self.rotateImgView.layer removeAllAnimations];
        rotationAnimation = nil;
    }
    if (self.state != MJRefreshStateIdle || images.count == 0) return;
    // 停止动画
    [self.gifView stopAnimating];
    // 设置当前需要显示的图片
    NSUInteger index =  images.count * pullingPercent;
    if (index >= images.count) index = images.count - 1;
    self.gifView.image = images[index];
}
//图片360°选装动画
-(void)rotate360DegreeWithImageView {
    
    if (!rotationAnimation) {
        rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; //让其在z轴旋转
        rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];//旋转角度
        rotationAnimation.duration = 1; //旋转周期
        rotationAnimation.cumulative = YES;//旋转累加角度
        rotationAnimation.repeatCount = 100000;//旋转次数
        [self.rotateImgView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    }
}



你可能感兴趣的:(动画)