Skip to content

Commit 69d59c2

Browse files
committed
- CADisplayLink cleanup and weak target
- Jalapeno fix for vertical transformation - Vertical page control example
1 parent 510607b commit 69d59c2

4 files changed

Lines changed: 75 additions & 6 deletions

File tree

CHIPageControl/CHIPageControlJalapeno.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ open class CHIPageControlJalapeno: CHIBasePageControl {
9797
middleX = rightX
9898
}
9999

100-
let top = (self.frame.size.height - self.diameter)*0.5
100+
let top = (self.bounds.size.height - self.diameter)*0.5
101101

102102
let points:[CGPoint] = [
103103
CGPoint(x:leftX, y:radius + top),

CHIPageControl/Core/CHIBasePageControl.swift

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ import UIKit
115115
}
116116

117117
internal func setupDisplayLink() {
118-
self.displayLink = CADisplayLink(target: self, selector: #selector(updateFrame))
118+
self.displayLink = CADisplayLink(target: WeakProxy(self), selector: #selector(updateFrame))
119119
self.displayLink?.add(to: .current, forMode: .commonModes)
120120
}
121121

@@ -197,6 +197,11 @@ import UIKit
197197
func update(for progress: Double) {
198198
fatalError("Should be implemented in child class")
199199
}
200+
201+
deinit {
202+
self.displayLink?.remove(from: .current, forMode: .commonModes)
203+
self.displayLink?.invalidate()
204+
}
200205
}
201206

202207
extension CHIBasePageControl {
@@ -212,3 +217,21 @@ extension CHIBasePageControl {
212217
return UIColor(red: l1*r1 + l2*r2, green: l1*g1 + l2*g2, blue: l1*b1 + l2*b2, alpha: l1*a1 + l2*a2)
213218
}
214219
}
220+
221+
final class WeakProxy: NSObject {
222+
weak var target: NSObjectProtocol?
223+
224+
init(_ target: NSObjectProtocol) {
225+
self.target = target
226+
super.init()
227+
}
228+
229+
override func responds(to aSelector: Selector!) -> Bool {
230+
guard let target = target else { return super.responds(to: aSelector) }
231+
return target.responds(to: aSelector)
232+
}
233+
234+
override func forwardingTarget(for aSelector: Selector!) -> Any? {
235+
return target
236+
}
237+
}

Example/Example/Base.lproj/Main.storyboard

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13529" 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="14113" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
33
<device id="retina4_7" orientation="portrait">
44
<adaptation id="fullscreen"/>
55
</device>
66
<dependencies>
77
<deployment identifier="iOS"/>
8-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13527"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
99
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
1010
</dependencies>
1111
<scenes>
@@ -30,6 +30,26 @@
3030
<size key="footerReferenceSize" width="0.0" height="0.0"/>
3131
<inset key="sectionInset" minX="0.0" minY="0.0" maxX="0.0" maxY="0.0"/>
3232
</collectionViewFlowLayout>
33+
<view key="backgroundView" contentMode="scaleToFill" id="Tag-wA-w0f" userLabel="Colored Page Control Fresno" customClass="CHIPageControlFresno" customModule="CHIPageControl">
34+
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
35+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
36+
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
37+
<color key="tintColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
38+
<userDefinedRuntimeAttributes>
39+
<userDefinedRuntimeAttribute type="number" keyPath="numberOfPages">
40+
<integer key="value" value="4"/>
41+
</userDefinedRuntimeAttribute>
42+
<userDefinedRuntimeAttribute type="number" keyPath="progress">
43+
<real key="value" value="0.0"/>
44+
</userDefinedRuntimeAttribute>
45+
<userDefinedRuntimeAttribute type="number" keyPath="padding">
46+
<real key="value" value="5"/>
47+
</userDefinedRuntimeAttribute>
48+
<userDefinedRuntimeAttribute type="number" keyPath="radius">
49+
<real key="value" value="4"/>
50+
</userDefinedRuntimeAttribute>
51+
</userDefinedRuntimeAttributes>
52+
</view>
3353
<cells>
3454
<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" reuseIdentifier="aCell" id="chJ-Fk-SUc">
3555
<rect key="frame" x="0.0" y="1" width="372" height="665"/>
@@ -46,7 +66,7 @@
4666
<outlet property="delegate" destination="BYZ-38-t0r" id="FDZ-02-1k7"/>
4767
</connections>
4868
</collectionView>
49-
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="deJ-g4-VW8" userLabel="Colored Page Control Fresno" customClass="CHIPageControlFresno" customModule="CHIPageControl">
69+
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="deJ-g4-VW8" userLabel="Colored Page Control Fresno" customClass="CHIPageControlJalapeno" customModule="CHIPageControl">
5070
<rect key="frame" x="164" y="457" width="47" height="8"/>
5171
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
5272
<color key="tintColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
@@ -65,6 +85,26 @@
6585
</userDefinedRuntimeAttribute>
6686
</userDefinedRuntimeAttributes>
6787
</view>
88+
<view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="H9V-p9-Xqb" userLabel="Vertical Page Control Jalapeno" customClass="CHIPageControlJalapeno" customModule="CHIPageControl">
89+
<rect key="frame" x="20" y="70" width="47" height="8"/>
90+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
91+
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
92+
<color key="tintColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
93+
<userDefinedRuntimeAttributes>
94+
<userDefinedRuntimeAttribute type="number" keyPath="numberOfPages">
95+
<integer key="value" value="4"/>
96+
</userDefinedRuntimeAttribute>
97+
<userDefinedRuntimeAttribute type="number" keyPath="progress">
98+
<real key="value" value="0.0"/>
99+
</userDefinedRuntimeAttribute>
100+
<userDefinedRuntimeAttribute type="number" keyPath="padding">
101+
<real key="value" value="5"/>
102+
</userDefinedRuntimeAttribute>
103+
<userDefinedRuntimeAttribute type="number" keyPath="radius">
104+
<real key="value" value="4"/>
105+
</userDefinedRuntimeAttribute>
106+
</userDefinedRuntimeAttributes>
107+
</view>
68108
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="NOY-UP-9br" userLabel="Colored Page Control Jalapeno" customClass="CHIPageControlJalapeno" customModule="CHIPageControl">
69109
<rect key="frame" x="164" y="477" width="47" height="8"/>
70110
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
@@ -290,6 +330,7 @@
290330
</constraints>
291331
</view>
292332
<connections>
333+
<outlet property="verticalPageControl" destination="H9V-p9-Xqb" id="w02-92-gvK"/>
293334
<outletCollection property="pageControls" destination="nqO-qI-pML" collectionClass="NSMutableArray" id="DKS-zI-GEh"/>
294335
<outletCollection property="pageControls" destination="Nud-cF-PvK" collectionClass="NSMutableArray" id="Gcb-T0-nZS"/>
295336
<outletCollection property="pageControls" destination="nFD-kM-D2z" collectionClass="NSMutableArray" id="JhG-pl-3Jb"/>
@@ -300,6 +341,7 @@
300341
<outletCollection property="pageControls" destination="b83-NZ-aYL" collectionClass="NSMutableArray" id="qGs-iu-W7c"/>
301342
<outletCollection property="coloredPageControls" destination="NOY-UP-9br" collectionClass="NSMutableArray" id="Ux7-3J-El6"/>
302343
<outletCollection property="coloredPageControls" destination="deJ-g4-VW8" collectionClass="NSMutableArray" id="Gbd-E3-MhS"/>
344+
<outletCollection property="pageControls" destination="H9V-p9-Xqb" collectionClass="NSMutableArray" id="Oz7-1J-Xyo"/>
303345
</connections>
304346
</viewController>
305347
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>

Example/Example/ViewController.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ class ViewController: UIViewController {
1414
internal let numberOfPages = 4
1515
@IBOutlet var pageControls: [CHIBasePageControl]!
1616
@IBOutlet var coloredPageControls: [CHIBasePageControl]!
17-
17+
@IBOutlet weak var verticalPageControl: CHIPageControlJalapeno!
18+
1819
override func viewDidLoad() {
1920
super.viewDidLoad()
2021
self.pageControls.forEach { (control) in
@@ -29,6 +30,9 @@ class ViewController: UIViewController {
2930

3031
// You can also initialize the tintColors with an array
3132
coloredPageControls.last?.tintColors = [randomColor(), randomColor(), randomColor(), randomColor()]
33+
34+
//you can display page control vertical
35+
self.verticalPageControl.transform = self.pageControls.last!.transform.rotated(by: .pi/2)
3236
}
3337

3438
func randomColor() -> UIColor{

0 commit comments

Comments
 (0)