judaco / iPhone

0 stars 0 forks source link

iPhone - Class 10 #6

Open judaco opened 7 years ago

judaco commented 7 years ago
//
//  ViewController.m
//  TableView Refresh
//
//  Created by hackeru on 17/05/2017.
//  Copyright © 2017 juda. All rights reserved.
//

#import "ViewController.h"

@implementation ViewController
{
    UITableView * tableView;
    NSMutableArray<NSDate*> * dates;
    UIRefreshControl * refreshControl;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    dates = [[NSMutableArray alloc] init];
    [dates addObject:[NSDate date]] ;

    tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [tableView registerClass:UITableViewCell.class forCellReuseIdentifier:@"identifier"];
    [self.view addSubview:tableView];
    tableView.delegate = self;
    tableView.dataSource = self;

    refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:NSSelectorFromString(@"handleRefresh:") forControlEvents:UIControlEventValueChanged];
    [tableView addSubview:refreshControl];

}

-(void)handleRefresh:(UIRefreshControl*)sender{

    dispatch_time_t poptime = (DISPATCH_TIME_NOW, NSEC_PER_SEC * 2);
    dispatch_after (poptime, dispatch_get_main_queue(), ^{
        [dates addObject:[NSDate date]];
        //[tableView reloadData];//refresh of all data
        [refreshControl endRefreshing];
        //refresh from a specific row, good for system runtime
        NSIndexPath * indexPathOfNewRow = [NSIndexPath indexPathForRow:dates.count - 1 inSection:0];
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:indexPathOfNewRow, nil] withRowAnimation:UITableViewRowAnimationAutomatic];

    });

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dates.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"identifier" forIndexPath:indexPath];
    NSDate * date = dates[indexPath.row];

    cell.textLabel.text = [date description];

    return cell;
}

-(BOOL)prefersStatusBarHidden{
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
judaco commented 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
judaco commented 7 years ago
//
//  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
judaco commented 7 years ago
//
//  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
judaco commented 7 years ago

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