Skip to content

Instantly share code, notes, and snippets.

@aug2uag
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aug2uag/17a751c7c486fc3ca268 to your computer and use it in GitHub Desktop.
Save aug2uag/17a751c7c486fc3ca268 to your computer and use it in GitHub Desktop.
Timer on NSDate
//
// ViewController.m
// gfhdsjkaryeuiw123
//
// Created by Rex Fatahi on 6/6/14.
// Copyright (c) 2014 aug2uag. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
{
NSMutableDictionary* questionsJeapordy;
NSMutableArray* questionsAllKeys;
UIView* containerView;
UILabel* containerViewLabel;
BOOL isActive;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
questionsJeapordy = [NSMutableDictionary dictionary];
questionsAllKeys = [NSMutableArray array];
containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[self.view addSubview:containerView];
containerViewLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 44, self.view.bounds.size.width - 20, 160)];
containerViewLabel.numberOfLines = 0;
containerViewLabel.lineBreakMode = NSLineBreakByWordWrapping;
[containerView addSubview:containerViewLabel];
isActive = NO;
// timer to check if 10 past the hour
[NSTimer scheduledTimerWithTimeInterval: 10.0
target: self
selector:@selector(checkEvery10Minutes)
userInfo: nil repeats:YES];
}
- (void)checkEvery10Minutes
{
if (isActive) return;
NSDate *date = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSMinuteCalendarUnit) fromDate:date];
NSInteger minute = [components minute];
if (minute % 10 == 0) {
isActive = YES;
[self makeQuestions];
}
}
- (void)makeQuestions
{
NSDictionary* dictionary1 = @{@"answer 1": @"question 1", @"answer 2": @"question 2", @"answer 3": @"question 3"};
NSArray* allKeys1 = [dictionary1 allKeys];
NSDictionary* dictionary2 = @{@"answer 1": @"question 1", @"answer 2": @"question 2", @"answer 3": @"question 3"};
NSArray* allKeys2 = [dictionary2 allKeys];
NSDictionary* dictionary3 = @{@"answer 1": @"question 1", @"answer 2": @"question 2", @"answer 3": @"question 3"};
NSArray* allKeys3 = [dictionary3 allKeys];
NSInteger randomInt = arc4random() % 3;
NSString* randString1 = [allKeys1 objectAtIndex:randomInt];
NSString* randString2 = [allKeys2 objectAtIndex:randomInt];
NSString* randString3 = [allKeys3 objectAtIndex:randomInt];
NSArray* arrayKeys = @[randString1, randString2, randString3];
NSString* value1 = [dictionary1 valueForKey:randString1];
NSString* value2 = [dictionary2 valueForKey:randString2];
NSString* value3 = [dictionary3 valueForKey:randString3];
NSArray* arrayValues = @[value1, value2, value3];
[questionsJeapordy removeAllObjects];
[questionsAllKeys removeAllObjects];
questionsJeapordy = [NSMutableDictionary dictionaryWithObjects:arrayValues forKeys:arrayKeys];
questionsAllKeys = (NSMutableArray *)[questionsJeapordy allKeys];
[self showQuestion1];
}
- (void)showQuestion1
{
NSString* key1 = [questionsAllKeys objectAtIndex:0];
NSString* value1 = [questionsJeapordy valueForKey:key1];
containerViewLabel.text = key1;
[self.view bringSubviewToFront:containerView];
[UIView animateWithDuration:0.3 animations:^{
containerView.frame = self.view.bounds;
int64_t delay = 60.0; // In seconds
dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, delay * NSEC_PER_SEC);
dispatch_after(time, dispatch_get_main_queue(), ^(void){
containerViewLabel.text = value1;
[self performSelector:@selector(showQuestion2)
withObject:nil
afterDelay:10.0f];
});
}];
}
- (void)showQuestion2
{
NSString* key2 = [questionsAllKeys objectAtIndex:1];
NSString* value2 = [questionsJeapordy valueForKey:key2];
containerViewLabel.text = key2;
int64_t delay = 60.0; // In seconds
dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, delay * NSEC_PER_SEC);
dispatch_after(time, dispatch_get_main_queue(), ^(void){
containerViewLabel.text = value2;
[self performSelector:@selector(showQuestion3)
withObject:nil
afterDelay:10.0f];
});
}
- (void)showQuestion3
{
NSString* key3 = [questionsAllKeys objectAtIndex:2];
NSString* value3 = [questionsJeapordy valueForKey:key3];
containerViewLabel.text = key3;
int64_t delay = 60.0; // In seconds
dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, delay * NSEC_PER_SEC);
dispatch_after(time, dispatch_get_main_queue(), ^(void){
containerViewLabel.text = value3;
int64_t delay2 = 15.0; // In seconds
dispatch_time_t time2 = dispatch_time(DISPATCH_TIME_NOW, delay2 * NSEC_PER_SEC);
dispatch_after(time2, dispatch_get_main_queue(), ^(void){
[UIView animateWithDuration:0.3 animations:^{
containerView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
isActive = NO;
}];
});
});
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment