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 命名管道文件. - 普通文件
随机推荐
- jquery实现DIV层拖动
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- C++explicit关键字
在C++中,explicit关键字用来修饰类的构造函数,被修饰的构造函数的类,不能发生相应的隐式类型转换,只能以显示的方式进行类型转换. explicit使用注意事项: * explicit ...
- iOS开发——C篇&数组与指针
2015-07-17 13:23 编辑 前面我们介绍了关于C语言的内存分配问题,下面我们就开始介绍关于C语言的两个非常重要的知识点:数组与指针 数组与指针其实不仅仅是再C语言中,再OC中(当然OC是内 ...
- JS之路——数组对象
String字符串对象 concat() 返回一个由两个数组合并组成的新数组 join() 返回一个由数组中的所有元素连接在一起的String对象 pop() 删除数组中最后一个元素 并返回该值 pu ...
- The executable was signed with invalid entitlements.
如图,出现这个的原因是 配置文件(provisioning profile)和 app 授权文件中的 entitlements(授权) 不匹配 具体应该从 配置文件 和证书的对应 问 ...
- Linux定时运行与开机运行任务
http://os.51cto.com/art/200805/75144.htm at命令与crontab命令 http://os.51cto.com/art/201007/211874.htm ht ...
- 【HDOJ】4982 Goffi and Squary Partition
题意就是整数划分,选出和为n的K个整数,其中K-1个数的和为完全平方数S.选择整数时需要从1,2,3..连续选择,当选择整数与n-S相等时,需要跳过n-S,即选择n-S+1.如此选择K-2个数,从而可 ...
- Stm32 定时器 定时时间设置及PWM频率 占空比的设置总结
一.定时器的时钟: 当SYSCLK等于72M,APB1等于36M APB2等于72M时,定时器的时钟为72M.注意图中这句话:如果APB1/APB2预分频器=1则频率不变,否则频率x2.如果此时,AP ...
- JSP学习笔记(一):JDK的安装及环境变量的配置
一.JDK的安装. JDK可以在Oracle(甲骨文)的官网下载,连接地址:http://www.oracle.com/technetwork/java/javase/downloads/index- ...
- java笔记9之switch
switch语句的注意事项: A:case后面只能是常量,不能是变量,而且,多个case后面的值不能出现相同的 B:default可以省略吗? 可以省 ...