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 命名管道文件. - 普通文件
随机推荐
- 栈的顺序存储方式的C语言实现
/* 编译器:Dev-c++ 5.4.0 文件名:stack.cpp 代码版本号:1.0 时间:2015-10-10 20:08:54 */ #include <stdio.h> #inc ...
- linux c数据库备份第四版
该版本算是比较成熟的啦,欢迎大伙拿来试用!!!1.新增数据库连接和备份时间配置文件conf2.新增日志文件,程序运行的一些异常会记录在log文件下 后续的工作:1.将代码切割为多个文件,分类存放代码2 ...
- Oracle归档日志定时删除任务
1.在Oracle账号下,创建归档日志删除文件del_arch.sh 文件位置:/home/oracle/crontabOra,内容如下: #!/bin/bash LOG_DIR=/home/orac ...
- Extjs4.1.x使用Application动态按需加载MVC各模块
我们知道Extjs4之后提出了MVC模块开发,将以前肥厚的js文件拆分成小的js模块[model\view\controller\store\form\data等],通过controller拼接黏合, ...
- 转:/etc/inittab文件的字段及其说明
/etc/inittab文件中每个登记项的结构都是一样的,共分为以冒号“:”分隔的4个字段.具体如下: identifier : run_level : action : pro ...
- UGUI穿透3D世界判断&&UGUI全事件监听
public bool isPointUI(){ PointerEventData eventDataCurrnt = new PointerEventData (EventSystem.curren ...
- VS2013程序打包部署(图解),vs2013部署
VS2013程序打包部署(图解),vs2013部署 首先要说明的是VS解决方案配置下的Debug模式和Release模式有什么区别.Debug模式通常称为调试模式,它包含调试信息,未对代码进行优化,方 ...
- mongose排序查询
Kc.find({bjid:req.params.bjid}).sort({'_id':1}).exec(function(err,kcs){ if(err){ res.json({no:0,msg: ...
- GitHub NetFlow
https://github.com/search?l=Java&p=1&q=netflow&ref=searchresults&type=Repositories&a ...
- Spring Assert.notNull
Exception in thread "main" java.lang.IllegalArgumentException: Source must not be null at ...