響應(yīng)者鏈(面試常問)和手勢(只是一些方法)
一皂贩、事件傳遞
1栖榨、從事件發(fā)生到其處理的對象,傳遞要經(jīng)過特殊的一段過程先紫,當(dāng)用戶點擊設(shè)備屏幕時治泥,iOS捕捉到一系列的觸摸,將其打包到UIEvent對象并放置到應(yīng)用程序活動事件隊列中遮精。
2居夹、UIApplication對象從事件隊列中取出最前面的事件并將其分發(fā),通常本冲,其將事件發(fā)送給應(yīng)用程序的主窗口-UIWindow實例准脂,再由窗口對象發(fā)送事件給第一響應(yīng)者處理,一般通過touchesBegan方法獲取該事件
3檬洞、具體過程
1)先將事件對象由上往下傳遞(由父控件傳遞給子控件)狸膏,找到最合適的控件來處理這個事件
2)調(diào)用最合適控件的touches方法
3)如果調(diào)用了[super touches...]方法,就會將事件順著"響應(yīng)者鏈條"往上傳遞添怔,傳遞給上一個響應(yīng)者
4)接著就會調(diào)用上一個響應(yīng)者的touches...方法
二湾戳、響應(yīng)者鏈
1、基本概念
響應(yīng)者對象是一個能接受并處理事件的對象广料,UIResponser是所有響應(yīng)者對象的基類砾脑,該基類定義了一系列編程接口,不但為事件處理進(jìn)行服務(wù)而且還提供了通用的響應(yīng)行為處理艾杏,UIApplication韧衣、UIView(UIWindow)、UIViewController都直接或間接的繼承自UIResponser,所有的這些類的實例都是響應(yīng)者對象
響應(yīng)者鏈表示一系列的響應(yīng)者對象,事件被交由第一響應(yīng)者對象處理畅铭,如果第一響應(yīng)者不處理氏淑,事件被沿著響應(yīng)者鏈向上傳遞,交給下一個響應(yīng)者(nextresponder)
2硕噩、事件響應(yīng)者鏈傳遞的過程(從下往上找)
1假残、當(dāng)用戶與試圖交互時,會將消息傳遞給試圖控制器榴徐,如果不存在控制器守问,傳遞給父試圖
2、如果不處理該消息坑资,則繼續(xù)將消息向上傳遞耗帕,如果最上層的試圖也不處理,將事件交給window對象袱贮,最后交由UIApplication實例仿便,如果不處理,丟棄事件
PS:傳遞方式
[self.nextResponder touchesBegan:touches withEvent:event];
3攒巍、通過響應(yīng)者鏈傳遞可以讓多個試圖響應(yīng)同一個手勢
四嗽仪、手勢識別器(在第五部分給大家具體的實例講解)
UIGestureRecognizer類,用于檢測柒莉、識別用戶使用設(shè)備時所用的手勢闻坚,他是一個抽象類,定義了所有手勢的基本行為兢孝,以下是UIGestureRecognizer子類窿凤,用與處理具體的用戶手勢行為
1、輕擊
UITapGestureRecognizer
常用屬性
//點擊次數(shù)
numberOfTapsRequired
//消除兩個手勢的影響
requireGestureRecognizerToFail
//手指的數(shù)量跨蟹,需將試圖的multipleTouchEnabled設(shè)為YES
numberOfTouchesRequired
2雳殊、輕掃
UISwipeGestureRecognizer
常用屬性
清掃方向,默認(rèn)是右
direction
3窗轩、長按
UILongPressGestureRecognizer
常用屬性
最小長按時間
minimumPressDuration
注意事項
如果不判斷會調(diào)用2次 按下2秒后自己調(diào)用一次 松開后又要調(diào)用一次
if (longPress.state == UIGestureRecognizerStateEnded) {
return;
}
NSLog(@"長按");
4夯秃、平移
UIPanGestureRecognizer
常用方法
獲取觸摸的位置
locationInView
注意事項
- (void)panAction:(UIPanGestureRecognizer *)gesture{
// 1.在view上面挪動的距離 //translationInView表示相對于起點挪動的位置是最新2點之間的間距
CGPoint translation = [gesture translationInView:gesture.view];
CGPoint center = gesture.view.center;
center.x += translation.x;
center.y += translation.y;
gesture.view.center = center;
// 2.清空移動的距離
[gesture setTranslation:CGPointZero inView:gesture.view];
}
5、捏合
UIPinchGestureRecognizer
注意事項:
每次調(diào)用過方法后痢艺,記得將scale置1仓洼,否則你的圖片在捏合一點便會變很大或很小
6、旋轉(zhuǎn)
UIRotationGestureRecognizer
注意事項:
每次調(diào)用過方法后堤舒,記得將rotation置0色建,否則圖片旋轉(zhuǎn)不正常
7、手勢識別器的代理方法
/**
* 是否允許多個手勢識別器同時有效
* Simultaneously : 同時地
*/
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
8植酥、重要的事情說三遍
實現(xiàn)旋轉(zhuǎn)、縮放、平移的時候一定要清零
實現(xiàn)旋轉(zhuǎn)友驮、縮放漂羊、平移的時候一定要清零
實現(xiàn)旋轉(zhuǎn)、縮放卸留、平移的時候一定要清零
五具體的實例講解
#import "ViewController.h"
@interface ViewController ()<UIGestureRecognizerDelegate>
{
UIImageView *imageView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
imageView = [[UIImageView alloc]initWithFrame:self.view.frame];
imageView.image = [UIImage imageNamed:@"minion"];
imageView.userInteractionEnabled = YES;
[self.view addSubview:imageView];
#pragma mark 1.輕擊
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
tapGesture.numberOfTapsRequired = 1;
[imageView addGestureRecognizer:tapGesture];//添加手勢
#pragma mark 2.雙擊
UITapGestureRecognizer *doubleGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleclick:)];
doubleGesture.numberOfTapsRequired = 2;
[tapGesture requireGestureRecognizerToFail:doubleGesture];
//消除兩個手勢的影響
[imageView addGestureRecognizer:doubleGesture];
#pragma mark 3.兩個手指同時點擊
UITapGestureRecognizer *doubleTouchAndClick = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTouchAndClick:)];
doubleTouchAndClick.numberOfTouchesRequired = 2;
doubleTouchAndClick.numberOfTapsRequired = 2;
[imageView addGestureRecognizer:doubleTouchAndClick];
#pragma mark 4.清掃(就一個屬性 direction)
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe)];
//設(shè)置清掃的對象
swipeGesture.direction = UISwipeGestureRecognizerDirectionDown;
//添加手勢
[imageView addGestureRecognizer:swipeGesture];
#pragma mark 5.長按
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressGesture:)];
//長按的一個屬性:長按最小時間
longPressGesture.minimumPressDuration = 2;
[imageView addGestureRecognizer:longPressGesture];
#pragma mark 6.平移
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)];
[imageView addGestureRecognizer:panGesture];
#pragma mark 7.捏合
UIPinchGestureRecognizer *pinGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinGesture:)];
pinGesture.delegate = self;
[imageView addGestureRecognizer:pinGesture];
#pragma mark 8.旋轉(zhuǎn)
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGesture:)];
rotationGesture.delegate = self;
[imageView addGestureRecognizer:rotationGesture];
}
// 1.輕擊
-(void)tap:(UITapGestureRecognizer *)gesture
{
NSLog(@"輕擊");
}
//2.雙擊
-(void)doubleclick:(UITapGestureRecognizer *)gesture
{
NSLog(@"雙擊");
}
//3.兩個手指同時點擊
-(void)doubleTouchAndClick:(UITapGestureRecognizer *)gesture
{
NSLog(@"同時雙擊");
}
//4.清掃
-(void)swipe
{
NSLog(@"清掃");
}
//5.長按
-(void)longPressGesture:(UILongPressGestureRecognizer *)gesture
{
if (gesture.state == UIGestureRecognizerStateEnded) {
return;
}
NSLog(@"長按");
}
//6.平移
-(void)panGesture:(UIPanGestureRecognizer *)gesture
{
// 1.在view上面挪動的距離 //translationInView表示相對于起點挪動的位置是最新2點之間的間距
CGPoint translation = [gesture translationInView:gesture.view];
CGPoint center = gesture.view.center;
center.x += translation.x;
center.y += translation.y;
gesture.view.center = center;
// 2.清空移動的距離
[gesture setTranslation:CGPointZero inView:gesture.view];
}
//7.捏合
-(void)pinGesture:(UIPinchGestureRecognizer *)gesture
{
imageView.transform = CGAffineTransformScale(imageView.transform, gesture.scale, gesture.scale);
NSLog(@"%f",gesture.scale);
gesture.scale = 1;
}
//8.旋轉(zhuǎn)
- (void)rotationGesture:(UIRotationGestureRecognizer *)gesture
{
imageView.transform = CGAffineTransformRotate(imageView.transform, gesture.rotation);
gesture.rotation = 0;
}
//9走越、手勢識別器的代理方法
/**
* 是否允許多個手勢識別器同時有效
* Simultaneously : 同時地
*/
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
@end