Open judaco opened 7 years ago
//
// ViewController.m
// GCD (Grand Central Dispatch)
//
// Created by hackeru on 17/05/2017.
// Copyright © 2017 juda. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
{
//this is a pointer, except the garbage collector, this is a weak pointer, and then the GC can demolish the object
__weak UILabel * label;
}
- (void)viewDidLoad {
[super viewDidLoad];
label = [[UILabel alloc] initWithFrame:CGRectMake(30, 30, 100, 30)];
[self.view addSubview:label];
//execute in the global (async) thread
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
int x = 0;
for (int i =0; i < 100000; i++) {
for (int j = 0; j < 20000; j++) {
if( i%2 ){
x++;
}else{
x--;
}
}
}
//NSLog(@"I am done!");
//an async thread, prohibited!!!
//label.text = @"I am done.";
//execute in the main thread
dispatch_async(dispatch_get_main_queue(), ^{
label.text = @"I am done";
});
});
NSLog(@"done viewDidLoad");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// ViewController.m
// Bouncing Circles (Exercise)
//
// Created by hackeru on 17/05/2017.
// Copyright © 2017 juda. All rights reserved.
//
#import "ViewController.h"
const CGFloat side = 50;
@interface ViewController ()
@end
@implementation ViewController
{
UIImage * imgBird;
UIImageView * imgView;
CGFloat x, y;
CGFloat velocityX, velocityY;
}
- (void)viewDidLoad {
[super viewDidLoad];
imgBird = [UIImage imageNamed:@"imgBird"];
imgView.image = imgBird;
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, side*2, side*2)];
imgView.contentMode = UIViewContentModeScaleToFill;
[self.view addSubview:imgView];
x = 29;
y = 30;
velocityX = 2.15;
velocityY = 1.25;
[self draw];
}
-(void)draw{
x += velocityX;
y += velocityY;
if (x + side >= self.view.frame.size.width) {
velocityX *= -1;
x = self.view.frame.size.width - side;
}
if (x - side <= 0) {
velocityX *= -1;
x = side;
}
if (y + side >= self.view.frame.size.height) {
velocityY *= -1;
y = self.view.frame.size.height - side;
}
if (y - side <= 0) {
velocityY *= -1;
y = side;
}
imgView.center = CGPointMake(x, y);
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC / 100);
dispatch_after(popTime, dispatch_get_main_queue(), ^{
[self draw];
});
}
-(BOOL)prefersStatusBarHidden{
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// ViewController.m
// NSTimer
//
// Created by hackeru on 17/05/2017.
// Copyright © 2017 juda. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
{
NSTimer * timer;
}
- (void)viewDidLoad {
[super viewDidLoad];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:NSSelectorFromString(@"tick:") userInfo:nil repeats:YES];
//[timer invalidate];
//timer = nil;
}
-(void)tick:(NSTimer *) sender{
NSLog(@"tick");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Running in Background StoryBoard GPS and Maps Gesture Recognizer Networking - HTTP Request Media - Audio, Video Telephony Working with Files and Directories Camera Notification Database - CoreData - SQLite CoreMotion - Gyroscope