Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the countdown to flash sale activity by iOS

Shulou Source: shulou.com Published: 2022-06-03 09:36:08 10月04日 Update

This article mainly introduces iOS how to achieve flash sale activity countdown, the article is very detailed, has a certain reference value, interested friends must read it!

IOS about large-scale website rush to buy, from the end of the event, the remaining time countdown implementation code, the code is relatively simple, we according to the needs of the appropriate addition, modification and deletion of the code

1. Define 4 Label to receive the countdown:

@ property (weak, nonatomic) IBOutlet UILabel * dayLabel;@property (weak, nonatomic) IBOutlet UILabel * hourLabel;@property (weak, nonatomic) IBOutlet UILabel * minuteLabel;@property (weak, nonatomic) IBOutlet UILabel * secondLabel

two。 Implement the method in the implementation file:

/ / time stamp is converted to date format (time stamp in milliseconds)-(NSString *) timeWithTimeIntervalString: (NSString *) timeString {/ / format time NSDateFormatter* formatter = [[NSDateFormatter alloc] init]; formatter.timeZone = [NSTimeZone timeZoneWithName:@ "shanghai"]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@ "yyyy-MM-dd HH:mm:ss"] / / the millisecond value is converted to NSDate* date = [NSDate dateWithTimeIntervalSince1970: [timeString doubleValue] / 1000.0]; NSString* dateString = [formatter stringFromDate:date]; NSLog (@ "time = =% @", dateString); return dateString;}-(void) downSecondHandle: (NSString*) aTimeString {NSDateFormatter * dateFormatter= [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@ "yyyy-MM-dd HH:mm:ss"]; NSDate* endDate = [dateFormatter dateFromString: [self timeWithTimeIntervalString:aTimeString]] / / end time NSDate * endDate_tomorrow = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate: ([endDate timeIntervalSinceReferenceDate])]; NSDate * startDate = [NSDate date]; NSString* dateString = [dateFormatter stringFromDate:startDate]; NSLog (@ "time now =% @", dateString); NSTimeInterval timeInterval = [endDate_tomorrow timeIntervalSinceDate:startDate]; if (_ timer==nil) {_ _ block int timeout = timeInterval / / countdown time if (timeouttiming queue 0) {dispatch_queue_t queue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); _ timer = dispatch_source_create (DISPATCH_SOURCE_TYPE_TIMER, 0,0Powerqueue); dispatch_source_set_timer (_ timer,dispatch_walltime (NULL, 0), 1.0*NSEC_PER_SEC, 0); / / execute dispatch_source_set_event_handler (_ timer, ^ {if (timeout) per second

Tags: Time countdown activity code content format article appropriate value interest guy partner file method date more knowledge website industry information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Huawei vpn macOS MySQL