手勢操作---辨認單擊照樣雙擊
在視圖上同時辨認單擊手勢和雙擊手勢的成績在於,當檢測到一個單擊操作時,沒法肯定是確切是一個單擊操作或許只是雙擊操作中的第一次點擊。處理這個成績的辦法就是:在檢測到單擊時,須要等一段時光期待第二次點擊,假如沒有第二次點擊,則為單擊操作;假如有第二次點擊,則為雙擊操作。
檢測手勢有兩種辦法,一種是定制子視圖,重寫視圖從UIResponder類中繼續來的事宜處置辦法,即touchesBegan:withEvent:等一系列辦法來檢測手勢;另外一個辦法是應用手勢辨認器,即UIGestureRecognizer的各類詳細子類。
一.重寫事宜處置辦法
- (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];
//...
}
起首確認定制視圖的userInteractionEnabled和multipleTouchEnabled屬性都為YES.
在touchesEnded:withEvent:辦法中,假如是第一次觸摸停止,則cancelPreviousPerformRequestsWithtarget:辦法不會起感化,由於self未調劑任何辦法,此時tapCount為1,應用performSelector:withObject:afterDelay:挪用單擊事宜處置辦法,在0.3s鐘後履行。
[self performSelector:@selector(handleSingleTap:) withObject:[NSValue valueWithCGPoint:touchPoint] afterDelay:0.3];
假如這是一個單擊操作,則前面0.3鐘內不會再有觸摸事宜,則handleSingleTap:辦法履行,如許辨認出了單擊操作。
假如這是一個雙擊操作,則第二次點擊在0.3s內觸發,在第二次觸摸操作的touchesEnded:withEvent:辦法中,cancelPreviousPerformRequestsWithtarget:起首會撤消之前對handleSingleTap:辦法的調劑,使之不會履行,然後在挪用handleDoubleTap:辦法處置雙擊操作。
二.應用Gesture Recognizer
應用Gesture Recognizer辨認就會簡略很多,只需添加兩個手勢辨認器,分離檢測單擊和雙擊事宜,設置需要的屬性便可。
- (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];
這句話的意思時,只要當doubleTapGesture辨認掉敗的時刻(即辨認出這不是雙擊操作),singleTapGesture能力開端辨認,同我們一開端講的是統一個成績。
UIGestureRecognizer小運用
1、輕鼓掌勢:雙指、單擊,修正imageView的frame為(0,0,320,200)
2、長按手指:單指,修正imageView的alpha=0.5
3、完成平移、扭轉、捏合
4、輕掃:豎向輕掃完成圖:像隨機切換顯示;橫向輕掃完成:圖象消逝,隨機修正imageview的配景色彩
5、imageview每次只能添加一種手勢辨認器。
#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; // 垂直滑動是YES,程度滑動是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 - 設置圖象
- (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 - 設置手勢
#pragma mark 點擊手勢
- (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 長按手勢
- (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 平移拖拽手勢
- (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 = CGAff.netransformTranslate(self.imageView.transform, txty.x, txty.y);
[panGesture setTranslation:CGPointMake(0, 0) inView:self.view];
}
#pragma mark 扭轉手勢
- (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 = CGAff.netransformRotate(self.imageView.transform, rotateGesture.rotation);
rotateGesture.rotation = 0;
}
#pragma mark 捏合縮撒手勢
- (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 = CGAff.netransformScale(self.imageView.transform, pinchGesture.scale, pinchGesture.scale);
pinchGesture.scale = 1;
}
#pragma mark - 輕掃手勢
#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 - 設置按鈕
- (void)createButtonView{
NSArray * buttonArray = @[@"輕點",@"長按",@"平移",@"扭轉",@"捏合",@"輕掃"];
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開辟中的幾個手勢操作實例分享】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!