Open judaco opened 7 years ago
//
// ViewController.m
// Gestures - Swipe
//
// Created by hackeru on 28/05/2017.
// Copyright © 2017 juda. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
{
UISwipeGestureRecognizer * swipeRecognizer;
}
- (void)viewDidLoad {
[super viewDidLoad];
swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:NSSelectorFromString(@"handleSwipes:")];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
swipeRecognizer.numberOfTouchesRequired = 2;
[self.view addGestureRecognizer:swipeRecognizer];
}
-(void)handleSwipes:(UISwipeGestureRecognizer *)sender{
NSLog(@"swipe left");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// ViewController.m
// Gestures - Rotation
//
// Created by hackeru on 28/05/2017.
// Copyright © 2017 juda. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
{
UILabel * label;
UIRotationGestureRecognizer * rotationRecognizer;
CGFloat angle;
}
- (void)viewDidLoad {
[super viewDidLoad];
label = [[UILabel alloc] init];
label.text = @"Juda is here";
[label sizeToFit];
label.center = self.view.center;
[self.view addSubview:label];
rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:NSSelectorFromString(@"handleRotations:")];
[self.view addGestureRecognizer:rotationRecognizer];
}
-(void)handleRotations:(UIRotationGestureRecognizer*)sender{
label.transform = CGAffineTransformMakeRotation(angle + sender.rotation);
if (sender.state == UIGestureRecognizerStateEnded) {
angle += sender.rotation;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// ViewController.m
// Gestures - Pan (drag)
//
// Created by hackeru on 28/05/2017.
// Copyright © 2017 juda. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
{
UILabel * label;
UIPanGestureRecognizer * panRecognizer;
}
- (void)viewDidLoad {
[super viewDidLoad];
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 250, 60)];
label.text = @"Juda is here";
label.backgroundColor = [UIColor greenColor];
label.textColor = [UIColor whiteColor];
label.center = self.view.center;
label.userInteractionEnabled = YES;
label.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label];
panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:NSSelectorFromString(@"handlePan:")];
[label addGestureRecognizer:panRecognizer];
panRecognizer.minimumNumberOfTouches = 1;
panRecognizer.maximumNumberOfTouches = 1;
}
-(void)handlePan:(UIPanGestureRecognizer *)sender{
if (sender.state != UIGestureRecognizerStateEnded && sender.state != UIGestureRecognizerStateFailed) {
CGPoint location = [sender locationInView:self.view];
label.center = location;//same as - sender.center.label = location
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// ViewController.m
// Gestures - Long Press
//
// Created by hackeru on 28/05/2017.
// Copyright © 2017 juda. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
{
UIView * blueBox;
UILongPressGestureRecognizer * longPressRecognizer;
}
- (void)viewDidLoad {
[super viewDidLoad];
blueBox = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
blueBox.backgroundColor = [UIColor redColor];
[self.view addSubview:blueBox];
longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:NSSelectorFromString(@"handleLongPress:")];
longPressRecognizer.numberOfTouchesRequired = 2;
longPressRecognizer.allowableMovement = 100;//move max 100 pixels
longPressRecognizer.minimumPressDuration = 1;//touch min 1 second
[self.view addGestureRecognizer:longPressRecognizer];
}
-(void)handleLongPress:(UILongPressGestureRecognizer*)sender{
NSLog(@"long press");
if (sender.numberOfTouches == 2) {
CGPoint point1 = [sender locationOfTouch:0 inView:self.view];
CGPoint point2 = [sender locationOfTouch:1 inView:self.view];
CGPoint middle = CGPointMake((point1.x + point2.x)/2, (point1.y + point2.y/2));
blueBox.center = middle;
}
}
-(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// ViewController.m
// Gestures - Tap (click)
//
// Created by hackeru on 28/05/2017.
// Copyright © 2017 juda. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
{
UITapGestureRecognizer * tapRecognizer;
UIImageView * imageView;
}
- (void)viewDidLoad {
[super viewDidLoad];
UIImage * image = [UIImage imageNamed:@"img1"];
imageView = [[UIImageView alloc] initWithImage:image];
imageView.center = self.view.center;
[self.view addSubview:imageView];
imageView.userInteractionEnabled = YES;
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:NSSelectorFromString(@"handleTap:")];
[imageView addGestureRecognizer:tapRecognizer];
}
-(void)handleTap:(UITapGestureRecognizer*)sender{
NSLog(@"tap");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
NSURLSession
for internet connection we need permission: info.plist - + - app transport security setting by arrow direction down --> under - Allow Arbitrary Loads, boolean = YES
//
// ViewController.m
// NSURLSession
//
// Created by hackeru on 28/05/2017.
// Copyright © 2017 juda. All rights reserved.
//
#import "ViewController.h"
@interface ViewController () <NSURLSessionDelegate>
@end
@implementation ViewController
{
NSURLSession * session;
}
- (void)viewDidLoad {
[super viewDidLoad];
//Define a configuration
NSURLSessionConfiguration * configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
configuration.timeoutIntervalForRequest = 15.0; // after 15 seconds if the server is not responding disconnect the request
session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL * url = [NSURL URLWithString:@"http://www.uefa.com"];
//bring data
NSURLSessionDataTask * task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//NSData is an array of bytes, these are the bytes of the response
if (error == nil) {
NSLog(@"data length: %li", data.length);
NSString * dataAsString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//NSLog(@"%i", [[NSThread currentThread] isMainThread]);
//runs on global queue
}else{
NSLog(@"un grande casino %@", [error description]);
}
[session finishTasksAndInvalidate];//close the connection. like connection.close
}];
[task resume];//starting the task itself
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//this is POST
NSURLSessionDownloadTask * task = [session downloadTaskWithURL:url completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
}];
complete the location changes