iOS开发中六种手势识别

iOS开发中手势识别有六种:

轻击手势(TapGestureRecognizer),

轻扫手势 (SwipeGestureRecognizer),

长按手势(LongPressGestureRecognizer),

拖动手势(PanGestureRecognizer),

捏合手势(PinchGestureRecognizer),

旋转手势(RotationGestureRecognizer),

1,轻击手势(TapGestureRecognizer)

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
tapGesture.numberOfTapsRequired = 1; //点击次数
tapGesture.numberOfTouchesRequired = 1; //点击手指数
[self.view addGestureRecognizer:tapGesture];

//轻击手势触发方法
-(void)tapGesture:(UITapGestureRecognizer *)sender
{
    //your code
}

2,长按手势(LongPressGestureRecognizer)

UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
//设置长按时间
longPressGesture.minimumPressDuration = 0.5;
[self.view addGestureRecognizer:longPressGesture];
//长按手势触发方法
-(void)longPressGesture:(id)sender
{
	 UILongPressGestureRecognizer *longPress = sender;
	 if (longPress.state == UIGestureRecognizerStateBegan)
	 {
		 //your code
	 }
}
说明:长按手势的常用状态如下
开始:UIGestureRecognizerStateBegan
改变:UIGestureRecognizerStateChanged
结束:UIGestureRecognizerStateEnded
取消:UIGestureRecognizerStateCancelled
失败:UIGestureRecognizerStateFailed

3,轻扫手势(SwipeGestureRecognizer)

UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
//设置轻扫的方向
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; //向右
[self.view addGestureRecognizer:swipeGesture];
UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
//设置轻扫的方向
swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft; //向左
[self.view addGestureRecognizer:swipeGestureLeft];
//轻扫手势触发方法
-(void)swipeGesture:(id)sender
{
	UISwipeGestureRecognizer *swipe = sender;
	if (swipe.direction == UISwipeGestureRecognizerDirectionLeft)
	{
		//向左轻扫
	}
	if (swipe.direction == UISwipeGestureRecognizerDirectionRight)
	{
		//向右轻扫
	}
}

4,捏合手势(PinchGestureRecognizer)

UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
[self.view addGestureRecognizer:pinchGesture];
	////捏合手势触发方法
-(void) pinchGesture:(id)sender
{
	UIPinchGestureRecognizer *gesture = sender;
	//手势改变时
	if (gesture.state == UIGestureRecognizerStateChanged)
	{
		 //捏合手势中scale属性记录的缩放比例
		_imageView.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale);
	}
	//结束后恢复
	if(gesture.state==UIGestureRecognizerStateEnded)
	{
		[UIView animateWithDuration:0.5 animations:^{
			_imageView.transform = CGAffineTransformIdentity;//取消一切形变
		}];
	}
}

5,拖动手势(PanGestureRecognizer)

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
[self.view addGestureRecognizer:panGesture];

//拖动手势触发方法
-(void) panGesture:(id)sender
{
    UIPanGestureRecognizer *panGesture = sender;
    CGPoint movePoint = [panGesture translationInView:self.view];
    //your code
}

6,旋转手势(RotationGestureRecognizer)

UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
[self.view addGestureRecognizer:rotationGesture];
//旋转手势触发方法
-(void)rotationGesture:(id)sender
{
	UIRotationGestureRecognizer *gesture = sender;
	if (gesture.state==UIGestureRecognizerStateChanged)
	{
		_imageView.transform=CGAffineTransformMakeRotation(gesture.rotation);
	}
	if(gesture.state==UIGestureRecognizerStateEnded)
	{
		[UIView animateWithDuration:1 animations:^{
			_imageView.transform=CGAffineTransformIdentity;//取消形变
		}];
	}
}
时间: 2024-08-06 20:02:28

iOS开发中六种手势识别的相关文章

iOS开发中UIPopoverController的使用详解

这篇文章主要介绍了iOS开发中UIPopoverController的使用,代码基于传统的Objective-C,需要的朋友可以参考下 一.简单介绍 1.什么是UIPopoverController 是iPad开发中常见的一种控制器(在iPhone上不允许使用) 跟其他控制器不一样的是,它直接继承自NSObject,并非继承自UIViewController 它只占用部分屏幕空间来呈现信息,而且显示在屏幕的最前面 2.使用步骤 要想显示一个UIPopoverController,需要经过下列步骤

iOS开发中一些有用的小代码

1.判断邮箱格式是否正确的代码: //利用正则表达式验证 -(BOOL)isValidateEmail:(NSString *)email { NSString *emailRegex = @"[A-Z0-9a-z._%+-][email protected][A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@&qu

IOS开发中使用CNContact对通讯录增删改查

IOS开发中使用CNContact对通讯录增删改查 首先当然是把CNcontact包含在工程中: 1 @import Contacts; 1.下面是增加联系人的程序段: 1 CNMutableContact * contact = [[CNMutableContact alloc]init]; 2 contact.imageData = UIImagePNGRepresentation([UIImage imageNamed:@"22"]); 3 //设置名字 4 contact.gi

ios开发中-AFNetworking 的简单介绍

Blog: Draveness 关注仓库,及时获得更新: iOS-Source-Code-Analyze 在这一系列的文章中,我会对 AFNetworking 的源代码进行分析,深入了解一下它是如何构建的,如何在日常中完成发送 HTTP 请求.构建网络层这一任务. AFNetworking 是如今 iOS 开发中不可缺少的组件之一.它的 github 配置上是如下介绍的: Perhaps the most important feature of all, however, is the ama

iOS 开发中用户记住账户,密码

在iOS开发中经常会用到记住账户.密码,以此来提高用户的体验.下面就浅谈一下账户.密码的存储. 一.登录 记录已登录用户步骤,存入偏好设置中存储放入一个数组. 具体存储 1:存储用户到偏好设置中,其中用户是一个数组向服务器响应客户端后的一些操作(如果响应数据成功)其中用户和密码是一一对应的 1.1先从沙盒中偏好设置中读取对应的用户集合 读取用户名: NSMutableArray *AccArys = [NSMutableArray arrayWithArray:[[NSUserDefaults

iOS开发中KVC、KVO简介

在iOS开发中,KVC和KVO是经常被用到的.可以使用KVC对对象的属性赋值和取得对象的属性值,可以使用KVO监听对象属性值的变化.简单介绍一下KVC和KVO. 一:键值编码(KVC) KVC,全称 Key Value Coding(键值编码),是OC 语言的一个特性,使用KVC,可以对对象的属性进行动态读写. KVC的操作方法由 NSKeyValueCoding协议提供,而NSObject已经实现了这个协议,因此OC中的几乎所有对象都可以使用KVC操作.常用的KVC操作方法有: (1)设置属性

iOS开发中打电话发短信等功能的实现

在APP开发中,可能会涉及到打电话.发短信.发邮件等功能.比如说,通常一个产品的“关于”页面,会有开发者的联系方式,理想情况下,当用户点击该电话号码时,能够自动的帮用户拨出去,就涉及到了打电话的功能. iOS开发中,有三种方式可以打电话: (1)直接跳到拨号界面,代码如下 1 2 NSURL *url = [NSURL URLWithString:@"tel://10010"];  [[UIApplication sharedApplication] openURL:url]; 缺点:

iOS开发——淫技篇&iOS开发中各种淫技总结(六)

iOS开发中各种淫技总结(六) swift中指针的使用 在 Swift 中,指针都使用一个特殊的类型来表示,那就是 UnsafePointer<T>.遵循了 Cocoa 的一贯不可变原则,UnsafePointer<T> 也是不可变的.当然对应地,它还有一个可变变体,UnsafeMutablePointer<T>.绝大部分时间里,C 中的指针都会被以这两种类型引入到 Swift 中:C 中 const 修饰的指针对应 UnsafePointer (最常见的应该就是 C

iOS开发中的压缩以及解压

事实上,在iOS开发中,压缩与解压,我都是采用第三方框架SSZipArchive实现的 gitHub地址:   https://github.com/ZipArchive/ZipArchive 上面有详细的使用方法 因为ZipArchive不支持ARC,所以如果你的工程开启了ARC,那么就需要对ZipArchive设置一下.在ZipArchive.mm编译选项中,增加-fno-objc-arc即可. 最后,需要为工程链接libz.dylib动态链接库. 使用示范(压缩): // 获得mainBu