杨锴
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
/**
 * Tencent is pleased to support the open source community by making QMUI_iOS available.
 * Copyright (C) 2016-2021 THL A29 Limited, a Tencent company. All rights reserved.
 * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://opensource.org/licenses/MIT
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 */
 
//
//  UIScrollView+QMUI.m
//  qmui
//
//  Created by QMUI Team on 15/7/20.
//
 
#import "UIScrollView+QMUI.h"
#import "QMUICore.h"
#import "NSNumber+QMUI.h"
#import "UIView+QMUI.h"
#import "UIViewController+QMUI.h"
 
@interface UIScrollView ()
 
@property(nonatomic, assign) CGFloat qmuiscroll_lastInsetTopWhenScrollToTop;
@property(nonatomic, assign) BOOL qmuiscroll_hasSetInitialContentInset;
@end
 
@implementation UIScrollView (QMUI)
 
QMUISynthesizeCGFloatProperty(qmuiscroll_lastInsetTopWhenScrollToTop, setQmuiscroll_lastInsetTopWhenScrollToTop)
QMUISynthesizeBOOLProperty(qmuiscroll_hasSetInitialContentInset, setQmuiscroll_hasSetInitialContentInset)
 
+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        OverrideImplementation([UIScrollView class], @selector(description), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP (^originalIMPProvider)(void)) {
            return ^NSString *(UIScrollView *selfObject) {
                // call super
                NSString *(*originSelectorIMP)(id, SEL);
                originSelectorIMP = (NSString *(*)(id, SEL))originalIMPProvider();
                NSString *result = originSelectorIMP(selfObject, originCMD);
                
                if (NSThread.isMainThread) {
                    result = ([NSString stringWithFormat:@"%@, contentInset = %@", result, NSStringFromUIEdgeInsets(selfObject.contentInset)]).mutableCopy;
                }
                return result;
            };
        });
        
        if (QMUICMIActivated && AdjustScrollIndicatorInsetsByContentInsetAdjustment) {
            OverrideImplementation([UIScrollView class], @selector(setContentInsetAdjustmentBehavior:), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP (^originalIMPProvider)(void)) {
                return ^(UIScrollView *selfObject, UIScrollViewContentInsetAdjustmentBehavior firstArgv) {
                    
                    // call super
                    void (*originSelectorIMP)(id, SEL, UIScrollViewContentInsetAdjustmentBehavior);
                    originSelectorIMP = (void (*)(id, SEL, UIScrollViewContentInsetAdjustmentBehavior))originalIMPProvider();
                    originSelectorIMP(selfObject, originCMD, firstArgv);
                    
                    if (firstArgv == UIScrollViewContentInsetAdjustmentNever) {
                        selfObject.automaticallyAdjustsScrollIndicatorInsets = NO;
                    } else {
                        selfObject.automaticallyAdjustsScrollIndicatorInsets = YES;
                    }
                };
            });
        }
    });
}
 
- (BOOL)qmui_alreadyAtTop {
    if (CGFloatEqualToFloat(self.contentOffset.y, -self.adjustedContentInset.top)) {
        return YES;
    }
    
    return NO;
}
 
- (BOOL)qmui_alreadyAtBottom {
    if (!self.qmui_canScroll) {
        return YES;
    }
    
    if (((NSInteger)self.contentOffset.y) == ((NSInteger)self.contentSize.height + self.adjustedContentInset.bottom - CGRectGetHeight(self.bounds))) {
        return YES;
    }
    
    return NO;
}
 
- (UIEdgeInsets)qmui_contentInset {
    return self.adjustedContentInset;
}
 
static char kAssociatedObjectKey_initialContentInset;
- (void)setQmui_initialContentInset:(UIEdgeInsets)qmui_initialContentInset {
    objc_setAssociatedObject(self, &kAssociatedObjectKey_initialContentInset, [NSValue valueWithUIEdgeInsets:qmui_initialContentInset], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    self.contentInset = qmui_initialContentInset;
    self.scrollIndicatorInsets = qmui_initialContentInset;
    if (!self.qmuiscroll_hasSetInitialContentInset || !self.qmui_viewController || self.qmui_viewController.qmui_visibleState < QMUIViewControllerDidAppear) {
        [self qmui_scrollToTopUponContentInsetTopChange];
    }
    self.qmuiscroll_hasSetInitialContentInset = YES;
}
 
- (UIEdgeInsets)qmui_initialContentInset {
    return [((NSValue *)objc_getAssociatedObject(self, &kAssociatedObjectKey_initialContentInset)) UIEdgeInsetsValue];
}
 
- (BOOL)qmui_canScroll {
    // 没有高度就不用算了,肯定不可滚动,这里只是做个保护
    if (CGSizeIsEmpty(self.bounds.size)) {
        return NO;
    }
    BOOL canVerticalScroll = self.contentSize.height + UIEdgeInsetsGetVerticalValue(self.adjustedContentInset) > CGRectGetHeight(self.bounds);
    BOOL canHorizontalScoll = self.contentSize.width + UIEdgeInsetsGetHorizontalValue(self.adjustedContentInset) > CGRectGetWidth(self.bounds);
    return canVerticalScroll || canHorizontalScoll;
}
 
- (void)qmui_scrollToTopForce:(BOOL)force animated:(BOOL)animated {
    if (force || (!force && [self qmui_canScroll])) {
        [self setContentOffset:CGPointMake(-self.adjustedContentInset.left, -self.adjustedContentInset.top) animated:animated];
    }
}
 
- (void)qmui_scrollToTopAnimated:(BOOL)animated {
    [self qmui_scrollToTopForce:NO animated:animated];
}
 
- (void)qmui_scrollToTop {
    [self qmui_scrollToTopAnimated:NO];
}
 
- (void)qmui_scrollToTopUponContentInsetTopChange {
    if (self.qmuiscroll_lastInsetTopWhenScrollToTop != self.contentInset.top) {
        [self qmui_scrollToTop];
        self.qmuiscroll_lastInsetTopWhenScrollToTop = self.contentInset.top;
    }
}
 
- (void)qmui_scrollToBottomAnimated:(BOOL)animated {
    if ([self qmui_canScroll]) {
        [self setContentOffset:CGPointMake(self.contentOffset.x, self.contentSize.height + self.adjustedContentInset.bottom - CGRectGetHeight(self.bounds)) animated:animated];
    }
}
 
- (void)qmui_scrollToBottom {
    [self qmui_scrollToBottomAnimated:NO];
}
 
- (void)qmui_stopDeceleratingIfNeeded {
    if (self.decelerating) {
        [self setContentOffset:self.contentOffset animated:NO];
    }
}
 
- (void)qmui_setContentInset:(UIEdgeInsets)contentInset animated:(BOOL)animated {
    [UIView qmui_animateWithAnimated:animated duration:.25 delay:0 options:QMUIViewAnimationOptionsCurveOut animations:^{
        self.contentInset = contentInset;
    } completion:nil];
}
 
@end