IOS Implementation Countdown

Source: Internet
Author: User

#import "ViewController.h"
#import "QSSDateHelper.h"
@interface Viewcontroller ()
@property (Nonatomic,strong) UIButton *btn;
@property (nonatomic,assign) BOOL Istouch;
@property (nonatomic,assign) Nsinteger time;
@property (nonatomic,assign) int year;
@property (nonatomic,assign) int mouth;
@property (nonatomic,assign) int day;
@property (nonatomic,assign) int hour;
@property (nonatomic,assign) int minute;
@property (nonatomic,assign) int second;
@property (nonatomic,assign) int mytouchsecond;
@property NSDate *touchdate;
@property Nscalendar *cal;
@property Nstimer *timer;
@end

@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
[Self initbtn];
Self.istouch=no;

}
-(void) dealloc{
[Self.timer invalidate];
Self.timer=nil;
self.time=0;
}
-(void) initbtn{
Self.btn=[[uibutton alloc]initwithframe:cgrectmake (100, 100, 100, 100)];
[Self.btn settitle:@ "Countdown" forstate:uicontrolstatenormal];
[Self.btn Setbackgroundcolor:[uicolor Lightgraycolor];
[Self.view ADDSUBVIEW:SELF.BTN];
[Self.btn addtarget:self Action: @selector (Releasetime) forcontrolevents:uicontroleventtouchupinside];
}
-(void) Timerfiremethod: (Nstimer *) timer{



NSDate *DD = [NSDate DateWithTimeIntervalSince1970:self.myTouchSecond];
int targetsecond = Self.mytouchsecond + 10*60;//target time

Converts the target's number of seconds to a fixed-format number of seconds

NSDate *DD = [NSDate Datewithtimeintervalsince1970:targetsecond];

unsigned int time2=nscalendarunityear| nscalendarunitmonth| nscalendarunitday| nscalendarunithour| nscalendarunitminute| Nscalendarunitsecond;

Nscalendar *cal=[nscalendar Currentcalendar];

Nsdatecomponents *t2=[cal components:time2 FROMDATE:DD];
================== Start the timing time =====================
unsigned int time=nscalendarunityear| nscalendarunitmonth| nscalendarunitday| nscalendarunithour| nscalendarunitminute| Nscalendarunitsecond;

Nsdatecomponents *t=[self.cal Components:time FromDate:self.touchDate]; Time to start timing
NSLog (@ "Start countdown time:%d days%d hours%d minutes%d seconds", (int) t.day, (int) t.hour, (int) t.minute, (int) t.second);
================== Target Time ==========================
NSLog (@ "Target time:%d days%d hours%d minutes%d seconds", (int) t2.day, (int) t2.hour, (int) t2.minute, (int) t2.second);
Target time
Nscalendar *calendar = [Nscalendar Currentcalendar];

Nsdatecomponents *components = [[Nsdatecomponents alloc] init];

[Components SetYear:t2.year];
[Components SetMonth:t2.month];
[Components SetDay:t2.day];
[Components SetHour:t2.hour];
[Components SetMinute:t2.minute];

NSLog (@ "Minute +++++%d", self.minute+10);
[Components SetSecond:self.second];

unsigned int unitflags = Nscalendarunityear | Nscalendarunitmonth | Nscalendarunitday | Nscalendarunithour | Nscalendarunitminute | Nscalendarunitsecond;

NSDate *firedate = [Calendar datefromcomponents:components];//target time


NSDate *nowhhhhh=[nsdate Date];



nsdatecomponents *d = [Calendar components:unitflags fromdate:nowhhhhh todate:dd options:0];//Calculate time difference


Uialertview *alert=[[uialertview alloc]initwithtitle:[nsstring stringwithformat:@ "%d minutes%d seconds", (int) [D minute], (int) [D Second]] message:@ "Countdown" Delegate:self cancelbuttontitle:@ "I Know" otherbuttontitles:nil, nil];

alert.delegate=self;

[Alert show];

}
-(void) releasetime{
self.time++;
Self.istouch=yes;
if (self.time==1) {
NSLog (@ "Start Countdown");
This method is called only once at the current time ...
NSDate *now=[nsdate Date];
Nscalendar *cal=[nscalendar Currentcalendar];
unsigned int time=nscalendarunityear| nscalendarunitmonth| nscalendarunitday| nscalendarunithour| nscalendarunitminute| Nscalendarunitsecond;
Nsdatecomponents *t=[cal Components:time Fromdate:now];
self.year= (int) [T year];
self.mouth= (int) [T month];
self.day= (int) [T day];
self.hour= (int) [t hour];
self.minute= (int) [t minute];
self.second= (int) [T second];




Nstimeinterval time2=[[nsdate Date] timeIntervalSince1970];
Self.mytouchsecond = time2;
NSLog (@ "date\n%d", Self.mytouchsecond);


Self.touchdate=now;
self.cal=cal;
}else{
Self.timer=[nstimer scheduledtimerwithtimeinterval:1.0 target:self selector: @selector (timerfiremethod:) UserInfo: Nil Repeats:no];
[Self.timer setfiredate:[nsdate Distantpast];
}

}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
}

@end

IOS Implementation Countdown

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.