iOS開(kāi)發(fā)中的幾個(gè)手勢(shì)操作實(shí)例分享
手勢(shì)操作---識(shí)別單擊還是雙擊
在視圖上同時(shí)識(shí)別單擊手勢(shì)和雙擊手勢(shì)的問(wèn)題在于,當(dāng)檢測(cè)到一個(gè)單擊操作時(shí),無(wú)法確定是確實(shí)是一個(gè)單擊操作或者只是雙擊操作中的第一次點(diǎn)擊。解決這個(gè)問(wèn)題的方法就是:在檢測(cè)到單擊時(shí),需要等一段時(shí)間等待第二次點(diǎn)擊,如果沒(méi)有第二次點(diǎn)擊,則為單擊操作;如果有第二次點(diǎn)擊,則為雙擊操作。
檢測(cè)手勢(shì)有兩種方法,一種是定制子視圖,重寫(xiě)視圖從UIResponder類(lèi)中繼承來(lái)的事件處理方法,即touchesBegan:withEvent:等一系列方法來(lái)檢測(cè)手勢(shì);另一個(gè)方法是使用手勢(shì)識(shí)別器,即UIGestureRecognizer的各種具體子類(lèi)。
一.重寫(xiě)事件處理方法
- (id)init {
if ((self = [super init])) {
self.userInteractionEnabled = YES;
self.multipleTouchEnabled = YES;
// ...
}
return self;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self];
if (touch.tapCount == 1) {
[self performSelector:@selector(handleSingleTap:) withObject:[NSValue valueWithCGPoint:touchPoint] afterDelay:0.3];
}else if(touch.tapCount == 2)
{
[self handleDoubleTap:[NSValue valueWithCGPoint:touchPoint]];
}
}
-(void)handleSingleTap:(NSValue*)pointValue
{
CGPoint touchPoint = [pointValue CGPointValue];
//...
}
-(void)handleDoubleTap:(NSValue*)pointValue
{
CGPoint touchPoint = [pointValue CGPointValue];
//...
}
首先確認(rèn)定制視圖的userInteractionEnabled和multipleTouchEnabled屬性都為YES.
在touchesEnded:withEvent:方法中,如果是第一次觸摸結(jié)束,則cancelPreviousPerformRequestsWithTarget:方法不會(huì)起作用,因?yàn)閟elf未調(diào)度任何方法,此時(shí)tapCount為1,使用performSelector:withObject:afterDelay:調(diào)用單擊事件處理方法,在0.3s鐘后執(zhí)行。
[self performSelector:@selector(handleSingleTap:) withObject:[NSValue valueWithCGPoint:touchPoint] afterDelay:0.3];
如果這是一個(gè)單擊操作,則后面0.3鐘內(nèi)不會(huì)再有觸摸事件,則handleSingleTap:方法執(zhí)行,這樣識(shí)別出了單擊操作。
如果這是一個(gè)雙擊操作,則第二次點(diǎn)擊在0.3s內(nèi)觸發(fā),在第二次觸摸操作的touchesEnded:withEvent:方法中,cancelPreviousPerformRequestsWithTarget:首先會(huì)取消之前對(duì)handleSingleTap:方法的調(diào)度,使之不會(huì)執(zhí)行,然后在調(diào)用handleDoubleTap:方法處理雙擊操作。
二.使用Gesture Recognizer
使用Gesture Recognizer識(shí)別就會(huì)簡(jiǎn)單許多,只需添加兩個(gè)手勢(shì)識(shí)別器,分別檢測(cè)單擊和雙擊事件,設(shè)置必要的屬性即可。
- (id)init {
if ((self = [super init])) {
self.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTap:)];
singleTapGesture.numberOfTapsRequired = 1;
singleTapGesture.numberOfTouchesRequired = 1;
[self addGestureRecognizer:singleTapGesture];
UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDoubleTap:)];
doubleTapGesture.numberOfTapsRequired = 2;
doubleTapGesture.numberOfTouchesRequired = 1;
[self addGestureRecognizer:doubleTapGesture];
[singleTapGesture requireGestureRecognizerToFail:doubleTapGesture];
}
return self;
}
-(void)handleSingleTap:(UIGestureRecognizer *)sender{
CGPoint touchPoint = [sender locationInView:self];
//...
}
-(void)handleDoubleTap:(UIGestureRecognizer *)sender{
CGPoint touchPoint = [sender locationInView:self];
//...
}
唯一需要注意的是
[singleTapGesture requireGestureRecognizerToFail:doubleTapGesture];
這句話的意思時(shí),只有當(dāng)doubleTapGesture識(shí)別失敗的時(shí)候(即識(shí)別出這不是雙擊操作),singleTapGesture才能開(kāi)始識(shí)別,同我們一開(kāi)始講的是同一個(gè)問(wèn)題。
UIGestureRecognizer小應(yīng)用
1、輕拍手勢(shì):雙指、單擊,修改imageView的frame為(0,0,320,200)
2、長(zhǎng)按手指:?jiǎn)沃福薷膇mageView的alpha=0.5
3、實(shí)現(xiàn)平移、旋轉(zhuǎn)、捏合
4、輕掃:豎向輕掃實(shí)現(xiàn)圖:像隨機(jī)切換顯示;橫向輕掃實(shí)現(xiàn):圖像消失,隨機(jī)修改imageview的背景顏色
5、imageview每次只能添加一種手勢(shì)識(shí)別器。
#define _originalRect CGRectMake(10, 50, 300, 450)
#define _originalImageName @"h4.jpeg"
#import "HMTRootViewController.h"
@interface HMTRootViewController (){
UITapGestureRecognizer * _tapGesture;
UILongPressGestureRecognizer * _longGesture;
UIPanGestureRecognizer * _panGesture;
UIRotationGestureRecognizer * _rotateGesture;
UIPinchGestureRecognizer * _pinchGesture;
UISwipeGestureRecognizer * _verticalSwipeGesture;
UISwipeGestureRecognizer * _horizontanlSwipeGesture;
BOOL isTopDownOfRightLeft; // 垂直滑動(dòng)是YES,水平滑動(dòng)是NO
}
@property (nonatomic,retain) UIButton * button;
@property (nonatomic,retain) UIImageView * imageView;
@end
@implementation HMTRootViewController
- (void)dealloc{
RELEASE_SAFELY(_imageView);
RELEASE_SAFELY(_button);
[super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
isTopDownOfRightLeft = YES;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self createButtonView];
[self createImageView];
}
#pragma mark - 設(shè)置圖像
- (void)createImageView{
self.imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:_originalImageName]];
_imageView.frame = CGRectMake(10, 50, 300, 450);
_imageView.userInteractionEnabled = YES;
[self.view addSubview:_imageView];
[_imageView release];
}
#pragma mark - 設(shè)置手勢(shì)
#pragma mark 點(diǎn)擊手勢(shì)
- (void)createTapGestureRecognizer{
_tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(TapGestureRecognizer:)];
_tapGesture.numberOfTapsRequired = 1;
_tapGesture.numberOfTouchesRequired = 2;
[self.imageView addGestureRecognizer:_tapGesture];
[_tapGesture release];
}
- (void)TapGestureRecognizer:(UITapGestureRecognizer *)tapGesture{
self.imageView.frame = CGRectMake(0, 0, 320, 200);
NSLog(@"%@",NSStringFromCGRect(self.imageView.frame));
}
#pragma mark 長(zhǎng)按手勢(shì)
- (void)createLongGestureRecognizer{
_longGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longGestureRecognizer:)];
_longGesture.numberOfTouchesRequired = 1;
_longGesture.minimumPressDuration = 1.0;
[self.imageView addGestureRecognizer:_longGesture];
[_longGesture release];
}
- (void)longGestureRecognizer:(UILongPressGestureRecognizer *)longGesture{
self.imageView.alpha = 0.5;
NSLog(@"%s",__FUNCTION__);
}
#pragma mark 平移拖拽手勢(shì)
- (void)createPanGestureRecognizer{
_panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureRecognizer:)];
[self.imageView addGestureRecognizer:_panGesture];
[_panGesture release];
}
- (void)panGestureRecognizer:(UIPanGestureRecognizer *)panGesture{
NSLog(@"%s",__FUNCTION__);
CGPoint txty = [panGesture translationInView:self.view];
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, txty.x, txty.y);
[panGesture setTranslation:CGPointMake(0, 0) inView:self.view];
}
#pragma mark 旋轉(zhuǎn)手勢(shì)
- (void)createRotationGestureRecognizer{
_rotateGesture = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGestureRecognizer:)];
[self.imageView addGestureRecognizer:_rotateGesture];
[_rotateGesture release];
}
- (void)rotationGestureRecognizer:(UIRotationGestureRecognizer *)rotateGesture{
NSLog(@"%s",__FUNCTION__);
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotateGesture.rotation);
rotateGesture.rotation = 0;
}
#pragma mark 捏合縮放手勢(shì)
- (void)createPinchGestureRecognizer{
_pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGestureRecognizer:)];
[self.imageView addGestureRecognizer:_pinchGesture];
[_pinchGesture release];
}
- (void)pinchGestureRecognizer:(UIPinchGestureRecognizer *)pinchGesture{
NSLog(@"%s",__FUNCTION__);
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinchGesture.scale, pinchGesture.scale);
pinchGesture.scale = 1;
}
#pragma mark - 輕掃手勢(shì)
#pragma mark 上下 豎 垂直輕掃
- (void)createVerticalSwipeGestureRecognizer{
_verticalSwipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGestureRecognizer:)];
_verticalSwipeGesture.direction = UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown;
[self.imageView addGestureRecognizer:_verticalSwipeGesture];
[_verticalSwipeGesture release];
}
#pragma mark 水平 左右輕掃
- (void)createHorizontanlSwipeGesture{
_horizontanlSwipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGestureRecognizer:)];
_horizontanlSwipeGesture.direction = UISwipeGestureRecognizerDirectionLeft |UISwipeGestureRecognizerDirectionRight;
[self.imageView addGestureRecognizer:_horizontanlSwipeGesture];
}
- (void)swipeGestureRecognizer:(UISwipeGestureRecognizer *)swipeGesture{
NSLog(@"%s",__FUNCTION__);
if (swipeGesture.direction == (UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionDown)) {
self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"h%i.jpeg",arc4random()%7+1]];
;
}else if (swipeGesture.direction == (UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight)){
self.imageView.image = nil;
self.imageView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
}
}
#pragma mark - 設(shè)置按鈕
- (void)createButtonView{
NSArray * buttonArray = @[@"輕點(diǎn)",@"長(zhǎng)按",@"平移",@"旋轉(zhuǎn)",@"捏合",@"輕掃"];
for (int i = 0; i < [buttonArray count]; i++) {
self.button = [UIButton buttonWithType:UIButtonTypeSystem];
_button.frame = CGRectMake(10+50*i, 500, 50, 48);
[_button setTitle:[buttonArray objectAtIndex:i] forState:UIControlStateNormal];
[_button addTarget:self action:@selector(onClikButton:) forControlEvents:UIControlEventTouchUpInside];
_button.tag = i;
[self.view addSubview:_button];
}
}
- (void)onClikButton:(UIButton *)button{
[self resetImageView];
switch (button.tag) {
case 0:
[self createTapGestureRecognizer];
break;
case 1:
[self createLongGestureRecognizer];
break;
case 2:
[self createPanGestureRecognizer];
break;
case 3:
[self createRotationGestureRecognizer];
break;
case 4:
[self createPinchGestureRecognizer];
break;
case 5:
if (isTopDownOfRightLeft == YES) {
[self createVerticalSwipeGestureRecognizer];
isTopDownOfRightLeft = NO;
} else {
[self createHorizontanlSwipeGesture];
isTopDownOfRightLeft = YES;
}
break;
default:
break;
}
}
#pragma mark - 重置imageView
- (void)resetImageView
{
for (int i = 0; i < [self.imageView.gestureRecognizers count]; i++) {
[self.imageView removeGestureRecognizer:[self.imageView.gestureRecognizers objectAtIndex:i]];
}
self.imageView.alpha = 1.0;
self.imageView.transform = CGAffineTransformIdentity;
self.imageView.frame = _originalRect;
self.imageView.image = [UIImage imageNamed:_originalImageName];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
- IOS手勢(shì)操作(拖動(dòng)、捏合、旋轉(zhuǎn)、點(diǎn)按、長(zhǎng)按、輕掃、自定義)
- iOS實(shí)現(xiàn)手勢(shì)解鎖操作
- 使用Swift代碼實(shí)現(xiàn)iOS手勢(shì)解鎖、指紋解鎖實(shí)例詳解
- 基于JS實(shí)現(xiàn)Android,iOS一個(gè)手勢(shì)動(dòng)畫(huà)效果
- iOS仿郵箱大師的九宮格手勢(shì)密碼解鎖
- iOS開(kāi)發(fā)之觸摸事件以及手勢(shì)
- iOS手勢(shì)密碼的實(shí)現(xiàn)方法
- iOS輕點(diǎn)、觸摸和手勢(shì)代碼開(kāi)發(fā)
- iOS開(kāi)發(fā)之手勢(shì)識(shí)別
- iOS手勢(shì)的實(shí)現(xiàn)方法
相關(guān)文章
iOS常用算法之兩個(gè)有序數(shù)組合并(要求時(shí)間復(fù)雜度為0(n))
這篇文章主要介紹了iOS常用算法之兩個(gè)有序數(shù)組合并(要求時(shí)間復(fù)雜度為0(n)),實(shí)現(xiàn)思路是先將一個(gè)數(shù)組作為合并后的數(shù)組, 然后遍歷第二個(gè)數(shù)組的每項(xiàng)元素,需要的朋友可以參考下2019-07-07iOS學(xué)習(xí)筆記(十六)——詳解數(shù)據(jù)庫(kù)操作(使用FMDB)
這篇文章主要介紹了iOS學(xué)習(xí)筆記(十六)——詳解數(shù)據(jù)庫(kù)操作(使用FMDB),具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12MAC 系統(tǒng)安裝java并配置環(huán)境變量
這篇文章主要介紹了MAC 系統(tǒng)安裝java并配置環(huán)境變量的相關(guān)資料,需要的朋友可以參考下2017-03-03iOS應(yīng)用中UILabel文字顯示效果的常用設(shè)置總結(jié)
UILabel組件可以用來(lái)設(shè)置文字內(nèi)容的排版與字體效果等,功能非常多,下面就來(lái)為大家整理一下基本的iOS應(yīng)用中UILabel文字顯示效果的常用設(shè)置總結(jié)2016-05-05