iOS学习笔记-068.手势识别02——捏合、拖拽、旋转手势
- 手势识别02捏合拖拽旋转手势
- 一捏合手势
- 1 UIPinchGestureRecognizerh
- 2 代码示例
- 3 图示
- 二拖拽手势
- 1 UIPanGestureRecognizerh
- 2 代码示例
- 3 图示
- 三旋转
- 1
- 2 代码示例
- 3 图示
- 四缩放拖拽旋转三个手势同时使用
- 1 是否支持多个手势的方法
- 2 示例代码
- 3 图示
- 一捏合手势
手势识别02——捏合、拖拽、旋转手势
一、捏合手势
捏合手势使用的是 UIPinchGestureRecognizer
1.1 UIPinchGestureRecognizer.h
#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIGestureRecognizer.h>NS_ASSUME_NONNULL_BEGINNS_CLASS_AVAILABLE_IOS(3_2) __TVOS_PROHIBITED @interface UIPinchGestureRecognizer : UIGestureRecognizer@property (nonatomic) CGFloat scale; //缩放的比例
@property (nonatomic,readonly) CGFloat velocity; // velocity of the pinch in scale/second@endNS_ASSUME_NONNULL_END
1.2 代码示例
/====================捏合手势=======================-(void)createPinGesture{//创建手势UIPinchGestureRecognizer * pinGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinGesture:)];//添加手势[_imageView addGestureRecognizer:pinGesture];
}-(void)pinGesture:(UIPinchGestureRecognizer*)pinGesture{//缩放_imageView.transform = CGAffineTransformScale(_imageView.transform, pinGesture.scale, pinGesture.scale);//复位pinGesture.scale = 1;
}//===========================================
1.3 图示
二、拖拽手势
捏合手势使用的是 UIPanGestureRecognizer
2.1 UIPanGestureRecognizer.h
//
// UIPanGestureRecognizer.h
// UIKit
//
// Copyright (c) 2008-2016 Apple Inc. All rights reserved.
//#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIGestureRecognizer.h>NS_ASSUME_NONNULL_BEGINNS_CLASS_AVAILABLE_IOS(3_2) @interface UIPanGestureRecognizer : UIGestureRecognizer @property (nonatomic) NSUInteger minimumNumberOfTouches __TVOS_PROHIBITED;
@property (nonatomic) NSUInteger maximumNumberOfTouches __TVOS_PROHIBITED; - (CGPoint)translationInView:(nullable UIView *)view; //获取移动的偏移量
- (void)setTranslation:(CGPoint)translation inView:(nullable UIView *)view;- (CGPoint)velocityInView:(nullable UIView *)view; @endNS_ASSUME_NONNULL_END
2.2 代码示例
/====================拖拽手势=======================-(void)createPanGesture{//创建手势UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)];//添加手势[_imageView addGestureRecognizer:pan];
}-(void)panGesture:(UIPanGestureRecognizer*)pan{//获取移动的偏移量CGPoint point = [pan translationInView:_imageView];_imageView.transform = CGAffineTransformTranslate(_imageView.transform, point.x, point.y);
// //复位[pan setTranslation:CGPointZero inView:_imageView];
}//===============================================
2.3 图示
三、旋转
3.1
#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIGestureRecognizer.h>NS_ASSUME_NONNULL_BEGINNS_CLASS_AVAILABLE_IOS(3_2) __TVOS_PROHIBITED @interface UIRotationGestureRecognizer : UIGestureRecognizer@property (nonatomic) CGFloat rotation; //偏移弧度
@property (nonatomic,readonly) CGFloat velocity; @endNS_ASSUME_NONNULL_END
3.2 代码示例
//=======================旋转手势========================
-(void)createRotationGesture{UIRotationGestureRecognizer * rota = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGesture:)];[_imageView addGestureRecognizer:rota];
}-(void)rotationGesture:(UIRotationGestureRecognizer*)rotation{_imageView.transform = CGAffineTransformRotate(_imageView.transform, rotation.rotation);//弧度rotation.rotation = 0;
}
3.3 图示
四、缩放、拖拽、旋转三个手势同时使用
4.1 是否支持多个手势的方法
// 是否同时支持多个手势
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{return YES;
}
4.2 示例代码
//
// ViewController.m
// 03_UIView61_手势识别2
//
// Created by 杞文明 on 17/3/31.
// Copyright © 2017年 杞文明. All rights reserved.
//#import "ViewController.h"@interface ViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];_imageView.userInteractionEnabled = YES;[self createPinGesture];[self createPanGesture];[self createRotationGesture];
}// 是否同时支持多个手势
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{return YES;
}//====================捏合手势=======================-(void)createPinGesture{//创建手势UIPinchGestureRecognizer * pinGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinGesture:)];pinGesture.delegate = self;//添加手势[_imageView addGestureRecognizer:pinGesture];
}-(void)pinGesture:(UIPinchGestureRecognizer*)pinGesture{//缩放_imageView.transform = CGAffineTransformScale(_imageView.transform, pinGesture.scale, pinGesture.scale);//复位pinGesture.scale = 1;
}//===========================================//====================拖拽手势=======================-(void)createPanGesture{//创建手势UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)];pan.delegate = self;//添加手势[_imageView addGestureRecognizer:pan];
}-(void)panGesture:(UIPanGestureRecognizer*)pan{//获取移动的偏移量CGPoint point = [pan translationInView:_imageView];_imageView.transform = CGAffineTransformTranslate(_imageView.transform, point.x, point.y);
// //复位[pan setTranslation:CGPointZero inView:_imageView];
}//===============================================//=======================旋转手势========================
-(void)createRotationGesture{UIRotationGestureRecognizer * rota = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGesture:)];rota.delegate = self;[_imageView addGestureRecognizer:rota];
}-(void)rotationGesture:(UIRotationGestureRecognizer*)rotation{_imageView.transform = CGAffineTransformRotate(_imageView.transform, rotation.rotation);//弧度rotation.rotation = 0;
}//===============================================@end
4.3 图示
iOS学习笔记-068.手势识别02——捏合、拖拽、旋转手势相关推荐
- IOS学习笔记 ---- 15/09/02
1.三种方式监听方式: addTarget方法: 使用代理方法: [[NSNotificationCenter defaultCenter] addObserver]方法监听通知: 2.在storyb ...
- iOS学习笔记-自己动手写RESideMenu
代码地址如下: http://www.demodashi.com/demo/11683.html 很多app都实现了类似RESideMenu的效果,RESideMenu是Github上面一个stars ...
- iOS学习笔记-地图MapKit入门
代码地址如下: http://www.demodashi.com/demo/11682.html 这篇文章还是翻译自raywenderlich,用Objective-C改写了代码.没有逐字翻译,如有错 ...
- IOS学习笔记39——拍照、从相册选图并对图片进行裁剪
2013第一篇,大家新年快乐!感谢一直关注我博客的同学们,有你们的支持我才有动力越做越好!有阵子没写博客了,因为前阵子着实比较忙,没时间整理,今天主要实现一个小Demo,我们知道在Instagram或 ...
- OpenCV for Ios 学习笔记(4)-标记检测1
本文原始地址:OpenCV for Ios 学习笔记(4)-标记检测1 简单的标记经常是以白色块和黑色块构成的规则图形.因为我们预先知道这些因素,所以我们可以很容易检测标记. 如图: 首先,我们需要找 ...
- IOS学习笔记(九)之UIAlertView(警告视图)和UIActionSheet(操作表视图)基本概念和使用方法...
IOS学习笔记(九)之UIAlertView(警告视图)和UIActionSheet(操作表视图)基本概念和使用方法 Author:hmjiangqq Email:jiangqqlmj@163.com ...
- IOS学习笔记(四)之UITextField和UITextView控件学习
IOS学习笔记(四)之UITextField和UITextView控件学习(博客地址:http://blog.csdn.net/developer_jiangqq) Author:hmjiangqq ...
- iOS学习笔记-自定义过渡动画
代码地址如下: http://www.demodashi.com/demo/11678.html 这篇笔记翻译自raywenderlick网站的过渡动画的一篇文章,原文用的swift,由于考虑到swi ...
- IOS学习笔记07---C语言函数-scanf函数
2013/8/7 IOS学习笔记07---C语言函数-scanf函数 ------------------------------ qq交流群:创梦技术交流群:251572072 ...
最新文章
- 个人计算机中的cache,cache在计算机中的作用是什么?
- 归并排序-python
- 解决pycharm输入法不跟随的方法
- zcmu-1957(状态压缩枚举)
- 转android项目开发 工作日志 2011.10.8--bundle类使用
- java方法、方法重载
- 不容易,今天的成果。
- 首款“印度制造”的微处理器 AJIT 面世!
- Nginx设置expires设定页面缓存时间
- VS2012 的快捷键使用
- 怎么获取计算机用户权限,如何获取电脑的最高管理权限|细讲电脑最高管理权限的获取方式...
- [I T]深圳唯冠天价律师费遭起诉 补偿金由法院暂管理
- mysql 修改列格式化_prettytable:像数据库一样格式化输出内容
- php输入为空,ecshop搜索框内容为空提示用户输入内容
- 五 IP核行业潜在投资方向和机会
- jQuery 选择器 _ 菜鸟教程
- 芝麻任务,快速获得小程序用户
- 被动信息收集(一)nslookup.dig.DNS字典爆破.whois
- 2小时上车AI作画_NovelAI (学会能做游戏mod)
- 关于字符编码,中文所占字节的整理。