IOS 中的手势
来源:互联网 发布:重庆大学电气工程 知乎 编辑:程序博客网 时间:2024/05/16 05:55
// Copyright (c) 2015年 zhangxueke. All rights reserved.//#import "RootViewController.h"#import "UIColor+Creat.h"@interface RootViewController ()@end@implementation RootViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)]; redView.backgroundColor = [UIColor redColor]; [self.view addSubview:redView]; [redView release]; //手势识别器的基类:UIGestureRecognizer //轻拍手势 { //1. 创建轻拍对象 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapGesture:)]; //设置轻拍的次数 //tapGesture.numberOfTapsRequired = 1; //设置手指的个数 //tapGesture.numberOfTouchesRequired = 1; //2. 将手式添加到视图上 [redView addGestureRecognizer:tapGesture]; //3. 释放手式 [tapGesture release]; } //长按手势 { //1.创建长按手势对象 UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)]; //设置长按手势的识别时间 longGesture.minimumPressDuration = 1; //2. 添加到视图 [redView addGestureRecognizer:longGesture]; //3. 释放 [longGesture release]; } //轻扫手势 { //1.创建对象 UISwipeGestureRecognizer *leftSwipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)]; //2. 添加到视图 [redView addGestureRecognizer:leftSwipeGesture]; //3. 释放 [leftSwipeGesture release]; //1.创建对象 UISwipeGestureRecognizer *rightSwipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)]; //设置轻扫方向 rightSwipeGesture.direction = UISwipeGestureRecognizerDirectionLeft; //2. 添加到视图 [redView addGestureRecognizer:rightSwipeGesture]; //3. 释放 [rightSwipeGesture release]; } //平移手势 { //1. 创建平移手势 UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanGesture:)]; //2. 添加手势到视图 [redView addGestureRecognizer:panGesture]; //3. 释放 [panGesture release]; } //捏和手式 { //1.创建捏合手指 UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinchGesture:)]; //添加到视图 [redView addGestureRecognizer:pinchGesture]; //释放手势 [pinchGesture release]; } //旋转手势 { //1.创建旋转手势 UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotationGesture:)]; //2.将旋转手势添加到视图 [redView addGestureRecognizer:rotationGesture]; //3. 释放手势 [rotationGesture release]; } //屏幕边缘平移手势 { //1.创建对象 UIScreenEdgePanGestureRecognizer *screenEdgePanGesture = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(handleScreenEdgePanGesture:)]; //设置平移的屏幕边界 //screenEdgePanGesture.edges = UIRectEdgeLeft;//从左边界开始平移 screenEdgePanGesture.edges = UIRectEdgeRight; //2. 添加到视图 [redView addGestureRecognizer:screenEdgePanGesture]; //3. 释放 [screenEdgePanGesture release]; } }#pragma mark--handle gesture action//处理屏幕边缘手势- (void)handleScreenEdgePanGesture:(UIScreenEdgePanGestureRecognizer *)screenEdgePanGesture { //1. 获取平移增量 CGPoint point = [screenEdgePanGesture translationInView:screenEdgePanGesture.view]; //2. 平移视图 screenEdgePanGesture.view.transform =CGAffineTransformTranslate(screenEdgePanGesture.view.transform, point.x, 0); //3. 将之前增量清零 [screenEdgePanGesture setTranslation:CGPointZero inView:screenEdgePanGesture.view]; }//处理旋转手势- (void)handleRotationGesture:(UIRotationGestureRecognizer *)rotationGesture { rotationGesture.view.transform = CGAffineTransformRotate(rotationGesture.view.transform, rotationGesture.rotation); rotationGesture.rotation = 0.0; }//处理捏合手势- (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinchGesture { pinchGesture.view.transform = CGAffineTransformMakeScale(pinchGesture.scale, pinchGesture.scale); pinchGesture.view.transform = CGAffineTransformScale(pinchGesture.view.transform, pinchGesture.scale, pinchGesture.scale); //将之前的缩放比例置1 pinchGesture.scale = 1;}//处理平移手势- (void)handlePanGesture:(UIPanGestureRecognizer *)panGesture { //1. 获取平移增量 CGPoint point = [panGesture translationInView:panGesture.view]; //2. 改变视图的位置 panGesture.view.center = CGPointMake(panGesture.view.center.x + point.x, panGesture.view.center.y + point.y); //3. 将之前的增量清零 [panGesture setTranslation:CGPointZero inView:panGesture.view]; // //仿射变换// //1.获取平移增量// CGPoint point = [panGesture translationInView:panGesture.view];// //2. 修改视图的transform// //移的很快// panGesture.view.transform = CGAffineTransformMakeScale(point.x, point.y);// //正常移动// panGesture.view.transform = CGAffineTransformTranslate(panGesture.view.transform, point.x, point.y);// //不用将增量清零,每次移动都会回到最初为止// panGesture.view.transform = CGAffineTransformMakeTranslation(point.x, point.y);// //3. 将之前的增量清零// [panGesture setTranslation:CGPointZero inView:panGesture.view]; }//处理轻扫手势- (void)handleSwipeGesture:(UISwipeGestureRecognizer *)swipeGesture { if (swipeGesture.direction == UISwipeGestureRecognizerDirectionRight) { //当识别为向右扫时,改变自己颜色 swipeGesture.view.backgroundColor = [UIColor randomColor]; } if (swipeGesture.direction == UISwipeGestureRecognizerDirectionLeft) { swipeGesture.view.superview.backgroundColor = [UIColor randomColor]; }}//处理长按手势- (void)handleLongPress:(UILongPressGestureRecognizer *)longGesture { //当手势识别器识别为长按时,触发长按改变颜色 if (longGesture.state == UIGestureRecognizerStateBegan) { longGesture.view.backgroundColor = [UIColor randomColor]; } }//处理轻拍手势- (void)handleTapGesture:(UITapGestureRecognizer *)tapGesture { tapGesture.view.backgroundColor = [UIColor randomColor];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}
0 0
- IOS中的六大手势
- iOS中的手势
- IOS中的手势详解
- iOS中的七大手势
- iOS中的各种手势
- IOS 中的手势
- ios中的手势
- iOS中的手势详解
- ios中的6种手势
- ios开发中的手势添加
- iOS中的事件,手势识别
- iOS开发中的手势添加
- iOS开发中的手势总结
- iOS开发中的手势触发
- 关于iOS中的手势识别
- 总结iOS开发中的手势识别
- 谈谈iOS中的手势及触摸
- iOS中的触摸事件和手势处理
- 多线程学习--简单的线程池创建
- gre备考五大要点【zhasite】
- 爬虫-利用urllib爬去网页增加user_agent和proxy(Python)
- unix的指令
- 2015-11-16亲测58可用httpwebrequest模拟登陆方法
- IOS 中的手势
- iOS:GIF图片的预览以及生成
- 内联函数与宏定义的区别举例
- ubuntu14.04,安装Eclipse(开源开发工具)
- Linux中使用正则表达式
- android 配置ndk
- 使用crontab定时执行shell脚本
- 【Android】 R.java详解
- 多线程学习--多线程中Lock的用法