当前位置: 首页 > 编程日记 > 正文

iOS中的各种手势

/**
基类UIGestureRecognizer
UITapGestureRecognizer Tap 点击
UIPanGestureRecognizer Pan (慢速滑动,拖移)
UILongPressGestureRecognizer LongPress (长按)
UIPinchGestureRecognizer Pinch (捏合,两手指往内或外拨动)
UIRotationGestureRecognizer Rotation (旋转)
UISwipeGestureRecognizer Swipe (快速滑动,轻扫)
*/

#import "ViewController.h"

#define kRandomValue (arc4random() %256 /256.0)
@interface ViewController () <UIGestureRecognizerDelegate>

@property (nonatomic,strong) UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
//强烈不建议往一个控件上加很多手势
[self addImageView];
[self addTapGesture];//点击手势
//[self addPanGesture];//拖移手势
[self addLongPressGesture];//长按手势
[self addPinchGesture];//捏合
[self addResetButton];//重置
[self addRotationGesture];//旋转
[self addSwipeGesture];//轻扫
}

-(void)addImageView{
UIImage *image=[UIImage imageNamed:@"cloth"];
self.imageView=[[UIImageView alloc] initWithImage:image];//这样写默认imageView的大小和image的大小一样
self.imageView.userInteractionEnabled=YES;//打开用户交互
self.imageView.center=self.view.center;
[self.view addSubview:self.imageView];
}

-(void)addTapGesture{
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] init];
//tap.numberOfTapsRequired=2;//默认为1,点击次数
//tap.numberOfTouchesRequired=2;//默认为1,多少根手指触摸
[tap addTarget:self action:@selector(tap:)];
[self.imageView addGestureRecognizer:tap];
}
-(void)tap:(UITapGestureRecognizer *)tap{
UIView *view=tap.view;
view.superview.backgroundColor=[UIColor colorWithRed:kRandomValue green:kRandomValue blue:kRandomValue alpha:1.0];
}

-(void)addPanGesture{
UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc] init];
pan.delegate=self;
[pan addTarget:self action:@selector(pan:)];
[self.imageView addGestureRecognizer:pan];
}
-(void)pan:(UIPanGestureRecognizer *)pan{
CGPoint offset=[pan
translationInView:self.view];
CGPoint center=self.imageView.center;
center.x+=offset.x;
center.y+=offset.y;
self.imageView.center=center;

//不让其累加
[pan setTranslation:CGPointZero inView:self.view];
}

-(void)addLongPressGesture{
UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc ] init];
[longPress addTarget:self action:@selector(longPress:)];
[self.imageView addGestureRecognizer:longPress];
}
-(void)longPress:(UILongPressGestureRecognizer *)longPress{
switch (longPress.state) {
case UIGestureRecognizerStateBegan:
NSLog(@"你长按了");
break;
case UIGestureRecognizerStateEnded:
NSLog(@"长按结束");
break;
default:
break;
}
}

-(void)addPinchGesture{
UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
[self.imageView addGestureRecognizer:pinch];
}
-(void)pinch:(UIPinchGestureRecognizer *)pinch{
NSLog(@"%lf",pinch.scale);
//捏合 scale缩放比例
//transform 变换,仿射变换/矩阵变换,
self.imageView.transform=CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);
//重置捏合手势比例,每次都应该相对于当前的
pinch.scale=1;//下次相对比例应该从1开始
}

-(void)addResetButton{
UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
button.frame=CGRectMake(0, CGRectGetMaxY(self.imageView.frame)+60, 200, 40);
[button setTitle:@"Reset" forState:UIControlStateNormal];
[button addTarget:self action:@selector(reset:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)reset:(id)sender{
//恢复imageView的transform
self.imageView.transform=CGAffineTransformIdentity;
}

-(void)addRotationGesture{
UIRotationGestureRecognizer *rotation=[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
[self.imageView addGestureRecognizer:rotation];
}
-(void)rotation:(UIRotationGestureRecognizer *)rotation{
NSLog(@"旋转弧度:%lf",rotation.rotation);
self.imageView.transform=CGAffineTransformRotate(self.imageView.transform, rotation.rotation);
rotation.rotation=0;//重置旋转弧度,相当于当前的从0开始
}

-(void)addSwipeGesture{
UISwipeGestureRecognizer *swipe=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
//swipe默认的direction为right
swipe.direction=UISwipeGestureRecognizerDirectionUp;
[self.imageView addGestureRecognizer:swipe];
}
-(void)swipe:(UISwipeGestureRecognizer *)swipe{
CGPoint center=self.imageView.center;
center.y -=50;
self.imageView.center=center;
}

#pragma mark -UIGestureRecognizerDelegate
//是否支持多个手势
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}

@end

相关文章:

Nginx问题定位之监控进程异常退出

nginx在运行过程中是否稳定&#xff0c;是否有异常退出过&#xff1f;这里总结几项平时会用到的小技巧。 1. 在error.log中查看是否有signal项&#xff0c;如果有&#xff0c;看看signal是多少。 比如&#xff0c;这是一个异常退出的情况&#xff1a; $grep signal error.log20…

k3应付系统初始化应付票据_在家工作时应付无尽干扰的真实感觉

k3应付系统初始化应付票据Whether or not you have worked remotely before, you’ve likely never had to share your “home office” with your partner and two children. 无论您以前是否在远程工作&#xff0c;您都可能从未与伴侣和两个孩子共享“家庭办公室”。 Before …

WinForm绘制带有升序、降序的柱形图

WinForm绘制带有升序、降序的柱形图 private void HuiZhiTu( string strPaiXu){//初始数据int[] nums { 150, 89, 200, 60, 70, 90 };if (strPaiXu "升序"){//冒泡排序for (int i 0; i < nums.Length; i){for (int j 0; j < nums.Length-1; j){if (nums[i]…

更轻量的 View Controllers

原文链接&#xff1a;http://objccn.io/issue-1-1/ View controllers 通常是 iOS 项目中最大的文件&#xff0c;并且它们包含了许多不必要的代码。所以 View controllers 中的代码几乎总是复用率最低的。我们将会看到给 view controllers 瘦身的技术&#xff0c;让代码变得可以…

Alpha阶段项目总结

一&#xff0c;设想和目标 1. 我们的软件要解决什么问题&#xff1f;是否定义得很清楚&#xff1f;是否对典型用户和典型场景有清晰的描述&#xff1f; 针对铁大校园&#xff0c;解决旧物堆积、资源浪费的问题。我们的定义很清楚&#xff0c;对于典型用户和场景有过清晰的描述。…

如何成为软件工程师的团队合作者

In my first software engineering role at an eCommerce brand, I often secretly worked on tasks outside of my core responsibilities. And many times I felt isolated from my teammates. 在电子商务品牌的第一个软件工程职位中&#xff0c;我经常秘密地从事核心职责以外…

HTML转义字符

转自地址&#xff1a;http://blog.csdn.net/wusuopubupt/article/details/8817826 by wusuopubupt No.文字表記10進表記16進表記文字 Comment001&quot;&#x22;""" quotation mark APL quote002&amp;&#x26;"&" ampersand003<&l…

制作模拟器和真机通用静态库

通常在项目中使用静态库的时候都会有两个版本&#xff0c;一个用于模拟器&#xff0c;一个用于真机&#xff0c;因为Mac和iPhone的CPU不同&#xff0c;才造成了这种情况。 为了模拟器与真机之间切换调试的方便&#xff0c;制作通用版本非常有必要。现在有两个版本的静态库libSQ…

使用静态URL片段 URL路由 精通ASP-NET-MVC-5-弗瑞曼

转载于:https://www.cnblogs.com/ganmk--jy/p/5570718.html

sql算术运算符_SQL运算符教程–按位,比较,算术和逻辑运算符查询示例

sql算术运算符At its core, the internet and all its applications are just data.互联网及其所有应用程序的核心只是数据。 Every email, tweet, selfie, bank transaction, and more is just data sitting in a database somewhere.每封电子邮件&#xff0c;推文&#xff0…

python 之socket 网络编程

socket通常也称作"套接字"&#xff0c;用于描述IP地址和端口&#xff0c;是一个通信链的句柄&#xff0c;应用程序通常通过"套接字"向网络发出请求或者应答网络请求。 socket起源于Unix&#xff0c;而Unix/Linux基本哲学之一就是“一切皆文件”&#xff0c…

(转)使用 Spring缓存抽象 支持 EhCache 和 Redis 混合部署

背景&#xff1a;最近项目组在开发本地缓存&#xff0c;其中用到了redis和ehcache&#xff0c;但是在使用注解过程中发现两者会出现冲突&#xff0c;这里给出解决两者冲突的具体方案。 spring-ehcache.xml配置&#xff1a; <?xml version"1.0" encoding"UTF…

终端SVN常用命令

svn help 帮助 svn checkout path 从服务器checkout文件到本地(path是服务器上的目录&#xff0c;简写svn co path) svn add file_name 往代码库添加新的文件 svn commit -m "xxx" 提交添加的文件&#xff0c;或者本地做的修改到服务器端(“xxx”内为提交说明…

程序员怎么赚更多的钱_自由职业技巧:如何感到更加自信和赚更多钱

程序员怎么赚更多的钱Over my 10 years as a freelance developer, many fellow freelancers have asked me for advice. How can they make freelancing work for them?在我作为自由开发者的10年中&#xff0c;许多自由职业者都向我寻求建议。 他们如何让他们从事自由职业&am…

RedHat 7.0及CentOS 7.0禁止Ping的三种方法

作者&#xff1a;荒原之梦原文链接&#xff1a;http://zhaokaifeng.com/?p538前言&#xff1a; “Ping”属于ICMP协议&#xff08;即“Internet控制报文协议”&#xff09;&#xff0c;而ICMP协议是TCP/IP协议的一个子协议&#xff0c;工作在网际层。ICMP协议主要用于传输网络…

关于sql 增删改

1.更改数据库的名称 --更改数据库的名称&#xff0c;逗号前面是之前的&#xff0c;后面是改成的名 sp_renamedb student,xuesheng 2.表中有数据的情况下再添加列、删除列 --后来添加列&#xff0c;只能默认可以为空值 altear table shuiguo add [int] varchar(10) --int加上中括…

使用version遇到的那些坑

公司代码管理使用的SVN, 所以就用到了SVN工具version 公司没给买正版的version, 遇到各种崩溃, 各种坑 1. 更新项目时遇到网络不稳定的情况, 更新失败, 项目中的某个文件就莫名其妙的被锁定了 !!! 如果只是更新一个文件还好说, unlock一下就好了,但是如果你是一个文件夹全部…

docker手册_Docker手册

docker手册The concept of containerization itself is pretty old, but the emergence of the Docker Engine in 2013 has made it much easier to containerize your applications. 容器化本身的概念还很老&#xff0c;但是Docker Engine在2013年的出现使容器化应用程序变得更…

MongoDB修改器的使用1

为什么要使用修改器&#xff1f; 通常我们只会修改文档的一部分&#xff0c;这时候更新整个文档就显得很麻烦&#xff0c;通常是通过原子性的更新修改器来完成。 1."$set"修改器 "$set"用来指定某个字段&#xff0c;如果不存在&#xff0c;则创建。这对部…

4GL之Non-SCROLLING CURSOR

在4gl中CURSOR可以说是每一个程序中都会有的&#xff0c;而CURSOR又分为三种SCROLLING CURSOR、Non-SCROLLING CURSOR、LOCKING CURSOR。 Non-SCROLLING CURSOR的聲明有兩種&#xff0c;一種是先定義好sql語句到一個變量里&#xff1a; DECLARE cursor名 CURSOR FROM 變量…

项目总结三--波纹视图

波纹视图的使用 代码在github&#xff1a;https://github.com/wyon0313/YGMoireAnimation

vlookup示例_VLOOKUP示例–如何在Excel中执行VLOOKUP

vlookup示例Microsoft Excel includes a variety of different functions that help users with calculations of any kind. The functionality of Excel is so comprehensive that average users dont even take advantage of most utilities.Microsoft Excel包括各种不同的功…

MySQL--从库启动复制报错1236

链接:http://blog.csdn.net/yumushui/article/details/42742461 今天在搭建一个MySQL master-slave集群时&#xff0c;执行了change master命令&#xff0c;然后又 start slave 启动slave服务&#xff0c;结果查看salve状态就出现错误了&#xff1a; mysql> show slave stat…

使用Script元素发送JSONP请求

// 根据指定URL发送一个JSONP请求 //然后把解析得到的相应数据传递给回调函数 //在URL中添加一个名为jsonp的查询参数,用于指定该请求的回调函数的名称 function getJSONP(url, callback){//为本次请求创建一个唯一的回调函数名称var cbnum "cb"getJSONP.counter;va…

iOS 崩溃记录

dyld: Library not loaded: /System/Library/Frameworks/UserNotifications.framework/UserNotifications Referenced from: /var/containers/Bundle/Application/AEECAAFB-F14A-43AA-9FB8-8388CAC40122/DouLiao.app/DouLiao Reason: image not found 原因应该是iOS系统版本太…

以太坊Geth几种同步模式

链客&#xff0c;专为开发者而生&#xff0c;有问必答&#xff01; 此文章来自链客区块链技术问答社区&#xff0c;未经允许拒绝转载。 以太坊Geth几种同步模式 同步模式分类 –fast Enable fast syncing through state downloads –light Enable light client mode –s…

[转]Membership 到 .NET4.5 之 ASP.NET Identity

本文转自&#xff1a;http://www.cnblogs.com/jesse2013/p/membership-part3.html 我们前面已经讨论过了如何在一个网站中集成最基本的Membership功能&#xff0c;然后深入学习了Membership的架构设计。正所谓从实践从来&#xff0c;到实践从去&#xff0c;在我们把Membership的…

js填充select下拉框并选择默认值

/* 使用json数组填充下拉框并复选 *//* 初始化下拉框数据 */ var jsonStr { "data": [] }; for (var str in JsonStr.data) {jsonStr.data.push({ "value": JsonStr.data[str].value, "text": JsonStr.data[str].text }); }/* 调用BandSelectOb…

关于curl使用记录

因经常需要排除线上用户问题&#xff0c;查看用户数据请求结果&#xff0c;使用到curl命令&#xff0c;但是总是忘记&#xff0c;在此做下记录。 curl post请求命令行如下&#xff1a; curl -d "param0value0&param1value1" "url"

智能合约部署及调用

链客&#xff0c;专为开发者而生&#xff0c;有问必答&#xff01; 此文章来自链客区块链技术问答社区&#xff0c;未经允许拒绝转载。 智能合约部署及调用 以太坊区块链技术2.0版本对于行业应用的开发最主要特性就是实现了智能合约&#xff0c;本质上讲智能合约是由事件驱…