如今的電容屏?xí)r代,也就是智能時(shí)代,基本上人人都是智能手機(jī)了,然而智能手機(jī)的屏幕需要手來(lái)點(diǎn),因此我們稱(chēng)這個(gè)點(diǎn)的動(dòng)作叫做手勢(shì),在手機(jī)專(zhuān)門(mén)有個(gè)類(lèi)來(lái)識(shí)別這些手勢(shì),今天就來(lái)介紹手勢(shì)識(shí)別器的用法。
//
// ViewController.m
// demo-UIGesture
//
// Created by mac on 16/8/1.
// Copyright ? 2016年 mac. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithRed:0.9 green:0.8 blue:0.8 alpha:1];
//注意:要開(kāi)啟用戶交互和多點(diǎn)控制
self.view.userInteractionEnabled = YES;
self.view.multipleTouchEnabled = YES;
/*
手勢(shì)識(shí)別器:
1.點(diǎn)擊
2.輕掃
3.捏合
4.旋轉(zhuǎn)
5.移動(dòng)
6.長(zhǎng)按
*/
/*
PS:因?yàn)橛行┓椒〞?huì)相互沖突,需要調(diào)用這個(gè)函數(shù)
[A requireGestureRecognizerToFail:B]函數(shù),它可以指定當(dāng)A手勢(shì)發(fā)生時(shí),即便A已經(jīng)滿足條件了,也不會(huì)立刻觸發(fā),會(huì)等到指定的手勢(shì)B確定失敗之后才觸發(fā)。
*/
//點(diǎn)擊手勢(shì) 1.單擊 2.雙擊
UITapGestureRecognizer *tapClick = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleclickAction:)];
[self.view addGestureRecognizer:tapClick];
UITapGestureRecognizer *tapdoubleClick = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleclickAction:)];
//雙擊屏幕調(diào)用的方法
tapdoubleClick.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:tapdoubleClick];
//雙擊失敗了就是單擊,進(jìn)行一個(gè)判別,不然每次雙擊的時(shí)候單擊都會(huì)調(diào)用一次
[tapClick requireGestureRecognizerToFail:tapdoubleClick];
//2.輕掃
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
//向上滑,默認(rèn)向右
swipe.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipe];
//3.捏合
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[self.view addGestureRecognizer:pinch];
//4.旋轉(zhuǎn)
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
[self.view addGestureRecognizer:rotation];
//5.移動(dòng)
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[self.view addGestureRecognizer:pan];
/*
注意:這里移動(dòng)會(huì)出現(xiàn)沖突
需要調(diào)用requeireGestureRecognizer方法
*/
[pan requireGestureRecognizerToFail:swipe];
//6.長(zhǎng)按
UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
[self.view addGestureRecognizer:longPress];
}
//單機(jī)屏幕事件
- (void)singleclickAction:(UITapGestureRecognizer *)sender{
NSLog(@"點(diǎn)擊了一下屏幕");
}
//雙擊屏幕事件
- (void)doubleclickAction:(UITapGestureRecognizer *)sender {
NSLog(@"雙擊了屏幕");
}
//輕掃事件
- (void)swipeAction:(UISwipeGestureRecognizer *)sender {
NSLog(@"輕掃屏幕");
}
//捏合
- (void)pinchAction:(UIPinchGestureRecognizer *)sender {
if(sender.state == UIGestureRecognizerStateChanged){
NSLog(@"一直捏合");
CGFloat scale = sender.scale;
self.view.transform = CGAffineTransformMakeScale(scale, scale);
}else if(sender.state == UIGestureRecognizerStateEnded){
//恢復(fù)到原來(lái)的大小
NSLog(@"旋轉(zhuǎn)結(jié)束");
self.view.transform = CGAffineTransformIdentity;
}
}
//旋轉(zhuǎn)
- (void)rotationAction:(UIRotationGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateChanged) {
CGFloat angle = sender.rotation;
self.view.transform = CGAffineTransformMakeRotation(angle);
NSLog(@"旋轉(zhuǎn)了屏幕");
}else if (sender.state == UIGestureRecognizerStateEnded){
NSLog(@"停止了旋轉(zhuǎn)");
self.view.transform = CGAffineTransformIdentity;
}
}
//平移
- (void)panAction:(UIPanGestureRecognizer *)sender {
//平移的坐標(biāo)
CGPoint moveDistance = [sender translationInView:self.view ];
//這是一個(gè)實(shí)時(shí)調(diào)用的方法
NSLog(@"平移了:%@",NSStringFromCGPoint(moveDistance));
}
//長(zhǎng)按
- (void)longPressAction:(UILongPressGestureRecognizer *)sender{
if (sender.state == UIGestureRecognizerStateBegan) {
NSLog(@"開(kāi)始長(zhǎng)按屏幕");
}else if(sender.state == UIGestureRecognizerStateEnded){
NSLog(@"結(jié)束長(zhǎng)按屏幕");
}
}
@end