//全部直接或者間接繼承自UIResponder(就是繼承自UIView和UIViewController的數組
//類),均可以去重寫UITouch的相關方法,去檢查開始觸摸,結束觸摸以及移動等事件app
#import "AppDelegate.h"ide
#import "FJViewController.h"spa
@interface AppDelegate ().net
@end 3d
@implementation AppDelegateorm
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {對象
// Override point for customization after application launch.繼承
_window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];事件
[_window setBackgroundColor:[UIColor whiteColor]];
//全部直接或者間接繼承自UIResponder(就是繼承自UIView和UIViewController的
//類),均可以去重寫UITouch的相關方法,去檢查開始觸摸,結束觸摸以及移動等事件
//=========================================
FJViewController *viewc = [[FJViewController alloc]init];
_window.rootViewController = viewc;
[_window makeKeyAndVisible];
return YES;
}
FJLabel.m
#import "FJLabel.h"
@implementation FJLabel
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.superview];
//label跟着移動,從新設置當前label的座標
self.center = point;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[UIView animateWithDuration:0.01 animations:^{
self.transform = CGAffineTransformMakeScale(0, 0) ;
}];
}
@end
FJViewController.m
#import "FJLabel.h"
#import "FJViewController.h"
@interface FJViewController (){
UITextField *field;
UIImageView *imageView;
}
@end
@implementation FJViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//設置背景顏色
self.view.backgroundColor = [UIColor redColor];
//UITouch的應用:點擊屏幕任何其餘的地方收起鍵盤
//重寫touchBegain方法在這個方法中收起鍵盤
field = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
field.backgroundColor = [UIColor whiteColor];
[self.view addSubview:field];
#pragma mark - UITouch在視圖控制器中拖動一個視圖
imageView = [[UIImageView alloc]
initWithFrame:CGRectMake(100, 250, 50, 50)];
imageView.backgroundColor = [UIColor cyanColor];
[self.view addSubview:imageView];
#pragma mark -UITouch移動一個label
FJLabel *lable= [[FJLabel alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
lable.text = @"hello world";
lable.userInteractionEnabled = YES;
[self.view addSubview:lable];
}
#pragma mark UITouch的基本知識
//開始觸摸當前視圖的時候調用這個方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//NSSet
//參數1:當前的觸摸對象集合:NSSet的功能和數組基本同樣用來同時存儲多個對象
//和數組的區別是:NSArray中的數組元素是有序的,可是NSSet中的元素是無序的
//參數2:事件
//1.拿到當前的觸摸對象
UITouch *touch = [touches anyObject];
//2.拿到當前觸摸的座標
CGPoint point = [touch locationInView:self.view];
NSLog(@"%@",NSStringFromCGPoint(point));
//3.拿到當前全部的觸摸點(可能同時拿到多個觸摸對象)
NSSet *set = event.allTouches;
//4.只能經過forin去遍歷(多個手指同時觸摸)
for (UITouch *tTouch in set) {
CGPoint point = [tTouch locationInView:self.view];
NSLog(@"%@",NSStringFromCGPoint(point));
}
NSLog(@"開始觸摸");
#pragma mark - UITouch的應用
//==================UITouch的應用:收起鍵盤=======================
[field resignFirstResponder];
//==================應用二:拖動視圖==============
imageView.center = point;
}
//觸摸結束調用方法
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"觸摸結束");
//拿到觸摸結束時得touch對象
UITouch *touch = [touches anyObject ];
//拿到觸摸結束的座標
CGPoint point = [touch locationInView:self.view];
NSLog(@"%@",NSStringFromCGPoint(point));
}
//這方法會在手指按住不放的過程當中時時調用
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"移動");
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
//==================應用二:拖動視圖==============
imageView.center = point;
NSLog(@"%@",NSStringFromCGPoint(point));
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end