iOS学习必须了解的七大手势
文章只要你有一点点基础应该就可以看的懂,文章只为学习交流
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,retain)UIImageView *imageView;
@property (nonatomic,assign)NSInteger index;//下标
@property (nonatomic,retain)NSMutableArray *images;//图片名 字数组
@end
@implementation ViewController
#加载视图
-(void)viewDidLoad
{
[super viewDidLoad];
//布局imageView
[self layoutImageView];
//1.创建轻拍手势
[self tapGestureRecognizer];
//2.创建清扫手势
[self swipeGestureRecognizer];
//3.创建长安手势
[self longPressGestureRecognizer];
//4.创建平移手势
[self panGestureTecognizer];
//5.创建捏合手势
[self pinchGestureRecognizer];
//6.创建 旋转手势
[self rotationGestureRecognizer]
//7.创建边缘手势
[self screenEdgePanGestureRecognizer];
}
##布局ImageView
-(void)layoutImageView
{
//创建对象
UIImageView *imageView =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 375, 667)];
//配置属性
imageView.backgroundColor =[UIColor purpleColor];
//设置图片
imageView.image =[UIImage imageNamed:@"car1.jpg"];
//添加父视图
[self.view addSubview:imageView];
//将创建的图片视图对象 给属性赋值
self.imageView =imageView;
//打开用户交互
self.imageView.userInteractionEnabled =YES;
self.images =[NSMutableArray array];
for (int i = 1; i<9; i++) {
NSString * imageName =[NSString stringWithFormat:@"car%d.jpg",i];
[_images addObject:imageName];
}
// _index =1;
}
#pragma 轻怕手势
//创建轻拍手势
-(void)tapGestureRecognizer
{
//创建手势对象
UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
//配置属性
//轻拍次数
tap.numberOfTapsRequired =1;
//轻拍手指个数
tap.numberOfTouchesRequired =1;
//讲手势添加到指定的视图上
[_imageView addGestureRecognizer:tap];
}
//轻拍事件
-(void)tapAction:(UITapGestureRecognizer *)tap
{
//图片切换
NSLog(@"拍一下");
_index ++;
if (_index == 8) {
_index = 1;
}
self.imageView.image =[UIImage imageNamed:_images[_index]];
}
#pragma 清扫手势
//清扫手势
-(void)swipeGestureRecognizer
{
UISwipeGestureRecognizer *swipe =[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
//配置属性
//一个清扫手势 只能有两个方向(上和下) 或者 (左或右)
//如果想支持上下左右清扫 那么一个手势不能实现 需要创建两个手势
swipe.direction =UISwipeGestureRecognizerDirectionLeft;
//添加到指定视图
[_imageView addGestureRecognizer:swipe];
UISwipeGestureRecognizer *swipe2 =[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
swipe2.direction =UISwipeGestureRecognizerDirectionRight;
[_imageView addGestureRecognizer:swipe2];
}
//清扫事件
-(void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
NSLog(@"扫一下");
if (swipe.direction ==UISwipeGestureRecognizerDirectionRight) {
NSLog(@"右清扫");
_index --;
if (_index < 1) {
_index =7;
}
_imageView.image =[UIImage imageNamed:_images[_index]];
}else if(swipe.direction == UISwipeGestureRecognizerDirectionLeft){
NSLog(@"左扫一下");
_index ++;
if (_index == 8) {
_index=1;
}
_imageView.image =[UIImage imageNamed:_images[_index]];
}
}
#pragma 长按手势
//创建长按手势
-(void)longPressGestureRecognizer
{
UILongPressGestureRecognizer *longPress =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
//最短长按时间
longPress.minimumPressDuration =2;
//允许移动最大距离
longPress.allowableMovement =1;
//添加到指定视图
[_imageView addGestureRecognizer:longPress];
}
//长按事件
-(void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
//NSLog(@"长按");
//对于长安有开始和 结束状态
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"长按开始");
//将图片保存到相册
UIImage *image =[UIImage imageNamed:_images[_index]];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}else if (longPress.state == UIGestureRecognizerStateEnded)
{
NSLog(@"长按结束");
}
}
#pragma 平移手势
//创建平移手势
-(void)panGestureTecognizer
{
UIPanGestureRecognizer *pan =[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
//添加到指定视图
[_imageView addGestureRecognizer:pan];
}
//创建平移事件
-(void)panAction:(UIPanGestureRecognizer *)pan
{
//获取手势的位置
CGPoint position =[pan translationInView:_imageView];
//通过stransform 进行平移交换
_imageView.transform = CGAffineTransformTranslate(_imageView.transform, position.x, position.y);
//将增量置为零
[pan setTranslation:CGPointZero inView:_imageView];
}
#pragma 捏合手势
-(void)pinchGestureRecognizer
{
UIPinchGestureRecognizer *pinch =[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
//添加到指定视图
[_imageView addGestureRecognizer:pinch];
}
//添加捏合事件
-(void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
//通过 transform(改变) 进行视图的视图的捏合
_imageView.transform =CGAffineTransformScale(_imageView.transform, pinch.scale, pinch.scale);
//设置比例 为 1
pinch.scale = 1;
}
#pragma 旋转手势
//创建旋转手势
-(void)rotationGestureRecognizer
{
UIRotationGestureRecognizer *rotation =[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
//添加到指定的视图
[_imageView addGestureRecognizer:rotation];
}
//旋转事件
-(void)rotationAction:(UIRotationGestureRecognizer *)rote
{
//通过transform 进行旋转变换
_imageView.transform = CGAffineTransformRotate(_imageView.transform, rote.rotation);
//将旋转角度 置为 0
rote.rotation = 0;
}
#pragma 边缘手势
//创建边缘手势
-(void)screenEdgePanGestureRecognizer
{
UIScreenEdgePanGestureRecognizer *screenPan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screenPanAction:)];
[_imageView addGestureRecognizer:screenPan];
}
//创建边缘事件
-(void)screenPanAction:(UIScreenEdgePanGestureRecognizer *)screenPan
{
NSLog(@"边缘");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
iOS学习必须了解的七大手势的更多相关文章
- iOS学习笔记——触控与手势
触控 此部分内容已学良久,恨记之甚晚,忙矣,懒矣!本文简而记焉,恐日后忘也. 在iOS的触控事件中,有触控.事件以及响应者这三个角色,一个触摸则代表了一只手指和屏幕接触这个动作所包含的信息:而事件则包 ...
- iOS学习路线图
一.iOS学习路线图 二.iOS学习路线图--视频篇 阶 段 学完后目标 知识点 配套学习资源(笔记+源码+PPT) 密码 基础阶段 学习周期:24天 学习后目标: ...
- ios学习之UISwipeGestureRecognizer手势识别
ios学习之UISwipeGestureRecognizer手势识别 本文部分转自俺是一个瓜娃!!!的博客UISwipeGestureRecognizer ---手指动作,转载过来仅是为了自己查询 ...
- iOS学习笔记-精华整理
iOS学习笔记总结整理 一.内存管理情况 1- autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段 落,开始 ...
- iOS学习笔记总结整理
来源:http://mobile.51cto.com/iphone-386851_all.htm 学习IOS开发这对于一个初学者来说,是一件非常挠头的事情.其实学习IOS开发无外乎平时的积累与总结.下 ...
- ios学习-delegate、传值、跳转页面
ios学习-delegate.传值.跳转页面 1.打开xcode,然后选择ios--Application--Empty Application一个空项目. 项目目录: 2.输入项目名称以及选 ...
- iOS学习——UIView的研究
在iOS开发中,我们知道有一个共同的基类——NSObject,但是对于界面视图而言,UIView是非常重要的一个类,UIView是很多视图控件的基类,因此,对于UIView的学习闲的非常有必要.在iO ...
- 2015最新iOS学习线路图
iOS是由苹果公司开发的移动操作系统,以xcode为主要开发工具,具有简单易用的界面.令人惊叹的功能,以及超强的稳定性,已经成为iPhone.iPad 和iPod touch 的强大基础:iOS 内置 ...
- iOS学习——iOS原生实现二维码扫描
最近项目上需要开发扫描二维码进行签到的功能,主要用于开会签到的场景,所以为了避免作弊,我们再开发时只采用直接扫描的方式,并且要屏蔽从相册读取图片,此外还在二维码扫描成功签到时后台会自动上传用户的当前地 ...
随机推荐
- 局域网网络性能測试方法HDtune 64K有缓存測速法,让你得知你的网络性能
该方法能够有效測试出您的网络传输性能究竟有多高,该方法通用于有盘,无盘(系统虚拟盘) ,游戏虚拟盘,以及其它带有缓存功能的虚拟盘软件,可是由于每款软件的工作方式和原理都不仅同样,所以每款软件的測试结果 ...
- 【POJ 3273】 Monthly Expense (二分)
[POJ 3273] Monthly Expense (二分) 一个农民有块地 他列了个计划表 每天要花多少钱管理 但他想用m个月来管理 就想把这个计划表切割成m个月来完毕 想知道每一个月最少花费多少 ...
- 【Java集合源代码剖析】LinkedList源代码剖析
转载请注明出处:http://blog.csdn.net/ns_code/article/details/35787253 您好.我正在參加CSDN博文大赛.假设您喜欢我的文章,希望您能帮我投一票,谢 ...
- UVA 1149 Bin Packing 二分+贪心
A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the samele ...
- nyoj--74--小学生算术(水)
小学生算术 时间限制:3000 ms | 内存限制:65535 KB 难度:1 描述 很多小学生在学习加法时,发现"进位"特别容易出错.你的任务是计算两个三位数在相加时需要多少 ...
- python中类的定义、实例化、封装以及私有变量/方法
1. 定义类 python中定义一个类的格式如下: class MyClass(object): def __init__(self,data1,data2): self.__data1=data1 ...
- 如何用php实现qq登陆网站
PHP网站入QQ互联,使用QQ号码登录网站. 平台接口系列文章 PHP网站入QQ互联,使用QQ号码登录网站 PHP网站接入人人网,授权登陆 php facebook api网站接入facebook 1 ...
- vue-quill-editor 禁止编辑
每天学习一点点,知识财富涨点点 因为权限问题需要对富文本vue-quill-editor进行禁止编辑,因为也不是专业前端,处理起问题来还是只有看文档和百度,发现对这个提问不是很多,可能很多大牛都不会出 ...
- POJ 1414 暴搜
题意比较复杂 (但是很好理解) 大概意思是给你等边三角形(详见题目中的图). 最后一行有n个数,下一次要填的数是c. 里面预先已经填好了数字.(0为未填) 得分的标准是这个分数的连通块周围没有空的地方 ...
- 3d touch 的使用(一)
废话不多说,直接上代码------------------ 在 - (BOOL)application:(UIApplication *)application didFinishLaunchingW ...