在介紹之前,咱們首先要知道為什么要用手勢。比如我們需要添加一些觸屏敲茄、滑屏事件位谋,比如捏合圖片、雙擊屏幕掏父、下拉視圖等等,就會用到手勢
一损同、手勢(UIGestureRecognizer)的種類
其實(shí)UIGestureRecognizer這一類并不復(fù)雜,簡單來說這就是一個特殊的觸摸事件,UIGestureRecognizer是一個父類,而實(shí)際操作中我們要使用它的子類,鸟款、大概有以下幾種:
子類名稱 | 名稱 |
---|---|
UITapGestureRecognizer | 輕拍手勢 |
UISwipeGestureRecognizer | 輕掃手勢 |
UILongPressGestureRecognizer | 長按手勢 |
UIPanGestureRecognizer | 平移手勢 |
UIPinchGestureRecognizer | 捏合(縮放)手勢 |
UIRotationGestureRecognizer | 旋轉(zhuǎn)手勢 |
UIScreenEdgePanGestureRecognizer | 屏幕邊緣平移 |
UISharkGestureRecognizer | 晃動 |
二、創(chuàng)建手勢
手勢的創(chuàng)建的方法大同小異
- UITapGestureRecognizer(輕拍手勢)
//創(chuàng)建手勢 使用initWithTarget:action:的方法創(chuàng)建
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapView:)];
//設(shè)置屬性
//tap 手勢一共兩個屬性组哩,一個是設(shè)置輕拍次數(shù),一個是設(shè)置點(diǎn)擊手指個數(shù)
//設(shè)置輕拍次數(shù)
tap.numberOfTapsRequired = 2;
//設(shè)置手指字?jǐn)?shù)
tap.numberOfTouchesRequired = 2;
//別忘了添加到testView上
[self.view addGestureRecognizer:tap];
-(void)tapView:(UITapGestureRecognizer *)sender{
//設(shè)置輕拍事件改變testView的顏色
_testView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
}
- UISwipeGestureRecognizer (輕掃手勢)
//創(chuàng)建手勢
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeView:)];
//設(shè)置屬性伶贰,swipe也是有兩種屬性設(shè)置手指個數(shù)及輕掃方向
swipe.numberOfTouchesRequired = 2;
//設(shè)置輕掃方向(默認(rèn)是從左往右)
//direction是一個枚舉值有四個選項(xiàng)罐栈,我們可以設(shè)置從左往右,從右往左荠诬,從下往上以及從上往下
//設(shè)置輕掃方向(默認(rèn)是從左往右)
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipe];
- (void)swipeView:(UISwipeGestureRecognizer *)sender {
self.view.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
//可在這里獲取手勢位置
}
- UILongPressGestureRecognizer(長按手勢)
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
//屬性設(shè)置
//最小長按時間
longPress.minimumPressDuration = 2;
[self.view addGestureRecognizer:longPress];
-(void)longPress:(UILongPressGestureRecognizer *)sender{
//進(jìn)行判斷,在什么時候觸發(fā)事件
if (sender.state == UIGestureRecognizerStateBegan) {
NSLog(@"長按狀態(tài)");
//改變testView顏色
self.view.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
}
}
- UIPanGestureRecognizer(拖拽手勢)
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self.imageV addGestureRecognizer:pan];
當(dāng)手指拖動時調(diào)用
- (void)pan:(UIPanGestureRecognizer *)pan{
拖動手勢也有狀態(tài)
if(pan.state == UIGestureRecognizerStateBegan){
開始拖動
}else if(pan.state == UIGestureRecognizerStateChanged){
注意:獲取偏移量是相對于最原始的點(diǎn)
CGPoint transP = [pan translationInView:self.imageV];
self.imageV.transform = CGAffineTransformTranslate(self.imageV.transform, transP.x, transP.y);
復(fù)位,讓它相對于上一次.
[pan setTranslation:CGPointZero inView:self.imageV];
}else if(pan.state == UIGestureRecognizerStateEnded){
結(jié)束拖動
}
}
- UIPinchGestureRecognizer(捏合手勢柑贞,用于縮放)
- UIRotationGestureRecognizer(旋轉(zhuǎn)手勢)
//現(xiàn)在視圖上添加圖片 對圖片進(jìn)行操作
//添加捏合手勢
UIPinchGestureRecognizer *pinGes = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinGes:)];
[self.imageView addGestureRecognizer:pinGes];
旋轉(zhuǎn)時調(diào)用
- (void)pinGes:(UIPinchGestureRecognizer *)pin{
self.imageView.transform = CGAffineTransformScale(self.imageV.transform, pin.scale, pin.scale);
復(fù)位
[pin setScale:1];
}
添加旋轉(zhuǎn)手勢
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
設(shè)置代理使其能夠同時支持多個手勢(捏合的時候同時支持旋轉(zhuǎn))
rotation.delegate = self;
[self.imageView addGestureRecognizer:rotation];
當(dāng)手指開始旋轉(zhuǎn)時調(diào)用.
- (void)rotation:(UIRotationGestureRecognizer *)rotation{
self.imageView.transform = CGAffineTransformRotate(self.imageV.transform, rotation.rotation);
復(fù)位.
[rotation setRotation:0];
}
三钧嘶、手勢的一些代理函數(shù)(UIGestureRecognizerDelegate)
// 是否允許開始觸發(fā)手勢
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
//手指觸摸屏幕后回調(diào)的方法,返回NO則不再進(jìn)行手勢識別有决,方法觸發(fā)等
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
//開始進(jìn)行手勢識別時調(diào)用的方法,返回NO則結(jié)束书幕,不再觸發(fā)手勢
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
//是否支持多時候觸發(fā),返回YES迟隅,則可以多個手勢一起觸發(fā)方法励七,返回NO則為互斥
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
//下面這個兩個方法也是用來控制手勢的互斥執(zhí)行的
//這個方法返回YES,第一個手勢和第二個互斥時掠抬,第一個會失效
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);
//這個方法返回YES,第一個和第二個互斥時瞳步,第二個會失效
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);
四腰奋、手勢沖突處理
一般在含有UIScrollView或者UIScrollView的子類的視圖上添加自己的手勢就會有沖突单起;利用UIGestureRecognizerDelegate中的代理方法可以解決相應(yīng)的沖突劣坊;
因?yàn)樵赨IScrollView上有手勢,利用
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
//設(shè)置為YES就可以一直傳遞下去
同時手勢與事件觸摸有優(yōu)先級的關(guān)系测蘑,使用手勢時應(yīng)該多注意