杨锴
2025-04-16 09a372bc45fde16fd42257ab6f78b8deeecf720b
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//
//  FFRereshView.m
//  FFPage
//
//  Created by North on 2019/8/14.
//  Copyright © 2019 North. All rights reserved.
//
 
#import "FFRereshView.h"
#import "FFPage.h"
 
static NSString *  contentOffset = @"contentOffset";
static NSString *  contentSize = @"contentSize";
static NSString *  PanState = @"state";
static NSString *  superBounds = @"bounds";
 
@interface FFRereshView ()
 
/**
 回调块
 */
@property (copy, nonatomic) FFRefreshBlock refreshBlock;
 
/**
 对象
 */
@property (nonatomic) id target;
 
/**
 方法
 */
@property (nonatomic) SEL action;
 
 
/**
 物理动画容器
 */
@property(nonatomic, strong) UIDynamicAnimator *dynamicAnimator;
 
/**
 回弹行为
 */
@property(nonatomic, weak)   UIAttachmentBehavior *bounceBehavior;
 
@end
 
@implementation FFRereshView
 
- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setUpSubView];
    }
    
    return self;
}
 
+ (instancetype)initWithRefreshBlock:(FFRefreshBlock)block{
    
    FFRereshView * view = [[self alloc] init];
    view.refreshBlock = block;
    return view;
    
}
 
+ (instancetype)initWithTarget:(id)target action:(SEL)action {
    
    FFRereshView * view = [[self alloc] init];
    view.target = target;
    view.action = action;
    return view;
}
 
 
- (CGFloat)refreshHeight{
 
    return 50;
}
 
- (void)setUpSubView{}
 
- (void)setUpKVO{
    
    NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld;
    
    [self.scrollView addObserver:self forKeyPath:contentOffset options:options context:nil];
    
    //监听滚动视图大小改变 以及可能的设备方向改变
    [self.scrollView addObserver:self forKeyPath:superBounds options:options context:nil];
    
    if (self.isFooter) {
        
        [self.scrollView addObserver:self forKeyPath:contentSize options:options context:nil];
    }
   
}
 
- (void)removeKVO{
    
    [self.scrollView removeObserver:self forKeyPath:contentOffset];
    
    [self.scrollView removeObserver:self forKeyPath:superBounds];
    
    if (self.isFooter) [self.scrollView removeObserver:self forKeyPath:contentSize];
    
}
 
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    
        if ([keyPath isEqualToString:contentOffset]) {
    
            [self calcuStatusAndProgress];
    
        } else if ([keyPath isEqualToString:contentSize]) {
    
            [self updateFrame];
            
        } else if ([keyPath isEqualToString:superBounds]) {
        
                [self updateFrame];
        }
    
}
 
- (void)willMoveToSuperview:(UIView *)newSuperview{
    [super willMoveToSuperview:newSuperview];
    
    if (!newSuperview || ![newSuperview isKindOfClass:[UIScrollView class]]) return;
    
    if (self.scrollView) [self removeKVO];
    
    self.scrollView = (UIScrollView *)newSuperview;
    
    self.dynamicAnimator =[[UIDynamicAnimator alloc]initWithReferenceView:self];
 
    [self updateTouchStatus];
    
    [self setUpKVO];
    
    [self updateFrame];
    
}
 
- (void)willMoveToWindow:(UIWindow *)newWindow{
    
    [super willMoveToWindow:newWindow];
    
    [self updateFrame];
    
}
 
- (void)updateTouchStatus{
    
    __weak typeof(self) weakself = self;
    
    [self.scrollView addTounchBlock:^(BOOL isTouch) {
         [weakself touchEndAction];
    }];
 
}
 
- (void)touchEndAction{
    
    if(self.scrollView.isTouch )return;
    
    if(self.status == FFRereshStatusWillBeginRefresh){
        
        [self beginRefresh];
    }
    
    if(self.status == FFRereshStatusWillEndRefresh){
        
        [self endRefresh];
    }
    
}
 
- (void)calcuStatusAndProgress{
    
    if (!self.scrollView.isTouch) return;
    if (self.status == FFRereshStatusRereshing) return;
    
    CGFloat offY = self.scrollView.contentOffset.y;
    
    //头部
    if (!self.isFooter && offY < 0) {
        
        CGFloat scale = fabs(offY)/[self refreshHeight];
        if (scale > 1) scale = 1;
        self.percent = scale;
 
    } else if (self.isFooter && offY > self.scrollView.maxOffsetY) {
        
        CGFloat scale = (offY - MAX(0, self.scrollView.contentSize.height - CGRectGetHeight(self.scrollView.bounds)))/[self refreshHeight];
        if (scale > 1) scale = 1;
        self.percent = scale;
        
    }
    
 
    if(self.percent <= 0 && self.status != FFRereshStatusNormal){
 
        self.status = FFRereshStatusNormal;
        return;
    }
    
   
    if (self.percent > 0 &&
       self.percent < 1 &&
       self.status  == FFRereshStatusNormal) {
           
        self.status = FFRereshStatusPulling;
        
        return;
    }
    
    if (self.percent >= 1 &&
       (self.status == FFRereshStatusPulling || self.status == FFRereshStatusWillEndRefresh)) {
        self.status = FFRereshStatusWillBeginRefresh;
        return;
    }
    
    
    if (self.percent > 0 &&
        self.percent < 1 &&
        self.status  == FFRereshStatusWillBeginRefresh
          ) {
            self.status = FFRereshStatusWillEndRefresh;
            return;
        }
    
    
    return;
    
}
 
@synthesize percent = _percent;
- (void)setPercent:(CGFloat)percent{
    
    if (_percent != percent) {
        
        _percent = percent;
        
        [self percentUpdated];
    }
    
}
@synthesize status = _status;
- (void)setStatus:(FFRereshStatus)status{
    
    if (self.status != status) {
        
        _status = status;
        
        UIEdgeInsets inset = self.scrollView.contentInset;
        
        //在将要刷新的时候 设置
        if (_status == FFRereshStatusWillBeginRefresh) {
 
            if (!self.isFooter) inset.top = [self refreshHeight];
            else inset.bottom = [self refreshHeight];
 
        }
 
        if (_status == FFRereshStatusNormal) {
            
            if (!self.isFooter) inset.top = 0;
            else inset.bottom = 0;
            
        }
        
        self.scrollView.contentInset = inset;
        
        
        [self statusUpdated];
    }
}
 
- (void)statusUpdated{
    
    
    
    
}
 
- (void)percentUpdated{
    
    
}
 
- (void)updateFrame{
    
    CGFloat height = [self refreshHeight];
    
    if (self.isFooter) {
        
        self.frame = CGRectMake(0,MAX(self.scrollView.contentSize.height, CGRectGetHeight(self.scrollView.bounds)), CGRectGetWidth(self.scrollView.bounds), height);
        return;
    }
    
    self.frame = CGRectMake(0, - height, CGRectGetWidth(self.scrollView.bounds), height);
}
 
 
- (void)beginRefresh{
    
    if (self.status == FFRereshStatusRereshing) return;
    
    //代码调用刷新
    if (self.status == FFRereshStatusNormal) {
    
        self.status  = FFRereshStatusWillBeginRefresh;
        [self beginRefresh];
        return;
    }
    
    //交互中不允许
    if (self.scrollView.isTouch ) return;
    
    self.status = FFRereshStatusRereshing;
    if (self.refreshBlock)self.refreshBlock();
    if (self.target && self.action) [self.target performSelectorOnMainThread:self.action withObject:nil waitUntilDone:NO];
    
    //只有顶部刷新的时候才会强制滚动
    if(self.isFooter)return;
    self.scrollView.isAnimationing = YES;
   
    [[NSNotificationCenter defaultCenter] postNotificationName:FFPageBeginRefreshNotice object:self userInfo:nil];
    
    CGFloat targetOffY =  -self.refreshHeight;
    
    //删除所有物理行为
    
    [self.dynamicAnimator removeAllBehaviors];
    self.bounceBehavior = nil;
 
    // 增加一个附着的行为
    
    FFDynamicItem *item = [[FFDynamicItem alloc] init];
    item.center = self.scrollView.contentOffset;
    
 
    UIAttachmentBehavior *bounceBehavior = [[UIAttachmentBehavior alloc] initWithItem:item attachedToAnchor:CGPointMake(0, targetOffY)];
    bounceBehavior.length = 0;
    bounceBehavior.damping = 1;
    bounceBehavior.frequency = 4;
    
    __weak typeof(self) weakSelf = self;
    bounceBehavior.action = ^{
        weakSelf.scrollView.contentOffset = CGPointMake(0, item.center.y);
        if (fabs(weakSelf.scrollView.contentOffset.y - targetOffY) < FLT_EPSILON) {
            [weakSelf.dynamicAnimator removeAllBehaviors];
            weakSelf.bounceBehavior = nil;
            weakSelf.scrollView.isAnimationing = NO;
           
        }
    };
    
    self.bounceBehavior = bounceBehavior;
    [self.dynamicAnimator addBehavior:bounceBehavior];
    
}
 
- (void)endRefresh{
    
    // 如果是普通状态直接返回
    if (self.status == FFRereshStatusNormal) return;
    
    // 如果是正在刷新
    if (self.status == FFRereshStatusRereshing) {
        // 将要结束刷新
        self.status = FFRereshStatusWillEndRefresh;
        [self endRefresh];
        return;
    }
    
    //交互中 不允许执行结束动画
    if (self.scrollView.isTouch) return;
    
    //结束的目标点
    CGFloat maxOffY = MAX(0, self.scrollView.contentSize.height - CGRectGetHeight(self.scrollView.bounds));
    
    //底部结束刷新时,控件被移出屏幕
    if(self.isFooter && self.scrollView.contentOffset.y < maxOffY){
        self.status = FFRereshStatusNormal;
        return;
    }
 
    // 设置正在动画中
    self.scrollView.isAnimationing = YES;
 
    [[NSNotificationCenter defaultCenter] postNotificationName:FFPageEndRefreshNotice object:self userInfo:nil];
    
    // 结束时回到的偏移量 ceil(maxOffY)防止maxoffy精度丢失
    CGFloat targetOffY = self.isFooter ? ceil(maxOffY): 0;
    
    //删除所有物理行为
    [self.dynamicAnimator removeAllBehaviors];
    self.bounceBehavior = nil;
 
    // 增加一个附着的行为
    
    FFDynamicItem *item = [[FFDynamicItem alloc] init];
    item.center = self.scrollView.contentOffset;
    
    UIAttachmentBehavior *bounceBehavior = [[UIAttachmentBehavior alloc] initWithItem:item attachedToAnchor:CGPointMake(0, targetOffY)];
    bounceBehavior.length = 0;
    bounceBehavior.damping = 1;
    bounceBehavior.frequency = 4;
    
    __weak typeof(self) weakSelf = self;
    bounceBehavior.action = ^{
        weakSelf.scrollView.contentOffset = CGPointMake(0, item.center.y);
        if (fabs(weakSelf.scrollView.contentOffset.y - targetOffY) < FLT_EPSILON) {
            [weakSelf.dynamicAnimator removeAllBehaviors];
            weakSelf.bounceBehavior = nil;
            weakSelf.scrollView.isAnimationing = NO;
            weakSelf.status = FFRereshStatusNormal;
        }
    };
    
    self.bounceBehavior = bounceBehavior;
    [self.dynamicAnimator addBehavior:bounceBehavior];
    
}
 
 
- (void)dealloc{
    
    [self removeKVO];
}
 
 
@end