我們?cè)陂_發(fā)過(guò)程中為了提高用戶體驗(yàn),以及實(shí)現(xiàn)更好的用戶交互往往會(huì)使用UiGestureRecognizer(手勢(shì)識(shí)別器)這一類
其實(shí)UiGestureRecognizer這一類并不復(fù)雜,簡(jiǎn)單來(lái)說(shuō)這就是一個(gè)特殊的觸摸事件,UiGestureRecognizer是一個(gè)父類,而實(shí)際操作中我們要使用它的子類,較為常用的有以下幾種:
UITapGestureRecognizer | 輕拍手勢(shì) |
UISwipeGestureRecognizer | 輕掃手勢(shì) |
UILongPressGestureRecognizer | 長(zhǎng)按手勢(shì) |
UIPanGestureRecognizer | 平移手勢(shì) |
UIPinchGestureRecognizer | 捏合(縮放)手勢(shì) |
UIRotationGestureRecognizer | 旋轉(zhuǎn)手勢(shì) |
UIScreenEdgePanGestureRecognizer | 屏幕邊緣平移 |
UITapGestureRecognizer(輕拍手勢(shì))
手勢(shì)的用法大同小異,我們以UITapGestureRecognizer(輕拍手勢(shì))為例
首先我們?cè)趘iewDidLoad中建立一個(gè)測(cè)試View
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
_testView = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 314, 500)];
_testView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:_testView];
}
效果如圖:
創(chuàng)建testView
接下來(lái)我們需要在viewDidLoad方法中給這個(gè)view添加手勢(shì)
//創(chuàng)建手勢(shì) 使用initWithTarget:action:的方法創(chuàng)建
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapView:)];
//設(shè)置屬性
//tap 手勢(shì)一共兩個(gè)屬性嗡靡,一個(gè)是設(shè)置輕拍次數(shù)跺撼,一個(gè)是設(shè)置點(diǎn)擊手指?jìng)€(gè)數(shù)
//設(shè)置輕拍次數(shù)
tap.numberOfTapsRequired = 2;
//設(shè)置手指字?jǐn)?shù)
tap.numberOfTouchesRequired = 2;
//別忘了添加到testView上
[_testView addGestureRecognizer:tap];
實(shí)現(xiàn)觸發(fā)事件,此處觸發(fā)的事件是改變testView的顏色
#pragma mark --- UITapGestureRecognizer 輕拍手勢(shì)事件 ---
-(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];
}
實(shí)現(xiàn)的效果如下
點(diǎn)擊后顏色改變
UISwipeGestureRecognizer (輕掃手勢(shì))
我們?nèi)匀皇褂胻estView這一場(chǎng)景
//創(chuàng)建手勢(shì)
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeView:)];
//設(shè)置屬性讨彼,swipe也是有兩種屬性設(shè)置手指?jìng)€(gè)數(shù)及輕掃方向
swipe.numberOfTouchesRequired = 2;
//設(shè)置輕掃方向(默認(rèn)是從左往右)
//direction是一個(gè)枚舉值有四個(gè)選項(xiàng)财边,我們可以設(shè)置從左往右,從右往左点骑,從下往上以及從上往下
//設(shè)置輕掃方向(默認(rèn)是從左往右)
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
// [_redView addGestureRecognizer:swipe];
觸發(fā)的事件為:
#pragma mark swipe輕掃手勢(shì)事件
-(void)swipeView:(UISwipeGestureRecognizer *)sender{
_testView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
}
UILongPressGestureRecognizer(長(zhǎng)按手勢(shì))
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
//屬性設(shè)置
//最小長(zhǎng)按時(shí)間
longPress.minimumPressDuration = 2;
[_testView addGestureRecognizer:longPress];
觸發(fā)事件:
#pragma mark langPress 長(zhǎng)按手勢(shì)事件
-(void)longPress:(UILongPressGestureRecognizer *)sender{
//進(jìn)行判斷,在什么時(shí)候觸發(fā)事件
if (sender.state == UIGestureRecognizerStateBegan) {
NSLog(@"長(zhǎng)按狀態(tài)");
//改變testView顏色
_testView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
}
}
UIPanGestureRecognizer(平移手勢(shì))
創(chuàng)建手勢(shì)
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
[_testView addGestureRecognizer:pan];
觸發(fā)事件
#pragma mark pan 平移手勢(shì)事件
-(void)panView:(UIPanGestureRecognizer *)sender{
CGPoint point = [sender translationInView:_testView];
// sender.view.transform = CGAffineTransformMake(1, 0, 0, 1, point.x, point.y);
//平移一共兩種移動(dòng)方式
//第一種移動(dòng)方法:每次移動(dòng)都是從原來(lái)的位置移動(dòng)
// sender.view.transform = CGAffineTransformMakeTranslation(point.x, point.y);
//第二種移動(dòng)方式:以上次的位置為標(biāo)準(zhǔn)(移動(dòng)方式 第二次移動(dòng)加上第一次移動(dòng)量)
sender.view.transform = CGAffineTransformTranslate(sender.view.transform, point.x, point.y);
//增量置為o
[sender setTranslation:CGPointZero inView:sender.view];
_testView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
}
UIScreenEdgePanGestureRecognizer(屏幕邊緣平移手勢(shì))
創(chuàng)建手勢(shì)
UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screenEdgePanView:)];
//注意:,使用屏幕邊界平移手勢(shì),需要注意兩點(diǎn)
//1. 視圖位置(屏幕邊緣)
//2. 設(shè)置edges屬性
// 設(shè)置屏幕邊緣手勢(shì)支持方法
screenEdgePan.edges = UIRectEdgeLeft;
//屬性設(shè)置
[_testView addGestureRecognizer:screenEdgePan];
觸發(fā)事件
#pragma mark screenEdgePan 屏幕邊界平移事件
-(void)screenEdgePanView:(UIScreenEdgePanGestureRecognizer *)sender{
//計(jì)算偏移量
CGPoint point = [sender translationInView:_testView];
//進(jìn)行平移
sender.view.transform = CGAffineTransformMakeTranslation(point.x, point.y);
}
UIPinchGestureRecognizer(捏合手勢(shì))
創(chuàng)建手勢(shì)
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
[_testView addGestureRecognizer:pinch];
觸發(fā)事件
#pragma mark pinch 捏合手勢(shì)事件
-(void)pinchView:(UIPinchGestureRecognizer *)sender{
//scale 縮放比例
// sender.view.transform = CGAffineTransformMake(sender.scale, 0, 0, sender.scale, 0, 0);
//每次縮放以原來(lái)位置為標(biāo)準(zhǔn)
// sender.view.transform = CGAffineTransformMakeScale(sender.scale, sender.scale);
//每次縮放以上一次為標(biāo)準(zhǔn)
sender.view.transform = CGAffineTransformScale(sender.view.transform, sender.scale, sender.scale);
//重新設(shè)置縮放比例 1是正澈眩縮放.小于1時(shí)是縮小(無(wú)論何種操作都是縮小),大于1時(shí)是放大(無(wú)論何種操作都是放大)
sender.scale = 1;
}
UIRotationGestureRecognizer(旋轉(zhuǎn)手勢(shì))
創(chuàng)建手勢(shì)
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationView:)];
[_testView addGestureRecognizer:rotation];
觸發(fā)事件
-(void)rotationView:(UIRotationGestureRecognizer *)sender{
// sender.view.transform = CGAffineTransformMake(cos(M_PI_4), sin(M_PI_4), -sin(M_PI_4), cos(M_PI_4), 0, 0);
//捏合手勢(shì)兩種改變方式
//以原來(lái)的位置為標(biāo)準(zhǔn)
// sender.view.transform = CGAffineTransformMakeRotation(sender.rotation);//rotation 是旋轉(zhuǎn)角度
//兩個(gè)參數(shù),以上位置為標(biāo)準(zhǔn)
sender.view.transform = CGAffineTransformRotate(sender.view.transform, sender.rotation);
//消除增量
sender.rotation = 0.0;
}
合理的運(yùn)用手勢(shì)會(huì)使我們的app擁有更好的體驗(yàn),但是在使用手勢(shì)的同時(shí)也要注意避免手勢(shì)和手勢(shì)間黑滴,以及手勢(shì)和其他控件間的沖突憨募。