zjf
2023-03-06 392b76515f40376b6d36f40a114850ef63650384
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//
//  PBRefreshGifHeader.m
//  IphoneBIMe
//
//  Created by zjf on 2021/2/3.
//  Copyright © 2021 ProBIM. All rights reserved.
//
 
#import "PBRefreshGifHeader.h"
#import <AudioToolbox/AudioToolbox.h>//轻抖动
#import <Lottie/Lottie.h>
 
@interface PBRefreshGifHeader ()
 
@property(nonatomic, strong) LOTAnimationView *loadingView;
//@property(nonatomic, strong) NSString *jsonString;
 
@end
 
@implementation PBRefreshGifHeader
 
- (instancetype)init {
    if (self = [super init]) {
        self.lastUpdatedTimeLabel.hidden = YES;
        self.stateLabel.hidden = YES;
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
//    self.backgroundColor = [UIColor redColor];
    [self addSubview:self.loadingView];
}
- (void)setJsonName:(NSString *)jsonName {
////    self.jsonString = jsonName;
//    [self addSubview:self.loadingView];
}
 
- (LOTAnimationView *)loadingView {
    if(_loadingView == nil) {
        //1.加载本地json
        _loadingView = [[LOTAnimationView alloc] init];
        //2.加载后台给的json(url)
        //_loadingView = [[LOTAnimationView alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]];
        _loadingView.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width / 2.0) - 15, 20, 30, 30);
        _loadingView.loopAnimation = YES;
        _loadingView.contentMode = UIViewContentModeScaleAspectFill;
        _loadingView.animationSpeed = 1.0;
        _loadingView.loopAnimation = YES;
    }
    return _loadingView;
}
 
#pragma mark - innerMethod
 
- (void)beginRefreshing {
    if (@available(iOS 10.0, *)) {//轻抖动
        UIImpactFeedbackGenerator *impactLight = [[UIImpactFeedbackGenerator alloc]initWithStyle:UIImpactFeedbackStyleMedium];
        [impactLight impactOccurred];
    } else {
        AudioServicesPlaySystemSound(1520);
    }
    [super beginRefreshing];
}
 
- (void)endRefreshing {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [super endRefreshing];
    });
}
 
#pragma mark - 监听控件的刷新状态
 
- (void)setState:(MJRefreshState)state {
    MJRefreshCheckState;
//    if(self.jsonString.length > 0) {
        switch (state) {
            case MJRefreshStateIdle:{//普通闲置状态
                [self.loadingView stop];
                [self.loadingView setAnimationNamed:@"loading_push"];
                //self.loadingView.hidden = YES;
                break;}
            case MJRefreshStatePulling:{//松开就可以进行刷新的状态
                //self.loadingView.hidden = NO;
                break;}
            case MJRefreshStateRefreshing:{//正在刷新中的状态
                NSLog(@"Refreshing=====");
                [self.loadingView setAnimationNamed:@"loading_refresh"];
                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];
        
        //id newVelue1 = [change objectForKey:NSKeyValueChangeNewKey];
        //CGPoint point1 = ((NSValue *)newVelue1).CGPointValue;//可以取值
        
        //id newVelue2 = [change objectForKey:@"new"];
        //CGPoint point2 = *((__bridge CGPoint *)(newVelue2));//无法取到值
    NSLog(@"=================%f",point.y);
    self.loadingView.hidden = !(self.pullingPercent);
    CGFloat progress = point.y/ ([UIScreen mainScreen].bounds.size.height / 10.0);
    if(self.state != MJRefreshStateRefreshing) {
        self.loadingView.animationProgress = -progress;
    }
        
//    }
}
 
@end