UI中的七种手势
//
// GestureRecognizerViewController.m #import "GestureRecognizerViewController.h"
#import "UIColor+RandomColor.h"
@interface GestureRecognizerViewController ()
{ CGRect _frame; // 用来记录view原来的frame }
@end @implementation GestureRecognizerViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// UIGestureRecognizer 手势识别器,是所有手势识别类的基类,提供了手势识别器的基本功能,有了手势识别器之后,手势的识别全部由这个类来识别,我们就不再关心手势识别的过程,我们只需要关心手势识别之后应该做哪些操作,它的子类有6个,轻拍手势,捏合手势,长按手势,轻扫手势,旋转手势,平移手势,以及平移手势的子类 屏幕边缘手势 UIView *view = [[UIView alloc]initWithFrame:(CGRectMake(, , , ))];
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];
[view release]; //! 轻拍手势 这种手势用的最多, 跟按钮似的 // UITapGestureRecognizer
/*
// 1. 创建轻拍手势对象
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)]; // self指视图控制器的对象 // 2. 设置轻拍触发方法时,需要的点击次数 // 一定要在添加手势之前设置
tapGesture.numberOfTapsRequired = 2;
tapGesture.numberOfTouchesRequired = 2; // 3. 向视图对象上添加手势
[view addGestureRecognizer:tapGesture]; // 多态 父类指针指向子类对象
// [view addGestureRecognizer:<#(UIGestureRecognizer *)#>] [tapGesture release];
*/ /**
1. 创建手势识别类的对象(轻拍手势/ 长按手势/ 轻扫手势/ 平移手势/ 捏合手势/ 旋转手势/ 屏幕边缘手势) 2. 设置手势方法的相关属性(如需要几根手指,多长时间才能触发手势事件 等) // 根据需要 3. 向视图对象上添加手势 4. 释放手势对象 */ // 长按手势 UILongPressGestureRecognizer
/*
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)];
// 设置长按手势最短的触发事件 秒为单位
longPressGesture.minimumPressDuration = 1; [view addGestureRecognizer:longPressGesture]; [longPressGesture release];
*/ // 轻扫手势 UISwipeGestureRecognizer UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)]; // 设置轻扫手势支持的方法 (默认是向右扫)
// UISwipeGestureRecognizerDirectionDown 向下扫
swipeGesture.direction = UISwipeGestureRecognizerDirectionDown; // 一定要在添加手势之前设置 [view addGestureRecognizer:swipeGesture]; [swipeGesture release]; // 一个视图可以添加多个手势
// 想要实现多个方向轻扫手势: 再添加一个轻扫手势
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
// UISwipeGestureRecognizerDirectionLeft 向左轻扫
swipe.direction = UISwipeGestureRecognizerDirectionLeft; [view addGestureRecognizer:swipe];
[swipe release]; // 平移手势 UIPanGestureRecognizer UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesTure:)]; [view addGestureRecognizer:panGesture];
[panGesture release]; // 捏合手势 UIPinchGestureRecognizer UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)]; // pinchGesture.scale [view addGestureRecognizer:pinchGesture];
[pinchGesture release]; // 旋转手势 // UIRotationGestureRecognizer
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotationGesture:)];
[view addGestureRecognizer:rotationGesture];
[rotationGesture release]; // 屏幕边缘手势 UIScreenEdgePanGestureRecognizer 是UIPanGestureRecognizer的子类 UIScreenEdgePanGestureRecognizer *screenEdgePanGesture = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleScreenEdgeGesture:)]; // 设置屏幕边缘手势支持方法
screenEdgePanGesture.edges = UIRectEdgeRight;
// 必须让位置和屏幕边缘重合
view.frame = CGRectMake(, , , );
// 使每次移动后的view回到原始位置,实现在action方法中
_frame = view.frame; [view addGestureRecognizer:screenEdgePanGesture]; [screenEdgePanGesture release]; }
#pragma mark - 轻拍手势方法
- (void)handleTapGesture:(UITapGestureRecognizer *)tapGesture { // 获取轻拍手势所在的视图对象
tapGesture.view.backgroundColor = [UIColor randomColor]; } #pragma mark - 长按手势方法
- (void)handleLongPressGesture:(UILongPressGestureRecognizer *)longPressGesture { // UIGestureRecognizerStateBegan 当达到条件((longPressGesture.minimumPressDuration = 1;))界限时,触发方法事件
// UIGestureRecognizerStateChanged 当达到条件时,滑动,触发
// UIGestureRecognizerStateEnded 当达到条件,手指离开,触发 // 根据长按手势的状态执行
if (longPressGesture.state == UIGestureRecognizerStateEnded) { longPressGesture.view.superview.backgroundColor = [UIColor randomColor]; } } #pragma mark - 轻扫手势方法
- (void)handleSwipeGesture:(UISwipeGestureRecognizer *)swipeGesture { swipeGesture.view.backgroundColor = [UIColor randomColor]; } - (void)handleSwipe:(UISwipeGestureRecognizer *)swipe { swipe.view.backgroundColor = [UIColor randomColor];
} #pragma mark - 平移手势方法
- (void)handlePanGesTure:(UIPanGestureRecognizer *)panGesture { // 1. 获取平移增量
CGPoint point = [panGesture translationInView:panGesture.view];
// 2. 让视图的位置发生移动,以上次视图为基准,transform 仿射变换技术 (view上所有点跟随移动) 用了线性代数的知识
panGesture.view.transform = CGAffineTransformTranslate(panGesture.view.transform, point.x, point.y);
//3. 将上次的平移增量置为0
//CGPointZero 代表一个(0, 0)的结构体 CGPointMake(0, 0)
[panGesture setTranslation:CGPointZero inView:panGesture.view]; panGesture.view.backgroundColor = [UIColor randomColor]; } #pragma mark - 捏合手势方法
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinchGesture { // 1.根据比例做放射变换, 也是以上次视图为基准 Scale 比例
pinchGesture.view.transform = CGAffineTransformScale(pinchGesture.view.transform, pinchGesture.scale, pinchGesture.scale);
// 2.将上次的缩放比例置为1
pinchGesture.scale = ; // 或者[pinchGesture setScale:1]; //pinchGesture.view.backgroundColor = [UIColor randomColor]; } #pragma mark - 旋转手势方法
- (void)handleRotationGesture:(UIRotationGestureRecognizer *)rotationGesture {
// 1. 根据旋转角度做放射变换,也是以上次视图形变量为基准 rotation 旋转,旋度
rotationGesture.view.transform = CGAffineTransformRotate(rotationGesture.view.transform, rotationGesture.rotation);
// 2. 将上次的旋转角度清零
rotationGesture.rotation = ; } #pragma mark - 边缘手势方法
// 方法和UIPanGestureRecognizer(平移手势)一样
- (void)handleScreenEdgeGesture:(UIScreenEdgePanGestureRecognizer *)screenEdgeGesture { // 手指离开的时候 ,view回到原来的位置
if (screenEdgeGesture.state == UIGestureRecognizerStateEnded) { screenEdgeGesture.view.frame = _frame;
} // 1.获取手指的平移增量
CGPoint point = [screenEdgeGesture translationInView:screenEdgeGesture.view];
// 2.根据平移增量做仿射变换
screenEdgeGesture.view.transform = CGAffineTransformTranslate(screenEdgeGesture.view.transform, point.x, point.y);
// 3. 把平移增量置为0的结构体
[screenEdgeGesture setTranslation:CGPointZero inView:screenEdgeGesture.view]; //screenEdgeGesture.view.frame = _frame;
//NSLog(@"触发了"); } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ @end
随机颜色
//
// UIColor+RandomColor.h
// UILessonTouch-04 #import <UIKit/UIKit.h> @interface UIColor (RandomColor)
+ (UIColor *)randomColor;
@end
//
// UIColor+RandomColor.m
// UILessonTouch-04 #import "UIColor+RandomColor.h"
#define kColorValue arc4random_uniform(256) / 255.0
@implementation UIColor (RandomColor) + (UIColor *)randomColor { return [UIColor colorWithRed:kColorValue green:kColorValue blue:kColorValue alpha:kColorValue]; } @end
UI中的七种手势的更多相关文章
- IOS的七种手势
今天为大家介绍一下IOS 的七种手势,手势在开发中经常用到,所以就简单 通俗易懂的说下, 话不多说,直接看代码: // 初始化一个UIimageView UIImageView *imageView ...
- AOP在 .NET中的七种实现方法
7Approaches for AOP in .Net AOP在 .NET中的七种实现方法 Here are all the ways that I can think of to add AOPto ...
- iOS七种手势
// 初始化一个UIimageView UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, ...
- C语言中的七种排序算法
堆排序: void HeapAdjust(int *arraydata,int rootnode,int len) { int j; int t; *rootnode+<len) { j=*ro ...
- 快速理解Java中的七种单例模式
饿汉式(推荐) package concurencyv2.chapter1; public class SingletonV2 { private static final SingletonV2 i ...
- iOS 开发的几种手势
今天为大家介绍一下IOS 的七种手势,手势在开发中经常用到,所以就简单 通俗易懂的说下, 话不多说,直接看代码: // 初始化一个UIimageView UIImageView *imageView ...
- 面试官的七种武器:Java篇
起源 自己经历过的面试也不少了,互联网的.外企的,都有.总结一下这些面试的经验,发现面试官问的问题其实不外乎几个大类,玩不出太多新鲜玩意的.细细想来,面试官拥有以下七种武器.恰似古龙先生笔下的武侠世界 ...
- Hibernate的七种映射关系之七种关联映射(二)
继续上篇博客 七.Hibernate双向一对多关联映射:让多的一端来维护关系. 主要是解决一对多单向关联的缺陷,而不是需求驱动的. 1.在Student.java实体类里添加Classes引用.pri ...
- 【Linux】七种文件类型
Linux中的七种文件类型 d 目录文件. l 符号链接(指向另一个文件). s 套接字文件. b 块设备文件,二进制文件. c 字符设备文件. p 命名管道文件. - 普通文件
随机推荐
- 2016-12-14jq笔记
1.在jq中声明一个数组的方法有两种: 1.var a=new Array(): 2 var b=[]; (效果一致) 2.bind()和live()的区别 3.animate的用法 4.place ...
- jquery ajax用例样板
$.ajax({ url: '${managerPath}/customer/updateOrder.do', type: 'POST', async: false, data: { id: date ...
- hdu 5100 Chessboard
http://acm.hdu.edu.cn/showproblem.php?pid=5100 在比赛时没看懂题就没看,结束之后,看了解题报告才知道怎么做. 解题报告: 首先,若n<k,则棋盘连一 ...
- cf C. Hamburgers
http://codeforces.com/contest/371/problem/C 二分枚举最大汉堡包数量就可以. #include <cstdio> #include <cst ...
- HID燈是什么及其工作原理
HID為HighIntensityDischarge的縮寫,即高壓氣體放電燈,它發光的原理是將12V電壓增壓至23000V的超高電壓,激穿填充在石英管的氙氣,使它發光.然后再將電壓轉成85V左右,穩定 ...
- 自己动手做 UEStudio/UltraEdit 的语法高亮文件 (*.uew)
自己一直比较习惯用 UEStudio 来编写 C/C++ 文件,因为 Visual Studio 2010 实在太大了,我的 T400 都跑的费劲,所以一般我只用它来编译和调试.但是可惜的是 UESt ...
- Bitwise AND of Numbers Range——LeetCode
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- yum nfs
linux下3大文件共享方法 1.NFS NFS服务器配置 编辑/etc/exports,在文件中列出,要共享的目录.书写规则是:共享目录主机(参数).并且每条规则占据一行.例如: /mnt/mp3 ...
- A - Fire Net - hdu 1045(二分图匹配)
题意:一个阵地可以向四周扫射,求出来最多能修多少个阵地,墙不可以被扫射透,阵地不能同行或者或者列(有墙隔着例外) 分析:很久以前就做过这道题..当时是练习深搜来着,不过时间复杂度比较高,现在再看突然发 ...
- HTTP学习笔记4-请求与响应结构例子
18,HTTP消息由客户端到服务器的请求和服务器到客户端的响应组成.请求消息和响应消息都是由开始行,消息报头(可选的),空行(只有CRLF的行),消息正文(可选的)组成. 19,对于请求消息,开始行就 ...