美文网首页iOS开发实用技术从零开始
MJRefresh自定义下拉刷新动画样式

MJRefresh自定义下拉刷新动画样式

作者: 金丝楠 | 来源:发表于2017-04-26 09:54 被阅读260次

    在使用MJRefresh实现下拉刷新时,经常有自定义刷新动画样式的需求,那么我们就要在MJRefresh库的基础上代码改造,并尽量做到改动工作量最小。

    1、新建一个对象,继承自MJRefreshGifHeader

    #import "MJRefreshGifHeader.h"
    
    @interface YtGifHeader : MJRefreshGifHeader
    
    @end
    

    2、然后重写方法- (void)prepare,重写后记得实现[super prepare]

    #pragma mark - 重写父类的方法
    - (void)prepare{
        [super prepare];
        
        // 设置普通状态的动画图片
        NSMutableArray *idleImages = [NSMutableArray array];
        for (NSUInteger i = 1; i<8; i++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"load%lu", (unsigned long)i]];
            // 设置Gif的size 对image等比例压缩
            UIImage *newImage = [image imageByScalingToSize:CGSizeMake(75, 65)];
            [idleImages addObject:newImage];
        }
        [self setImages:idleImages forState:MJRefreshStateIdle];
        
        // 设置即将刷新状态的动画图片(一松开就会刷新的状态)
        NSMutableArray *refreshingImages = [NSMutableArray array];
        for (NSUInteger i = 1; i<8; i++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"load%lu", (unsigned long)i]];
            // 设置Gif的size 对image等比例压缩
            UIImage *newImage = [image imageByScalingToSize:CGSizeMake(75, 65)];
            [refreshingImages addObject:newImage];
        }
        [self setImages:refreshingImages forState:MJRefreshStatePulling];
        
        // 设置正在刷新状态的动画图片
        [self setImages:refreshingImages forState:MJRefreshStateRefreshing];
        
        //隐藏时间
        self.lastUpdatedTimeLabel.hidden = YES;
        //隐藏状态
        self.stateLabel.hidden = YES;
    }
    

    3、在需要下拉刷新的控制器这样实现既可

    #import "YtGifHeader.h"
    
    self.tableView.mj_header = [YtGifHeader headerWithRefreshingBlock:^{
            //刷新请求
        }];
    
    注意事项:

    mj_header视图展示出来的是GIF原图的大小,如果美工给的切图稍大则效果很丑,那么我们就要在程序中控制GIF动图的Size,思路是对image等比例缩放

    1、新建类别UIImage+Extras继承自UIImage

    #import <UIKit/UIKit.h>
    
    @interface UIImage (Extras)
    
    - (UIImage *)imageByScalingToSize:(CGSize)targetSize;
    
    @end
    

    2、配置对Image等比例缩放的代码

    #import "UIImage+Extras.h"
    
    @implementation UIImage (Extras)
    
    - (UIImage *)imageByScalingToSize:(CGSize)targetSize {
        
        UIImage *sourceImage = self;
        UIImage *newImage = nil;
        CGSize imageSize = sourceImage.size;
        CGFloat width = imageSize.width;
        CGFloat height = imageSize.height;
        CGFloat targetWidth = targetSize.width;
        CGFloat targetHeight = targetSize.height;
        CGFloat scaleFactor = 0.0;
        CGFloat scaledWidth = targetWidth;
        CGFloat scaledHeight = targetHeight;
        CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
        
        if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
            CGFloat widthFactor = targetWidth / width;
            CGFloat heightFactor = targetHeight / height;
            if (widthFactor < heightFactor)
                scaleFactor = widthFactor;
            else
                scaleFactor = heightFactor;
            scaledWidth  = width * scaleFactor;
            scaledHeight = height * scaleFactor;
            // center the image
            if (widthFactor < heightFactor) {            
                thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
            } else if (widthFactor > heightFactor) {
                thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
            }
        }
        // this is actually the interesting part:
        UIGraphicsBeginImageContext(targetSize);
        CGRect thumbnailRect = CGRectZero;
        thumbnailRect.origin = thumbnailPoint;
        thumbnailRect.size.width  = scaledWidth;
        thumbnailRect.size.height = scaledHeight;
        [sourceImage drawInRect:thumbnailRect];
        newImage = UIGraphicsGetImageFromCurrentImageContext(); 
        UIGraphicsEndImageContext(); 
        if(newImage == nil) 
            NSLog(@"could not scale image");   
        return newImage ; 
    }
    
    @end
    

    3、调用方法

            // 设置Gif的size 对image等比例压缩
            UIImage *newImage = [image imageByScalingToSize:CGSizeMake(75, 65)];
    

    相关文章

      网友评论

      本文标题:MJRefresh自定义下拉刷新动画样式

      本文链接:https://www.haomeiwen.com/subject/spxizttx.html