//
// 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中的七种手势的更多相关文章

  1. IOS的七种手势

    今天为大家介绍一下IOS 的七种手势,手势在开发中经常用到,所以就简单 通俗易懂的说下, 话不多说,直接看代码: // 初始化一个UIimageView UIImageView *imageView ...

  2. AOP在 .NET中的七种实现方法

    7Approaches for AOP in .Net AOP在 .NET中的七种实现方法 Here are all the ways that I can think of to add AOPto ...

  3. iOS七种手势

    // 初始化一个UIimageView UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, ...

  4. C语言中的七种排序算法

    堆排序: void HeapAdjust(int *arraydata,int rootnode,int len) { int j; int t; *rootnode+<len) { j=*ro ...

  5. 快速理解Java中的七种单例模式

    饿汉式(推荐) package concurencyv2.chapter1; public class SingletonV2 { private static final SingletonV2 i ...

  6. iOS 开发的几种手势

    今天为大家介绍一下IOS 的七种手势,手势在开发中经常用到,所以就简单 通俗易懂的说下, 话不多说,直接看代码: // 初始化一个UIimageView UIImageView *imageView ...

  7. 面试官的七种武器:Java篇

    起源 自己经历过的面试也不少了,互联网的.外企的,都有.总结一下这些面试的经验,发现面试官问的问题其实不外乎几个大类,玩不出太多新鲜玩意的.细细想来,面试官拥有以下七种武器.恰似古龙先生笔下的武侠世界 ...

  8. Hibernate的七种映射关系之七种关联映射(二)

    继续上篇博客 七.Hibernate双向一对多关联映射:让多的一端来维护关系. 主要是解决一对多单向关联的缺陷,而不是需求驱动的. 1.在Student.java实体类里添加Classes引用.pri ...

  9. 【Linux】七种文件类型

    Linux中的七种文件类型 d 目录文件. l 符号链接(指向另一个文件). s 套接字文件. b 块设备文件,二进制文件. c 字符设备文件. p 命名管道文件. - 普通文件

随机推荐

  1. Ant快速入门(四)-----Ant的任务(Task)

    到目前为止,我们已经掌握了Ant生成文件的基本结构,以及<project.../>,<target.../>,<property.../>等元素的配置方式.而< ...

  2. STM32库中 __IO 修饰符(volatile修饰符)

    STM32例子代码中会有像这样的代码 static __IO uint32_t TimingDelay; 这里边的__IO修饰符不好理解,单从字面可以看出是为IO相关,查其标准库可以得知这个__IO原 ...

  3. library cache lock

    SESSION 34 执行存储过程: SESSION 43 编译存储过程: SESSION 25 删除存储过程: 1.查询查看library cache lock等待事件的相关会话 SQL> s ...

  4. -_-#【Angular】工具函数

    AngularJS学习笔记 上下文绑定 var f = angular.bind({a: 'xx'}, function() { console.log(this.a) }) f() // 'xx' ...

  5. 【动态规划】XMU 1560 新ACM规则

    题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1560 题目大意: 给定n(n<=200)个任务及每个任务的耗时,问m(m< ...

  6. Gas Station——LeetCode

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  7. UVa 11729 - Commando War(贪心)

    "Waiting for orders we held in the wood, word from the front never came By evening the sound of ...

  8. Java学习日记-4 StringBuffer类和数组

    一.StringBuffer类 StringBuffer是一个可变字符序列. 1.1 构造函数 StringBuffer() 构造一个不带字符的字符缓冲区,初始容量为16个字符. StringBuff ...

  9. Java Socket 编程指南

    Socket,又称为套接字,Socket是计算机网络通信的基本的技术之一.如今大多数基于网络的软件,如浏览器,即时通讯工具甚至是P2P下载都是基于Socket实现的.本文会介绍一下基于TCP/IP的S ...

  10. Java中的不可变类

    概念:不可变类的意思是创建该类的实例后,该实例的属性是不可改变的.java中的8个包装类和String类都是不可变类.所以不可变类并不是指该类是被final修饰的,而是指该类的属性是被final修饰的 ...