现在很多的app下拉刷新有好多好看动画效果,我们可以使用MJRefresh和Lottie来实现这个效果。
1、添加第三方库:
pod 'MJRefresh'
pod 'lottie-ios', '=2.5.3'
2、创建MJRefreshGifHeader的子类RefreshGifHeaderLottie
RefreshGifHeaderLottie.h
#import <Foundation/Foundation.h>
#import "MJRefreshGifHeader.h"
NS_ASSUME_NONNULL_BEGIN
@interface RefreshGifHeaderLottie : MJRefreshGifHeader
- (void)setJsonName:(NSString *)jsonName;
@end
NS_ASSUME_NONNULL_END
RefreshGifHaderLottie.m
#import "RefreshGifHeaderLottie.h"
#import <AudioToolbox/AudioToolbox.h>
#import <Lottie/Lottie.h>
@interface RefreshGifHeaderLottie ()
@property(nonatomic, strong) LOTAnimationView *loadingView;
@property(nonatomic, strong) NSString *jsonString;
@end
@implementation RefreshGifHeaderLottie
- (instancetype)init
{
if (self = [super init])
{
self.lastUpdatedTimeLabel.hidden = YES;
self.stateLabel.hidden = YES;
}
return self;
}
- (LOTAnimationView *)loadingView
{
if (!_loadingView)
{
_loadingView = [LOTAnimationView animationNamed:self.jsonString];
_loadingView.frame = CGRectMake(kScreenWidth/2 - 25, 0, 50, 50);
_loadingView.loopAnimation = YES;
_loadingView.contentMode = UIViewContentModeScaleAspectFill;
_loadingView.animationSpeed = 1.0;
}
return _loadingView;
}
- (void)setJsonName:(NSString *)jsonName
{
self.jsonString = jsonName;
[self addSubview:self.loadingView];
}
#pragma mark - innerMethod
- (void)beginRefreshing
{
if (@available(iOS 10.0, *))
{
UIImpactFeedbackGenerator *impactLight = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleMedium];
[impactLight impactOccurred];
}
else
{
AudioServicesPlaySystemSound(1502);
}
[super beginRefreshing];
}
#pragma mark - 监听控件的刷新状态
- (void)setState:(MJRefreshState)state
{
MJRefreshCheckState;
if (self.jsonString.length > 0)
{
switch (state)
{
case MJRefreshStateIdle: /// 普通闲置状态
{
[self.loadingView stop];
break;
}
case MJRefreshStatePulling:
{
break;
}
case MJRefreshStateRefreshing:
{
self.loadingView.animationProgress = 0;
[self.loadingView play];
break;
}
default:
break;
}
}
}
#pragma mark - 实时监听控件 scrollViewContentOffset
- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change
{
[super scrollViewContentOffsetDidChange:change];
if (self.jsonString.length > 0)
{
CGPoint point;
id newVelue = [change valueForKey:NSKeyValueChangeNewKey];
[(NSValue *)newVelue getValue:&point];
self.loadingView.hidden = !(self.pullingPercent);
CGFloat progress = point.y / (kScreenHeight/3.0);
if (self.state != MJRefreshStateRefreshing)
{
self.loadingView.animationProgress = -progress;
}
}
}
@end
3、控制器中的使用
懒加载RefreshGifHeaderLottie
- (RefreshGifHeaderLottie *)refreshGifHeaderLottie
{
if (!_refreshGifHeaderLottie)
{
_refreshGifHeaderLottie = [RefreshGifHeaderLottie new];
}
return _refreshGifHeaderLottie;
}
tableView中使用
[self.refreshGifHeaderLottie setJsonName:@"headerRefresh.json"];
self.homeView.tableView.mj_header = self.refreshGifHeaderLottie;
[self.refreshGifHeaderLottie setRefreshingBlock:^{
}];
网友评论