Skip to content

Commit 9478ccc

Browse files
author
Diogo Autilio
committed
Support countdown and timer
1 parent d15f5d3 commit 9478ccc

6 files changed

Lines changed: 43 additions & 25 deletions

File tree

SCLAlertView/SCLAlertView.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,9 @@ typedef NS_ENUM(NSInteger, SCLAlertViewBackground)
223223
/** Add Timer Display
224224
*
225225
* @param buttonIndex The index of the button to add the timer display to.
226+
* @param reverse Convert timer to countdown.
226227
*/
227-
- (void)addTimerToButtonIndex:(NSInteger)buttonIndex;
228+
- (void)addTimerToButtonIndex:(NSInteger)buttonIndex reverse:(BOOL)reverse;
228229

229230
/** Set SubTitle Height
230231
*

SCLAlertView/SCLAlertView.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,13 +646,14 @@ - (void)buttonTapped:(SCLButton *)btn
646646

647647
#pragma mark - Button Timer
648648

649-
- (void)addTimerToButtonIndex:(NSInteger)buttonIndex
649+
- (void)addTimerToButtonIndex:(NSInteger)buttonIndex reverse:(BOOL)reverse
650650
{
651651
buttonIndex = MAX(buttonIndex, 0);
652652
buttonIndex = MIN(buttonIndex, [_buttons count]);
653653

654654
buttonTimer = [[SCLTimerDisplay alloc] initWithOrigin:CGPointMake(5, 5) radius:13 lineWidth:4];
655655
buttonTimer.buttonIndex = buttonIndex;
656+
buttonTimer.reverse = reverse;
656657
}
657658

658659
#pragma mark - Show Alert

SCLAlertView/SCLTimerDisplay.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
}
2222

2323
@property CGFloat currentAngle;
24-
@property (nonatomic) UIColor *color;
2524
@property NSInteger buttonIndex;
25+
@property (nonatomic) UIColor *color;
26+
@property (nonatomic) BOOL reverse;
2627

2728
- (instancetype)initWithOrigin:(CGPoint)origin radius:(CGFloat)r;
2829
- (instancetype)initWithOrigin:(CGPoint)origin radius:(CGFloat)r lineWidth:(CGFloat)width;

SCLAlertView/SCLTimerDisplay.m

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ - (void)drawRect:(CGRect)rect
8888

8989
- (void)startTimerWithTimeLimit:(int)tl completed:(SCLActionBlock)completed
9090
{
91+
if (_reverse)
92+
{
93+
currentTime = tl;
94+
}
9195
timerLimit = tl;
9296
timer = [NSTimer scheduledTimerWithTimeInterval:TIMER_STEP target:self selector:@selector(updateTimerButton:) userInfo:nil repeats:YES];
9397
completedBlock = completed;
@@ -109,11 +113,22 @@ - (void)stopTimer
109113

110114
- (void)updateTimerButton:(NSTimer *)timer
111115
{
112-
currentTime += TIMER_STEP;
113-
currentAngle = (currentTime/timerLimit) * 360 + START_DEGREE_OFFSET;
114-
115-
if(currentAngle >= (360 + START_DEGREE_OFFSET)) {
116-
[self stopTimer];
116+
if (_reverse)
117+
{
118+
currentTime -= TIMER_STEP;
119+
currentAngle = (currentTime/timerLimit) * 360 + START_DEGREE_OFFSET;
120+
121+
if(currentTime <= 0) {
122+
[self stopTimer];
123+
}
124+
}
125+
else {
126+
currentTime += TIMER_STEP;
127+
currentAngle = (currentTime/timerLimit) * 360 + START_DEGREE_OFFSET;
128+
129+
if(currentAngle >= (360 + START_DEGREE_OFFSET)) {
130+
[self stopTimer];
131+
}
117132
}
118133
[self setNeedsDisplay];
119134
}

SCLAlertViewExample/Base.lproj/Storyboard.storyboard

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="7706" systemVersion="14E46" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="Ohq-jH-G4R">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8191" systemVersion="14F27" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="Ohq-jH-G4R">
33
<dependencies>
44
<deployment identifier="iOS"/>
5-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7703"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8154"/>
66
</dependencies>
77
<scenes>
88
<!--View Controller-->
@@ -18,7 +18,7 @@
1818
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
1919
<subviews>
2020
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="7sB-cQ-cdn">
21-
<rect key="frame" x="248" y="36" width="102" height="30"/>
21+
<rect key="frame" x="249" y="36" width="100" height="30"/>
2222
<state key="normal" title="Show Success">
2323
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
2424
</state>
@@ -27,7 +27,7 @@
2727
</connections>
2828
</button>
2929
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="4U1-31-wBg">
30-
<rect key="frame" x="261" y="74" width="77" height="30"/>
30+
<rect key="frame" x="262" y="74" width="76" height="30"/>
3131
<state key="normal" title="Show Error">
3232
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
3333
</state>
@@ -36,7 +36,7 @@
3636
</connections>
3737
</button>
3838
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="QYK-Po-EUn">
39-
<rect key="frame" x="257" y="112" width="88" height="30"/>
39+
<rect key="frame" x="258" y="112" width="87" height="30"/>
4040
<state key="normal" title="Show Notice">
4141
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
4242
</state>
@@ -45,7 +45,7 @@
4545
</connections>
4646
</button>
4747
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="c2J-Bd-hKP">
48-
<rect key="frame" x="251" y="150" width="100" height="30"/>
48+
<rect key="frame" x="252" y="150" width="99" height="30"/>
4949
<state key="normal" title="Show Warning">
5050
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
5151
</state>
@@ -54,7 +54,7 @@
5454
</connections>
5555
</button>
5656
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="cKc-Im-7MS">
57-
<rect key="frame" x="265" y="188" width="69" height="30"/>
57+
<rect key="frame" x="266" y="188" width="68" height="30"/>
5858
<state key="normal" title="Show Info">
5959
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
6060
</state>
@@ -63,7 +63,7 @@
6363
</connections>
6464
</button>
6565
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="mww-Jw-0ao">
66-
<rect key="frame" x="264" y="226" width="70" height="30"/>
66+
<rect key="frame" x="265" y="226" width="69" height="30"/>
6767
<state key="normal" title="Show Edit">
6868
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
6969
</state>
@@ -72,7 +72,7 @@
7272
</connections>
7373
</button>
7474
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="8ID-0w-GrU">
75-
<rect key="frame" x="240" y="264" width="121" height="30"/>
75+
<rect key="frame" x="241" y="264" width="121" height="30"/>
7676
<constraints>
7777
<constraint firstAttribute="width" constant="121" id="8I0-W2-Vao"/>
7878
</constraints>
@@ -84,7 +84,7 @@
8484
</connections>
8585
</button>
8686
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="w0L-ki-asc">
87-
<rect key="frame" x="229" y="302" width="143" height="30"/>
87+
<rect key="frame" x="230" y="302" width="143" height="30"/>
8888
<constraints>
8989
<constraint firstAttribute="width" constant="143" id="ClH-xh-nSa"/>
9090
</constraints>
@@ -96,7 +96,7 @@
9696
</connections>
9797
</button>
9898
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="DRS-IB-vIQ">
99-
<rect key="frame" x="228" y="340" width="143" height="30"/>
99+
<rect key="frame" x="229" y="340" width="143" height="30"/>
100100
<constraints>
101101
<constraint firstAttribute="width" constant="143" id="I9Y-ZI-qiU"/>
102102
</constraints>
@@ -108,7 +108,7 @@
108108
</connections>
109109
</button>
110110
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="XBG-LD-1eZ">
111-
<rect key="frame" x="229" y="378" width="143" height="30"/>
111+
<rect key="frame" x="230" y="378" width="143" height="30"/>
112112
<state key="normal" title="Show Validation">
113113
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
114114
</state>
@@ -117,7 +117,7 @@
117117
</connections>
118118
</button>
119119
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="fs5-sr-WWC">
120-
<rect key="frame" x="203" y="416" width="196" height="30"/>
120+
<rect key="frame" x="204" y="416" width="196" height="30"/>
121121
<constraints>
122122
<constraint firstAttribute="width" constant="196" id="hiy-05-eK2"/>
123123
</constraints>
@@ -133,11 +133,11 @@
133133
<constraints>
134134
<constraint firstAttribute="width" constant="196" id="l7J-oT-3CH"/>
135135
</constraints>
136-
<state key="normal" title="Show Countdown Timer">
136+
<state key="normal" title="Show Timer">
137137
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
138138
</state>
139139
<connections>
140-
<action selector="showCountdown:" destination="Ohq-jH-G4R" eventType="touchUpInside" id="m2j-hh-ctl"/>
140+
<action selector="showTimer:" destination="Ohq-jH-G4R" eventType="touchUpInside" id="TB1-ia-8d6"/>
141141
</connections>
142142
</button>
143143
</subviews>

SCLAlertViewExample/ViewController.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,10 @@ - (IBAction)showWaiting:(id)sender
246246
closeButtonTitle:nil duration:5.0f];
247247
}
248248

249-
- (IBAction)showCountdown:(id)sender
249+
- (IBAction)showTimer:(id)sender
250250
{
251251
SCLAlertView *alert = [[SCLAlertView alloc] init];
252-
[alert addTimerToButtonIndex:0];
252+
[alert addTimerToButtonIndex:0 reverse:YES];
253253
[alert showInfo:self title:@"Countdown Timer"
254254
subTitle:@"This alert has a duration set, and a countdown timer on the Dismiss button to show how long is left."
255255
closeButtonTitle:@"Dismiss" duration:10.0f];

0 commit comments

Comments
 (0)