概要
本章主要简示了IOS里面的触摸手势部分,触摸分为:开始触摸、移动、结束、取消,四个部分,本章主要根据这四个方法的信息监测触摸手势,按自己理解的,不知道正确的是怎么识别的,以后可以去对比对比,看和这里的方法有什么不同的地方。
结果展示
流程概要
1.因为触摸事件是逐渐向上层传递的,所以可以直接在UIView里面处理,也可以在UIViewController里面处理,本例子在控制类处理;
2.先在处理的四个方法里面打印触摸的信息,感受一下触摸动作对应的触摸信息及事件;
3.本例子使用cancelPreviousPerformRequestsWithTarget方法取消延迟调用performSelector,可以记一下该类型的使用
主要代码
h文件
//
// ViewController.h
// TouchDetect
//
// Created by God Lin on 14/12/16.
// Copyright (c) 2014年 arbboter. All rights reserved.
//
#import <UIKit/UIKit.h>
typedef enum
{
TOUCH_TYPE_CLICK, // 单击
TOUCH_TYPE_DBCLICK, // 双击
TOUCH_TYPE_MOVE_LEFT, // 左滑
TOUCH_TYPE_MOVE_RIGHT, // 右滑
TOUCH_TYPE_MOVE_TOP, // 上滑
TOUCH_TYPE_MOVE_BOTTOM, // 下滑
TOUCH_TYPE_ZOOM_IN, // 缩小
TOUCH_TYPE_ZOOM_OUT, // 放大
TOUCH_TYPE_TOUCH_ON // 长按
}TOUCH_TYPE;
@interface ViewController : UIViewController
{
BOOL _bZoom;
CGPoint _lastPoint;
CGFloat _fLastDistance;
TOUCH_TYPE _eTouchType;
UITextField* _textBox;
}
@property (nonatomic, readwrite) BOOL _bZoom;
@property (nonatomic, readwrite) CGFloat _fLastDistance;
@property (nonatomic, readwrite) CGPoint _lastPoint;
@property (nonatomic, readwrite) TOUCH_TYPE _eTouchType;
@property (nonatomic, retain) UITextField* _textBox;
@end
m文件
//
// ViewController.m
// TouchDetect
//
// Created by God Lin on 14/12/16.
// Copyright (c) 2014年 arbboter. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize _bZoom;
@synthesize _fLastDistance;
@synthesize _eTouchType;
@synthesize _textBox;
@synthesize _lastPoint;
- (void)printTouch:(UIEvent *)event
{
#if 0
NSSet* touchs = [event allTouches];
CGPoint viewPoint;
CGPoint screenPoint;
NSLog(@"touch count = %lu", (unsigned long)touchs.count);
for (UITouch* touch in touchs)
{
viewPoint = [touch locationInView:self.view];
screenPoint = [touch locationInView:nil];
NSLog(@"view position : [%.2f, %.2f]", viewPoint.x, viewPoint.y);
NSLog(@"screen position : [%.2f, %.2f]", screenPoint.x, screenPoint.y);
NSLog(@"tab count : %ld", touch.tapCount);
NSLog(@"tab phase : %ld", touch.phase);
NSLog(@"tab timestamp : %f", touch.timestamp);
}
#endif
}
- (void)showTouchType
{
#if 0
NSArray* array = [[NSArray alloc] initWithObjects:@"TOUCH_TYPE_CLICK",
@"TOUCH_TYPE_DBCLICK",
@"TOUCH_TYPE_MOVE_LEFT",
@"TOUCH_TYPE_MOVE_RIGHT",
@"TOUCH_TYPE_MOVE_TOP",
@"TOUCH_TYPE_MOVE_BOTTOM",
@"TOUCH_TYPE_ZOOM_IN",
@"TOUCH_TYPE_ZOOM_OUT",
@"TOUCH_TYPE_TOUCH_ON",
nil];
NSString* strMsg = [[NSString alloc] initWithFormat:@"Touch type : %@", [array objectAtIndex:(NSUInteger)_eTouchType]];
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Infomation" message:strMsg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
[strMsg release];
[array release];
#else
NSLog(@"touch type : %d",_eTouchType);
CGFloat x = _textBox.frame.origin.x;
CGFloat y = _textBox.frame.origin.y;
CGFloat w = _textBox.frame.size.width;
CGFloat h = _textBox.frame.size.height;
switch (_eTouchType)
{
case TOUCH_TYPE_DBCLICK:
if(_bZoom)
{
w += 10;
h += 10;
}
else
{
w -= 10;
h -= 10;
}
_bZoom = !_bZoom;
break;
case TOUCH_TYPE_ZOOM_OUT:
w += 10;
h += 10;
break;
case TOUCH_TYPE_ZOOM_IN:
w -= 10;
h -= 10;
break;
case TOUCH_TYPE_MOVE_BOTTOM:
y += 10;
break;
case TOUCH_TYPE_MOVE_TOP:
y -= 10;
break;
case TOUCH_TYPE_MOVE_LEFT:
x -= 10;
break;
case TOUCH_TYPE_MOVE_RIGHT:
x += 10;
break;
default:
break;
}
_textBox.frame = CGRectMake(x, y, w, h);
#endif
}
#pragma touch events
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touchesBegan");
[self printTouch:event];
CGPoint point1;
CGPoint point2;
NSArray* arrayTouchs = [[event allTouches] allObjects];
UITouch* touch1 = nil;
UITouch* touch2 = nil;
switch (arrayTouchs.count)
{
case 1:
{
touch1 = [arrayTouchs objectAtIndex:0];
if(touch1.tapCount == 1)
{
_eTouchType = TOUCH_TYPE_CLICK;
[self performSelector:@selector(showTouchType) withObject:self afterDelay:0.3];
}
else
{
_eTouchType = TOUCH_TYPE_DBCLICK;
[[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(showTouchType) object:self];
[self showTouchType];
}
_lastPoint = [touch1 locationInView:self.view];
}
break;
case 2:
{
touch1 = [arrayTouchs objectAtIndex:0];
point1 = [touch1 locationInView:self.view];
touch2 = [arrayTouchs objectAtIndex:1];
point2 = [touch2 locationInView:self.view];
CGFloat x = point1.x - point2.x;
CGFloat y = point1.y - point2.y;
_fLastDistance = sqrt(x*x + y*y);
}
break;
default:
break;
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touchesMoved");
[self printTouch:event];
[[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(showTouchType) object:self];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touchesEnded");
[self printTouch:event];
CGPoint point1;
CGPoint point2;
NSArray* arrayTouchs = [[event allTouches] allObjects];
UITouch* touch1 = nil;
UITouch* touch2 = nil;
switch (arrayTouchs.count)
{
// 滑动
case 1:
{
touch1 = [arrayTouchs objectAtIndex:0];
point1 = [touch1 locationInView:self.view];
CGFloat xMove = point1.x - _lastPoint.x;
CGFloat yMove = point1.y - _lastPoint.y;
if(ABS(xMove)>10 || ABS(yMove)>10)
{
// 横移动
if(ABS(yMove) > ABS(xMove))
{
if(yMove > 0)
{
_eTouchType = TOUCH_TYPE_MOVE_BOTTOM;
}
else
{
_eTouchType = TOUCH_TYPE_MOVE_TOP;
}
}
else
{
if(xMove > 0)
{
_eTouchType = TOUCH_TYPE_MOVE_RIGHT;
}
else
{
_eTouchType = TOUCH_TYPE_MOVE_LEFT;
}
}
[self showTouchType];
}
}
break;
case 2:
{
touch1 = [arrayTouchs objectAtIndex:0];
point1 = [touch1 locationInView:self.view];
touch2 = [arrayTouchs objectAtIndex:1];
point2 = [touch2 locationInView:self.view];
CGFloat x = point1.x - point2.x;
CGFloat y = point1.y - point2.y;
CGFloat fNowDistance = sqrt(x*x + y*y);
if(fNowDistance > _fLastDistance)
{
_eTouchType = TOUCH_TYPE_ZOOM_OUT;
}
else if(fNowDistance < _fLastDistance)
{
_eTouchType = TOUCH_TYPE_ZOOM_IN;
}
else
{
break;
}
[self showTouchType];
}
break;
default:
break;
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touchesCancelled");
[self printTouch:event];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UITextField* textBox = [[UITextField alloc] initWithFrame:CGRectMake(30, 100, 200, 40)];
self._textBox = textBox;
_textBox.layer.borderWidth = 2;
_textBox.layer.cornerRadius = 5;
_textBox.text = @"I will move";
_textBox.userInteractionEnabled = NO;
_textBox.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:_textBox];
_bZoom = YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc
{
[_textBox release];
[super dealloc];
}
@end