Skip to content

Commit 6d2e086

Browse files
committed
优化框架:以更少的代码实现功能
1 parent 7f24c05 commit 6d2e086

File tree

7 files changed

+84
-132
lines changed

7 files changed

+84
-132
lines changed

ESFloatingView/UIScrollView+ESFloatingView.h

-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,5 @@
1111
@interface UIScrollView (ESFloatingView)
1212

1313
@property (nonatomic, weak) UIView *ES_floatingView;
14-
@property (nonatomic, assign) BOOL ES_isFloating;
1514

1615
@end

ESFloatingView/UIScrollView+ESFloatingView.m

+14-74
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,10 @@
99
#import "UIScrollView+ESFloatingView.h"
1010
#import <objc/runtime.h>
1111

12-
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
13-
#define kKeyWindow [UIApplication sharedApplication].keyWindow
12+
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
13+
#define KeyWindow [UIApplication sharedApplication].keyWindow
1414

1515
static char KEY_ES_floatingView;
16-
static char KEY_ES_isFloating;
17-
static char KEY_displayLink;
18-
static char KEY_targetOffsetY;
19-
20-
@interface UIScrollView ()
21-
22-
@property (nonatomic, assign) CGFloat targetOffsetY;
23-
@property (strong, nonatomic) CADisplayLink *displayLink;
24-
25-
@end
2616

2717
@implementation UIScrollView (ESFloatingView)
2818

@@ -41,87 +31,48 @@ + (void)load
4131
- (instancetype)ESFloatView_initWithFrame:(CGRect)frame
4232
{
4333
UIScrollView *instance = [self ESFloatView_initWithFrame:frame];
44-
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ESFloatView_ReceivedNotification_KeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
34+
[instance ESFloatView_setup];
4535
return instance;
4636
}
4737

4838
- (instancetype)ESFloatView_initWithCoder:(NSCoder *)coder
4939
{
5040
UIScrollView *instance = [self ESFloatView_initWithCoder:coder];
51-
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ESFloatView_ReceivedNotification_KeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
41+
[instance ESFloatView_setup];
5242
return instance;
5343
}
5444

45+
- (void)ESFloatView_setup
46+
{
47+
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ESFloatView_ReceivedNotification_KeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
48+
}
49+
5550

5651
- (void)ESFloatView_ReceivedNotification_KeyboardWillShow:(NSNotification *)notification
5752
{
53+
self.autoresizesSubviews = NO;
5854
CGFloat keyboardH = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
5955

6056
if (self.ES_floatingView == NULL)
6157
{
6258
return;
6359
}
6460

65-
CGRect absFrame = [self.ES_floatingView.superview convertRect:self.ES_floatingView.frame toView:kKeyWindow];
61+
CGRect absFrame = [self.ES_floatingView.superview convertRect:self.ES_floatingView.frame toView:KeyWindow];
6662

67-
CGFloat benchmarkY = kScreenHeight - keyboardH - absFrame.size.height;
63+
CGFloat benchmarkY = ScreenHeight - keyboardH - absFrame.size.height;
6864

6965
CGFloat tableVOffset = self.contentOffset.y - (benchmarkY - absFrame.origin.y);
7066

71-
72-
self.targetOffsetY = tableVOffset;
73-
74-
if (self.contentOffset.y != self.targetOffsetY) {
75-
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
76-
}
77-
}
78-
79-
- (void)ESFloatView_scrollToOffset
80-
{
81-
if (self.contentOffset.y - self.targetOffsetY < 2 && self.contentOffset.y - self.targetOffsetY > -2)
82-
{
83-
CGPoint tmpOffset = self.contentOffset;
84-
tmpOffset.y = self.targetOffsetY;
85-
self.contentOffset = tmpOffset;
86-
87-
[self.displayLink invalidate];
88-
self.displayLink = nil;
89-
90-
self.ES_isFloating = NO;
67+
if (tableVOffset < -self.contentInset.top) {
9168
return;
9269
}
9370

94-
self.ES_isFloating = YES;
9571
CGPoint tmpOffset = self.contentOffset;
96-
tmpOffset.y += (self.targetOffsetY - self.contentOffset.y) * 0.15;
72+
tmpOffset.y = tableVOffset;
9773
self.contentOffset = tmpOffset;
9874
}
9975

100-
- (void)setTargetOffsetY:(CGFloat)targetOffsetY
101-
{
102-
objc_setAssociatedObject(self, &KEY_targetOffsetY, @(targetOffsetY), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
103-
}
104-
105-
106-
- (CGFloat)targetOffsetY
107-
{
108-
return [objc_getAssociatedObject(self, &KEY_targetOffsetY) doubleValue];
109-
}
110-
111-
- (void)setDisplayLink:(CADisplayLink *)displayLink
112-
{
113-
objc_setAssociatedObject(self, &KEY_displayLink, displayLink, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
114-
}
115-
116-
- (CADisplayLink *)displayLink
117-
{
118-
if (objc_getAssociatedObject(self, &KEY_displayLink) == NULL) {
119-
CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(ESFloatView_scrollToOffset)];
120-
objc_setAssociatedObject(self, &KEY_displayLink, displayLink, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
121-
}
122-
123-
return objc_getAssociatedObject(self, &KEY_displayLink);
124-
}
12576

12677
- (void)setES_floatingView:(UIView *)floatView
12778
{
@@ -133,16 +84,5 @@ - (UIView *)ES_floatingView
13384
return objc_getAssociatedObject(self, &KEY_ES_floatingView);
13485
}
13586

136-
- (void)setES_isFloating:(BOOL)ES_isFloating
137-
{
138-
objc_setAssociatedObject(self, &KEY_ES_isFloating, @(ES_isFloating), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
139-
}
140-
141-
- (BOOL)ES_isFloating
142-
{
143-
return [objc_getAssociatedObject(self, &KEY_ES_isFloating) boolValue];
144-
}
145-
146-
14787

14888
@end

ESFloatingView_Demo.xcodeproj/project.pbxproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
TargetAttributes = {
124124
243D5CB51E66AC4E001AAE29 = {
125125
CreatedOnToolsVersion = 8.2.1;
126-
DevelopmentTeam = KLL43ZU6XM;
126+
DevelopmentTeam = 3ZACQEMZBK;
127127
ProvisioningStyle = Automatic;
128128
};
129129
};
@@ -285,7 +285,7 @@
285285
isa = XCBuildConfiguration;
286286
buildSettings = {
287287
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
288-
DEVELOPMENT_TEAM = KLL43ZU6XM;
288+
DEVELOPMENT_TEAM = 3ZACQEMZBK;
289289
INFOPLIST_FILE = ESFloatingView_Demo/Info.plist;
290290
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
291291
PRODUCT_BUNDLE_IDENTIFIER = "E-Paper.ESFloatingView-Demo";
@@ -297,7 +297,7 @@
297297
isa = XCBuildConfiguration;
298298
buildSettings = {
299299
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
300-
DEVELOPMENT_TEAM = KLL43ZU6XM;
300+
DEVELOPMENT_TEAM = 3ZACQEMZBK;
301301
INFOPLIST_FILE = ESFloatingView_Demo/Info.plist;
302302
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
303303
PRODUCT_BUNDLE_IDENTIFIER = "E-Paper.ESFloatingView-Demo";

ESFloatingView_Demo/Assets.xcassets/AppIcon.appiconset/Contents.json

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
{
22
"images" : [
3+
{
4+
"idiom" : "iphone",
5+
"size" : "20x20",
6+
"scale" : "2x"
7+
},
8+
{
9+
"idiom" : "iphone",
10+
"size" : "20x20",
11+
"scale" : "3x"
12+
},
313
{
414
"idiom" : "iphone",
515
"size" : "29x29",
+40-46
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,58 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11762" systemVersion="16D32" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11762" systemVersion="16D32" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="vVM-Uj-2os">
33
<device id="retina4_7" orientation="portrait">
44
<adaptation id="fullscreen"/>
55
</device>
66
<dependencies>
77
<deployment identifier="iOS"/>
88
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11757"/>
9-
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
109
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
1110
</dependencies>
1211
<scenes>
12+
<!--Navigation Controller-->
13+
<scene sceneID="Ksa-uu-VvB">
14+
<objects>
15+
<navigationController id="vVM-Uj-2os" sceneMemberID="viewController">
16+
<navigationBar key="navigationBar" contentMode="scaleToFill" id="FBr-vQ-8bR">
17+
<rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
18+
<autoresizingMask key="autoresizingMask"/>
19+
</navigationBar>
20+
<connections>
21+
<segue destination="uZ6-qf-I11" kind="relationship" relationship="rootViewController" id="Qzp-bA-eB6"/>
22+
</connections>
23+
</navigationController>
24+
<placeholder placeholderIdentifier="IBFirstResponder" id="YaP-6Z-TA2" userLabel="First Responder" sceneMemberID="firstResponder"/>
25+
</objects>
26+
<point key="canvasLocation" x="-654" y="137"/>
27+
</scene>
1328
<!--View Controller-->
14-
<scene sceneID="tne-QT-ifu">
29+
<scene sceneID="iHU-1N-AhM">
1530
<objects>
16-
<viewController id="BYZ-38-t0r" customClass="ViewController" sceneMemberID="viewController">
17-
<layoutGuides>
18-
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
19-
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
20-
</layoutGuides>
21-
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
31+
<tableViewController id="uZ6-qf-I11" customClass="ViewController" sceneMemberID="viewController">
32+
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="28" sectionFooterHeight="28" id="tWR-kx-CAM">
2233
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
2334
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
24-
<subviews>
25-
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="XWJ-wB-ZHG">
26-
<rect key="frame" x="16" y="667" width="343" height="45"/>
27-
<constraints>
28-
<constraint firstAttribute="height" constant="45" id="S8c-aM-bFV"/>
29-
</constraints>
30-
<nil key="textColor"/>
31-
<fontDescription key="fontDescription" type="system" pointSize="14"/>
32-
<textInputTraits key="textInputTraits"/>
33-
</textField>
34-
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="pH1-Lh-SFG">
35-
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
36-
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
37-
<connections>
38-
<outlet property="dataSource" destination="BYZ-38-t0r" id="vzU-jQ-HuX"/>
39-
<outlet property="delegate" destination="BYZ-38-t0r" id="bcm-cp-ck3"/>
40-
</connections>
41-
</tableView>
42-
</subviews>
43-
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
44-
<constraints>
45-
<constraint firstAttribute="trailing" secondItem="pH1-Lh-SFG" secondAttribute="trailing" id="3av-8o-qFt"/>
46-
<constraint firstItem="XWJ-wB-ZHG" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leadingMargin" id="5pq-3O-jG9"/>
47-
<constraint firstItem="pH1-Lh-SFG" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leading" id="MLI-IE-9dP"/>
48-
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="XWJ-wB-ZHG" secondAttribute="bottom" constant="-45" id="U1i-e2-Sm7"/>
49-
<constraint firstAttribute="trailingMargin" secondItem="XWJ-wB-ZHG" secondAttribute="trailing" id="Vkb-pS-ajF"/>
50-
<constraint firstItem="pH1-Lh-SFG" firstAttribute="top" secondItem="8bC-Xf-vdC" secondAttribute="top" id="Zz5-1z-JTL"/>
51-
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="pH1-Lh-SFG" secondAttribute="bottom" id="hcM-hi-poJ"/>
52-
</constraints>
53-
</view>
54-
<connections>
55-
<outlet property="tableView" destination="pH1-Lh-SFG" id="uFo-jR-knn"/>
56-
<outlet property="textField" destination="XWJ-wB-ZHG" id="RgJ-DN-kBp"/>
57-
</connections>
58-
</viewController>
59-
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
35+
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
36+
<prototypes>
37+
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" id="oOJ-DE-6xh">
38+
<rect key="frame" x="0.0" y="28" width="375" height="44"/>
39+
<autoresizingMask key="autoresizingMask"/>
40+
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="oOJ-DE-6xh" id="i8b-S2-87y">
41+
<rect key="frame" x="0.0" y="0.0" width="375" height="43"/>
42+
<autoresizingMask key="autoresizingMask"/>
43+
</tableViewCellContentView>
44+
</tableViewCell>
45+
</prototypes>
46+
<connections>
47+
<outlet property="dataSource" destination="uZ6-qf-I11" id="aiH-bs-go4"/>
48+
<outlet property="delegate" destination="uZ6-qf-I11" id="8lX-QW-mtc"/>
49+
</connections>
50+
</tableView>
51+
<navigationItem key="navigationItem" id="q5m-B0-0aJ"/>
52+
</tableViewController>
53+
<placeholder placeholderIdentifier="IBFirstResponder" id="xAk-ZG-2AM" userLabel="First Responder" sceneMemberID="firstResponder"/>
6054
</objects>
61-
<point key="canvasLocation" x="136.80000000000001" y="137.18140929535232"/>
55+
<point key="canvasLocation" x="97" y="136"/>
6256
</scene>
6357
</scenes>
6458
</document>

ESFloatingView_Demo/ViewController.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#import <UIKit/UIKit.h>
1010

11-
@interface ViewController : UIViewController
11+
@interface ViewController : UITableViewController
1212

1313

1414
@end

ESFloatingView_Demo/ViewController.m

+16-7
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,24 @@
1010
#import "UIScrollView+ESFloatingView.h"
1111

1212
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
13-
@property (weak, nonatomic) IBOutlet UITableView *tableView;
14-
@property (weak, nonatomic) IBOutlet UITextField *textField;
13+
14+
@property (strong, nonatomic) UITextField *textField;
1515

1616
@end
1717

1818
@implementation ViewController
1919

2020
- (void)viewDidLoad {
2121
[super viewDidLoad];
22+
23+
self.textField = [[UITextField alloc] init];
24+
[self.view addSubview:self.textField];
25+
26+
27+
self.tableView.delegate = self;
2228
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"x"];
29+
self.automaticallyAdjustsScrollViewInsets = YES;
30+
2331
}
2432

2533
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
@@ -37,17 +45,18 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
3745

3846
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
3947
{
48+
if ([self.textField isFirstResponder]) {
49+
[self.textField endEditing:YES];
50+
return;
51+
}
52+
4053
self.tableView.ES_floatingView = [tableView cellForRowAtIndexPath:indexPath];
4154
[self.textField becomeFirstResponder];
4255
return;
4356
}
4457

45-
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
58+
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
4659
{
47-
if (scrollView.ES_isFloating) {
48-
return;
49-
}
50-
5160
[self.textField endEditing:YES];
5261
}
5362

0 commit comments

Comments
 (0)