MJRefresh自定義下拉刷新動(dòng)畫樣式

在使用MJRefresh實(shí)現(xiàn)下拉刷新時(shí),經(jīng)常有自定義刷新動(dòng)畫樣式的需求,那么我們就要在MJRefresh庫(kù)的基礎(chǔ)上代碼改造,并盡量做到改動(dòng)工作量最小。

1、新建一個(gè)對(duì)象,繼承自MJRefreshGifHeader

#import "MJRefreshGifHeader.h"

@interface YtGifHeader : MJRefreshGifHeader

@end

2、然后重寫方法- (void)prepare,重寫后記得實(shí)現(xiàn)[super prepare]

#pragma mark - 重寫父類的方法
- (void)prepare{
    [super prepare];
    
    // 設(shè)置普通狀態(tài)的動(dòng)畫圖片
    NSMutableArray *idleImages = [NSMutableArray array];
    for (NSUInteger i = 1; i<8; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"load%lu", (unsigned long)i]];
        // 設(shè)置Gif的size 對(duì)image等比例壓縮
        UIImage *newImage = [image imageByScalingToSize:CGSizeMake(75, 65)];
        [idleImages addObject:newImage];
    }
    [self setImages:idleImages forState:MJRefreshStateIdle];
    
    // 設(shè)置即將刷新狀態(tài)的動(dòng)畫圖片(一松開就會(huì)刷新的狀態(tài))
    NSMutableArray *refreshingImages = [NSMutableArray array];
    for (NSUInteger i = 1; i<8; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"load%lu", (unsigned long)i]];
        // 設(shè)置Gif的size 對(duì)image等比例壓縮
        UIImage *newImage = [image imageByScalingToSize:CGSizeMake(75, 65)];
        [refreshingImages addObject:newImage];
    }
    [self setImages:refreshingImages forState:MJRefreshStatePulling];
    
    // 設(shè)置正在刷新狀態(tài)的動(dòng)畫圖片
    [self setImages:refreshingImages forState:MJRefreshStateRefreshing];
    
    //隱藏時(shí)間
    self.lastUpdatedTimeLabel.hidden = YES;
    //隱藏狀態(tài)
    self.stateLabel.hidden = YES;
}

3、在需要下拉刷新的控制器這樣實(shí)現(xiàn)既可

#import "YtGifHeader.h"

self.tableView.mj_header = [YtGifHeader headerWithRefreshingBlock:^{
        //刷新請(qǐng)求
    }];
注意事項(xiàng):

mj_header視圖展示出來的是GIF原圖的大小,如果美工給的切圖稍大則效果很丑,那么我們就要在程序中控制GIF動(dòng)圖的Size,思路是對(duì)image等比例縮放

1、新建類別UIImage+Extras繼承自UIImage

#import <UIKit/UIKit.h>

@interface UIImage (Extras)

- (UIImage *)imageByScalingToSize:(CGSize)targetSize;

@end

2、配置對(duì)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、調(diào)用方法

        // 設(shè)置Gif的size 對(duì)image等比例壓縮
        UIImage *newImage = [image imageByScalingToSize:CGSizeMake(75, 65)];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,327評(píng)論 4 61
  • 寫在前面 “什么值得買”是我這種剁手族常用的軟件,最近發(fā)現(xiàn)它的下拉刷新做得挺好的,而且也算是一種經(jīng)常見到的樣式,正...
    ZZZEoEv閱讀 4,701評(píng)論 2 45
  • 總有人說自己之所以恐婚,是因?yàn)榛橐鍪菒矍榈膲災(zāi)梗鼤?huì)如同煉爐一般,用油鹽醬醋的日常將辛苦尋覓得的愛情焚煉得灰飛煙滅...
    行不留閱讀 1,506評(píng)論 3 5
  • 上次的創(chuàng)業(yè)研習(xí)課中,泓默老師提到,廈門的面授課有可能會(huì)安排同學(xué)們“上臺(tái)”分享,哇咔咔,一聽到要上臺(tái),我就好緊張,好...
    苗苗在故鄉(xiāng)閱讀 318評(píng)論 2 2

友情鏈接更多精彩內(nèi)容