Skip to content

Commit 6dab98d

Browse files
author
szl
committed
添加倒计时
1 parent af5eaec commit 6dab98d

File tree

5 files changed

+350
-11
lines changed

5 files changed

+350
-11
lines changed

STKitSwiftDemo/STKitSwiftDemo.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
/* Begin PBXBuildFile section */
1010
0410C62D22C9F2FE005C2382 /* STMoveButtonController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0410C62C22C9F2FE005C2382 /* STMoveButtonController.swift */; };
1111
0410C63122C9FD56005C2382 /* STGradientButtonController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0410C63022C9FD56005C2382 /* STGradientButtonController.swift */; };
12+
0466F64922CF531C0007D707 /* STTimerButtonController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0466F64822CF531C0007D707 /* STTimerButtonController.swift */; };
1213
04783DBC22CAE8870089AAB8 /* STPhoneTextFieldController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04783DBB22CAE8870089AAB8 /* STPhoneTextFieldController.swift */; };
1314
04783DC822CB01B70089AAB8 /* STGradientViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04783DC722CB01B70089AAB8 /* STGradientViewController.swift */; };
15+
0491946F22D2E88000709995 /* STTimerButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0491946E22D2E87F00709995 /* STTimerButton.swift */; };
16+
0491947122D2E89C00709995 /* STTimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0491947022D2E89C00709995 /* STTimer.swift */; };
1417
0492AF2322C9A6C000F73D31 /* STHUDController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0492AF2222C9A6C000F73D31 /* STHUDController.swift */; };
1518
0492AF3222C9C29700F73D31 /* STAlertController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0492AF3122C9C29700F73D31 /* STAlertController.swift */; };
1619
04EB3E0222CC2D270064DDF1 /* STSegmentedController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04EB3E0122CC2D270064DDF1 /* STSegmentedController.swift */; };
@@ -25,8 +28,11 @@
2528
/* Begin PBXFileReference section */
2629
0410C62C22C9F2FE005C2382 /* STMoveButtonController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = STMoveButtonController.swift; sourceTree = "<group>"; };
2730
0410C63022C9FD56005C2382 /* STGradientButtonController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = STGradientButtonController.swift; sourceTree = "<group>"; };
31+
0466F64822CF531C0007D707 /* STTimerButtonController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = STTimerButtonController.swift; sourceTree = "<group>"; };
2832
04783DBB22CAE8870089AAB8 /* STPhoneTextFieldController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = STPhoneTextFieldController.swift; sourceTree = "<group>"; };
2933
04783DC722CB01B70089AAB8 /* STGradientViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = STGradientViewController.swift; sourceTree = "<group>"; };
34+
0491946E22D2E87F00709995 /* STTimerButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = STTimerButton.swift; sourceTree = "<group>"; };
35+
0491947022D2E89C00709995 /* STTimer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = STTimer.swift; sourceTree = "<group>"; };
3036
0492AF2222C9A6C000F73D31 /* STHUDController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = STHUDController.swift; sourceTree = "<group>"; };
3137
0492AF3122C9C29700F73D31 /* STAlertController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = STAlertController.swift; sourceTree = "<group>"; };
3238
04EB3E0122CC2D270064DDF1 /* STSegmentedController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = STSegmentedController.swift; sourceTree = "<group>"; };
@@ -93,6 +99,9 @@
9399
04783DC722CB01B70089AAB8 /* STGradientViewController.swift */,
94100
04EB3E0122CC2D270064DDF1 /* STSegmentedController.swift */,
95101
04EB3E0522CC7E8B0064DDF1 /* STProgressController.swift */,
102+
0491946E22D2E87F00709995 /* STTimerButton.swift */,
103+
0491947022D2E89C00709995 /* STTimer.swift */,
104+
0466F64822CF531C0007D707 /* STTimerButtonController.swift */,
96105
6784886121B7CCFE0002FDF5 /* Main.storyboard */,
97106
6784886421B7CD000002FDF5 /* Assets.xcassets */,
98107
6784886621B7CD000002FDF5 /* LaunchScreen.storyboard */,
@@ -224,12 +233,15 @@
224233
buildActionMask = 2147483647;
225234
files = (
226235
0492AF3222C9C29700F73D31 /* STAlertController.swift in Sources */,
236+
0466F64922CF531C0007D707 /* STTimerButtonController.swift in Sources */,
227237
0410C62D22C9F2FE005C2382 /* STMoveButtonController.swift in Sources */,
228238
0410C63122C9FD56005C2382 /* STGradientButtonController.swift in Sources */,
239+
0491947122D2E89C00709995 /* STTimer.swift in Sources */,
229240
04EB3E0622CC7E8B0064DDF1 /* STProgressController.swift in Sources */,
230241
0492AF2322C9A6C000F73D31 /* STHUDController.swift in Sources */,
231242
04783DBC22CAE8870089AAB8 /* STPhoneTextFieldController.swift in Sources */,
232243
04783DC822CB01B70089AAB8 /* STGradientViewController.swift in Sources */,
244+
0491946F22D2E88000709995 /* STTimerButton.swift in Sources */,
233245
6784885E21B7CCFE0002FDF5 /* AppDelegate.swift in Sources */,
234246
04EB3E0222CC2D270064DDF1 /* STSegmentedController.swift in Sources */,
235247
);

STKitSwiftDemo/STKitSwiftDemo/Base.lproj/Main.storyboard

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
</viewController>
7575
<placeholder placeholderIdentifier="IBFirstResponder" id="b2R-Mj-5NN" userLabel="First Responder" sceneMemberID="firstResponder"/>
7676
</objects>
77-
<point key="canvasLocation" x="1851" y="-1329"/>
77+
<point key="canvasLocation" x="1016" y="-1896"/>
7878
</scene>
7979
<!--Alert Controller-->
8080
<scene sceneID="EYz-MP-K7n">
@@ -183,7 +183,7 @@
183183
</viewController>
184184
<placeholder placeholderIdentifier="IBFirstResponder" id="VEQ-vz-J0p" userLabel="First Responder" sceneMemberID="firstResponder"/>
185185
</objects>
186-
<point key="canvasLocation" x="2584" y="-1089"/>
186+
<point key="canvasLocation" x="1851" y="-1783"/>
187187
</scene>
188188
<!--Move Button Controller-->
189189
<scene sceneID="hnB-u7-Gfm">
@@ -198,7 +198,7 @@
198198
</viewController>
199199
<placeholder placeholderIdentifier="IBFirstResponder" id="hHS-xg-0WF" userLabel="First Responder" sceneMemberID="firstResponder"/>
200200
</objects>
201-
<point key="canvasLocation" x="3432" y="-652"/>
201+
<point key="canvasLocation" x="2583" y="-1594"/>
202202
</scene>
203203
<!--Gradient Button Controller-->
204204
<scene sceneID="ldy-ZC-jYZ">
@@ -213,7 +213,7 @@
213213
</viewController>
214214
<placeholder placeholderIdentifier="IBFirstResponder" id="MYg-vs-uwq" userLabel="First Responder" sceneMemberID="firstResponder"/>
215215
</objects>
216-
<point key="canvasLocation" x="4227.536231884058" y="-188.16964285714286"/>
216+
<point key="canvasLocation" x="3432" y="-1371"/>
217217
</scene>
218218
<!--Segmented Controller-->
219219
<scene sceneID="NQb-1K-Xw9">
@@ -228,7 +228,7 @@
228228
</viewController>
229229
<placeholder placeholderIdentifier="IBFirstResponder" id="MPk-zM-OcX" userLabel="First Responder" sceneMemberID="firstResponder"/>
230230
</objects>
231-
<point key="canvasLocation" x="1851" y="1108"/>
231+
<point key="canvasLocation" x="5051" y="76"/>
232232
</scene>
233233
<!--Gradient View Controller-->
234234
<scene sceneID="QaP-bS-ucs">
@@ -433,7 +433,7 @@
433433
</viewController>
434434
<placeholder placeholderIdentifier="IBFirstResponder" id="KfS-LZ-qTt" userLabel="First Responder" sceneMemberID="firstResponder"/>
435435
</objects>
436-
<point key="canvasLocation" x="2583" y="915"/>
436+
<point key="canvasLocation" x="5052" y="-633"/>
437437
</scene>
438438
<!--Phone Text Field Controller-->
439439
<scene sceneID="z0y-qY-gNu">
@@ -480,7 +480,7 @@
480480
</viewController>
481481
<placeholder placeholderIdentifier="IBFirstResponder" id="DvE-V7-S4p" userLabel="First Responder" sceneMemberID="firstResponder"/>
482482
</objects>
483-
<point key="canvasLocation" x="3431.884057971015" y="388.39285714285711"/>
483+
<point key="canvasLocation" x="4242" y="-1062"/>
484484
</scene>
485485
<!--Table View Controller-->
486486
<scene sceneID="sUp-LJ-0BD">
@@ -709,6 +709,33 @@
709709
<segue destination="tNc-yr-FvV" kind="show" id="5bg-T1-JVV"/>
710710
</connections>
711711
</tableViewCell>
712+
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" textLabel="Jvy-Dg-Kx7" detailTextLabel="yoh-rA-n5A" style="IBUITableViewCellStyleValue1" id="5qA-jI-eGM">
713+
<rect key="frame" x="0.0" y="387" width="414" height="44"/>
714+
<autoresizingMask key="autoresizingMask"/>
715+
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="5qA-jI-eGM" id="TEb-I4-Cw2">
716+
<rect key="frame" x="0.0" y="0.0" width="414" height="43.5"/>
717+
<autoresizingMask key="autoresizingMask"/>
718+
<subviews>
719+
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" insetsLayoutMarginsFromSafeArea="NO" text="STView" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="Jvy-Dg-Kx7">
720+
<rect key="frame" x="20" y="12" width="58" height="20.5"/>
721+
<autoresizingMask key="autoresizingMask"/>
722+
<fontDescription key="fontDescription" type="system" pointSize="17"/>
723+
<nil key="textColor"/>
724+
<nil key="highlightedColor"/>
725+
</label>
726+
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" insetsLayoutMarginsFromSafeArea="NO" text="STTimerButton" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="yoh-rA-n5A">
727+
<rect key="frame" x="278" y="12" width="116" height="20.5"/>
728+
<autoresizingMask key="autoresizingMask"/>
729+
<fontDescription key="fontDescription" type="system" pointSize="17"/>
730+
<nil key="textColor"/>
731+
<nil key="highlightedColor"/>
732+
</label>
733+
</subviews>
734+
</tableViewCellContentView>
735+
<connections>
736+
<segue destination="sMv-eT-R86" kind="show" id="wi1-Pj-gfT"/>
737+
</connections>
738+
</tableViewCell>
712739
</cells>
713740
</tableViewSection>
714741
</sections>
@@ -795,10 +822,10 @@
795822
</objects>
796823
<point key="canvasLocation" x="-1745" y="2402"/>
797824
</scene>
798-
<!--Segmented Controller-->
825+
<!--Timer Button Controller-->
799826
<scene sceneID="JVh-wy-9Ti">
800827
<objects>
801-
<viewController hidesBottomBarWhenPushed="YES" id="sMv-eT-R86" customClass="STSegmentedController" customModule="STKitSwiftDemo" customModuleProvider="target" sceneMemberID="viewController">
828+
<viewController hidesBottomBarWhenPushed="YES" id="sMv-eT-R86" customClass="STTimerButtonController" customModule="STKitSwiftDemo" customModuleProvider="target" sceneMemberID="viewController">
802829
<view key="view" contentMode="scaleToFill" id="xcx-ul-rdP">
803830
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
804831
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
@@ -808,7 +835,7 @@
808835
</viewController>
809836
<placeholder placeholderIdentifier="IBFirstResponder" id="7xo-as-uJo" userLabel="First Responder" sceneMemberID="firstResponder"/>
810837
</objects>
811-
<point key="canvasLocation" x="1851" y="1108"/>
838+
<point key="canvasLocation" x="3432" y="663"/>
812839
</scene>
813840
<!--Progress Controller-->
814841
<scene sceneID="NgA-Hb-is6">
@@ -823,7 +850,7 @@
823850
</viewController>
824851
<placeholder placeholderIdentifier="IBFirstResponder" id="SZy-zb-nK4" userLabel="First Responder" sceneMemberID="firstResponder"/>
825852
</objects>
826-
<point key="canvasLocation" x="1017" y="1246"/>
853+
<point key="canvasLocation" x="4241" y="343"/>
827854
</scene>
828855
</scenes>
829856
</document>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// STTimer.swift
3+
// STKitSwiftDemo
4+
//
5+
// Created by mac on 2019/7/8.
6+
// Copyright © 2019 沈兆良. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
class STTimer: NSObject {
12+
13+
private(set) var _timer: Timer?
14+
fileprivate weak var _aTarget: AnyObject?
15+
fileprivate var _aSelector: Selector?
16+
var fireDate: Date {
17+
get{
18+
return _timer?.fireDate
19+
}
20+
set{
21+
_timer?.fireDate = newValue
22+
}
23+
}
24+
25+
class func scheduledTimer(timeInterval ti: TimeInterval, target aTarget: AnyObject, selector aSelector: Selector, userInfo: Any?, repeats yesOrNo: Bool) -> STTimer {
26+
let timer = STTimer()
27+
28+
timer._aTarget = aTarget
29+
timer._aSelector = aSelector
30+
timer._timer = Timer.scheduledTimer(timeInterval: ti, target: timer, selector: #selector(STTimer.st_timerRun), userInfo: userInfo, repeats: yesOrNo)
31+
return timer
32+
}
33+
34+
func fire() {
35+
_timer.fire()
36+
}
37+
38+
func invalidate() {
39+
_timer.invalidate()
40+
}
41+
42+
@objc func st_timerRun() {
43+
_ = _aTarget.perform(_aSelector)
44+
}
45+
46+
deinit {
47+
print("计时器已销毁")
48+
}
49+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
//
2+
// STTimerButton.swift
3+
// STKitSwiftDemo
4+
//
5+
// Created by mac on 2019/7/4.
6+
// Copyright © 2019 沈兆良. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
12+
public enum STTimerButtonType: Int {
13+
case none = -1 // no state
14+
case start = 0
15+
case pause
16+
case resume
17+
case ongoing
18+
case finish
19+
case cancel
20+
}
21+
22+
public class STTimerButton: UIButton {
23+
// MARK: 1.lift cycle
24+
25+
// MARK: 2.private methods
26+
public func startCountDown(duration time: Int, processBlock block: ((_ button: STTimerButton, _ type: STTimerButtonType, _ timerNumer: Int) -> Void)? = nil){
27+
duration = time
28+
timerNumer = time
29+
processBlock = block
30+
type = .start
31+
if timer == nil {
32+
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(actionCountDown), userInfo: nil, repeats: true)
33+
}
34+
processBlock?(self, type, timerNumer)
35+
}
36+
37+
public func startTime(startTime time: Int = 0, processBlock block: ((_ button: STTimerButton, _ type: STTimerButtonType, _ timerNumer: Int) -> Void)? = nil){
38+
duration = time
39+
timerNumer = time
40+
processBlock = block
41+
type = .start
42+
if timer == nil {
43+
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(actionTime), userInfo: nil, repeats: true)
44+
}
45+
processBlock?(self, type, timerNumer)
46+
}
47+
48+
// MARK: 3.event response
49+
private func start() {
50+
type = .start
51+
if timer == nil {
52+
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(actionCountDown), userInfo: nil, repeats: true)
53+
}
54+
processBlock?(self, type, timerNumer)
55+
}
56+
57+
public func pause() {
58+
type = .pause
59+
timer?.fireDate = Date.distantFuture
60+
processBlock?(self, type, timerNumer)
61+
}
62+
63+
public func resume() {
64+
type = .resume
65+
timer?.fireDate = Date.distantPast
66+
processBlock?(self, type, timerNumer)
67+
}
68+
69+
public func cancel() {
70+
type = .cancel
71+
timer?.fireDate = Date.distantFuture
72+
timer?.invalidate()
73+
timer = nil
74+
processBlock?(self, type, timerNumer)
75+
}
76+
77+
public func finish() {
78+
type = .finish
79+
timer?.fireDate = Date.distantFuture
80+
timer?.invalidate()
81+
timer = nil
82+
processBlock?(self, type, timerNumer)
83+
}
84+
85+
private func ongoing() {
86+
type = .ongoing
87+
processBlock?(self, type, timerNumer)
88+
}
89+
90+
@objc public func actionCountDown() {
91+
if timerNumer > 1 {
92+
timerNumer -= 1
93+
ongoing()
94+
}else{
95+
timerNumer = duration
96+
finish()
97+
}
98+
}
99+
100+
@objc public func actionTime() {
101+
timerNumer += 1
102+
ongoing()
103+
}
104+
105+
// MARK: 4.interface
106+
private var processBlock: ((_ button: STTimerButton, _ type: STTimerButtonType, _ timerNumer: Int) -> Void)? = nil
107+
private var timer:Timer?
108+
private var timerNumer:Int = 0
109+
private var duration:Int = 10
110+
111+
public private(set) var type: STTimerButtonType = .none
112+
113+
// MARK: 5.getter
114+
115+
}

0 commit comments

Comments
 (0)