Ios中手势的利用

来源:互联网 发布:php 创建远程文件夹 编辑:程序博客网 时间:2024/05/17 00:50

1.  触摸

1.1.  触摸术语

  • 事件:在手指与设备的屏幕发生交互时生成,事件记录手势的相关信息
  • 轻击:当手指触摸屏幕然后立即离开屏幕不发生任何方向的移动叫做一次轻击
  • 手势:如按下、拖动、离开、取消
  • 响应者链:屏幕的一切事件的传递流程

1.2.  如何拦截按下事件

-       (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event {

UITouch*touch = [touches anyObject];
NSUInteger numTaps = [touch tapCount];
//获取按下时的屏幕参数

}

1.3.  如何拦截拖动事件

-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent *)event{

    //获取实时的坐标

}

1.4.  如何拦截离开事件

-       (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent*)event {

UITouch*touch = [touches anyObject];
NSUInteger numTaps = [touch tapCount];

}

1.5.  如何拦截取消事件

-       (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent*)event {

UITouch*touch = [touches anyObject];
NSUInteger numTaps = [touch tapCount];

}

1.6.  坐标及大小

  • CGPoint:空间的位置{x,y}
  • CGSize:尺寸{width,height}
  • CGRect:矩形范围

1.7.  练习一:如何判断是否点到了某个控件

-       获取到相对于此控件的CGPoint对象:

-       获取此对象的CGRect  :bounds方法

-       判断这个rect是否包含此point

1.8.  练习二:实现拖拽效果

效果图:

实现流程:

  • 添加两张图片到界面当中并在屏幕底部添加两个View背景为蓝色和黄色
  • 添加touch事件
  • 在begin里面获取图片信息
  • 通过图片信息再创建一份UIImageView
  • 将新UIImageView添加到屏幕当中
  • 在move里面获取实时的坐标,将此坐标设置给图片的显示参数
  • 在end里面将拖动的图片删除并判断松手的区域是否在区域a或b
  • 如果在其中某个区域在此区域里面添加图片

2.  多点触控

2.1.  如何判断多点触控

  • 判断touchesBegan的touches参数的count属性即可:

-(void)touchesBegan:(NSSet *)touches 

          withEvent:(UIEvent*)event{

[touchescount];//获取点击到屏幕的点的数量

}

2.2.  如何获取每个点的坐标

  • 判断touchesBegan的touches参数的count属性即可:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

if([touchescount]==2) {//识别两点触摸,并记录两点间距离

NSArray*twoTouches=[touches allObjects];

CGPoint  p1 = [[twoTouches objectAtIndex:0];

CGPointp2 = [[twoTouches objectAtIndex:1];

}}

2.3.  实现两点的图片缩放流程

  • 添加图片到界面显示
  • 添加touch相关的事件
  • 按下的时候获取两点间的距离
  • 在move里面获取实时两点间的距离
  • 通过实时两点间的距离和按下时两点间的距离的比值决定图片应该缩放的倍数
  • 通过算出的缩放倍数设置给图片

2.4.  两点间距离公式

-(CGFloat)spaceToPoint:(CGPoint)first FromPoint:(CGPoint)two{

//计算两点之间的距离

           float x = first.x – two.x;

           float y = first.y – two.y;

           returnsqrt(x * x + y * y);

}

2.5.  通过算出的缩放倍数设置给图片

if(fabsf(currSpace-originSpace)>=min_offset) 

{//两指间移动距离超过min_offset,识别为手势“捏合”

           CGFloat s=currSpace/originSpace; //计算缩放比例

           [self scaleTo:s];

}

3.  手势

3.1.  都有哪几种手势

   1、拍击UITapGestureRecognizer (任意次数的拍击) 

   2、向里或向外捏UIPinchGestureRecognizer (用于缩放) 

   3、摇动或者拖拽UIPanGestureRecognizer 

   4、划动UISwipeGestureRecognizer (以任意方向) 

   5、旋转UIRotationGestureRecognizer(手指朝相反方向移动) 

   6、长按UILongPressGestureRecognizer

3.2.  如何使用点击手势:UITapGestureRecognizer

//创建一个手势识别器

UITapGestureRecognizer*oneFingerTwoTaps = [[[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(oneFingerTwoTaps)] autorelease];

//Set required taps and number of touches

[oneFingerTwoTapssetNumberOfTapsRequired:2];

[oneFingerTwoTapssetNumberOfTouchesRequired:1];

//Add the gesture to the view

[[selfview] addGestureRecognizer:oneFingerTwoTaps];

消息方法oneFingerTwoTaps

-(void)oneFingerTwoTaps{

  NSLog(@"Action: One finger, twotaps");}

3.3.  如何使用缩放手势:UIPinchGestureRecognizer

UIImageView *vi;

//缩放

- (IBAction)Pinch:(id)sender {

    UIPinchGestureRecognizer*pinch=sender;

   vi.transform=CGAffineTransformScale(vi.transform,pinch.scale,pinch.scale);

   pinch.scale=1; //每次缩放后使值初始化

}

3.4.  如何使用旋转手势:UIRotationGestureRecognizer

//旋转

- (IBAction)Rotation:(id)sender {

   UIRotationGestureRecognizer *rotation=sender;

   vi.transform=CGAffineTransformRotate(vi.transform, rotation.rotation);

   rotation.rotation=0;  注意:每次旋转后进行初始化

}

 

 

 

 

UIRotationGestureRecognizer*twoFingersRotate =

  [[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(twoFingersRotate:)] autorelease];

[[selfview] addGestureRecognizer:twoFingersRotate];

-(void)twoFingersRotate:(UIRotationGestureRecognizer *)recognizer{

  // Convert the radian value to show thedegree of rotation

  NSLog(@"Rotation in degrees since lastchange: %f", [recognizer rotation] * (180 / M_PI));

 UIImageView旋转

UIImageView *_weiboContentTextView;

_weiboContentTextView.transform=CGAffineTransformMakeRotation(CGFloatangle);传入弧度值

弧度值为:角度**M_PI/180

}

3.5.  如何使用任意方向移动手势:UISwipeGestureRecognizer

//向上移动

UISwipeGestureRecognizer*oneFingerSwipeUp =

  [[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(oneFingerSwipeUp:)] autorelease];

[oneFingerSwipeUpsetDirection:UISw

ipeGestureRecognizerDirectionUp];//这里有四个方向

[[selfview] addGestureRecognizer:oneFingerSwipeUp];

-(void)oneFingerSwipeUp:(UISwipeGestureRecognizer *)recognizer{

  CGPoint point = [recognizerlocationInView:[self view]];

  NSLog(@"Swipe up - start location:%f,%f", point.x, point.y);

}

3.6.  如何使用长按手势:UILongPressGestureRecognizer

UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)];  

lpgr.minimumPressDuration = 1.0; //seconds  

lpgr.delegate = self;  

[self.view addGestureRecognizer:lpgr];  

原创粉丝点击