iOS开发之手势(UIGestureRecognizer)
来源:互联网 发布:儿童早教软件 编辑:程序博客网 时间:2024/06/04 19:57
//
// RootViewController.m
// LessonUIGestureRecognizer
//
// Created by 楠少轻狂 on 14-8-27.
// Copyright (c) 2014年 lanouhn. All rights reserved.
//
#import "RootViewController.h"
#import "UIColor+RandomColor.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
//手势识别器
//1.能够为系统提供的视图对象添加触摸事件响应方法(比如:UIVIew, UILabel, UIImageView等)
//2.内部封装了手势识别的过程,只需要把重心放到手势识别之后对应的操作上
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor greenColor];
// [self setupSubviews];
UIView * blueView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
blueView.backgroundColor = [UIColor blueColor];
blueView.tag = 100;
[self.view addSubview:blueView];
[blueView release];
UIButton * btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(20, 20, 60, 40);
btn.backgroundColor = [UIColor orangeColor];
[btn setTitle:@"滚回去" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[blueView addSubview:btn];
//UIGestureRecognizer是手势识别器的一个抽象的基类,只提供手势识别器的基本功能,使用时使用具体的子类(--7大子类)
//轻拍手势
// UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
// //设置轻拍的次数
// tapGesture.numberOfTapsRequired = 1;
// //设置轻拍时需要的手指的个数
// tapGesture.numberOfTouchesRequired = 2;
// //给视图添加轻拍手势
// [blueView addGestureRecognizer:tapGesture];
// [tapGesture release];
//长按手势
// UILongPressGestureRecognizer * longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)];
// //设置长按触发的最短时间
// longPressGesture.minimumPressDuration = 0.5;
//// longPressGesture.numberOfTapsRequired = 2;(一般不用的属性)
// [blueView addGestureRecognizer:longPressGesture];
// [longPressGesture release];
//清扫手势
// UISwipeGestureRecognizer * swipGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipGesture:)];
// //设置清扫手势清扫的方向,让左右方向都存在
// //只要枚举值中有左移运算符,代表枚举值可以通过按位或,让多个枚举值同时存在
// //warning:上下和左右方向在一个手势识别器中不能同时存在
// //如果想让上下左右四个方向都有,那只能创建两个清扫手势对象,一个支持上下,一个支持左右
// swipGesture.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
// [blueView addGestureRecognizer:swipGesture];
// [swipGesture release];
//平移手势
// UIPanGestureRecognizer * panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
// [blueView addGestureRecognizer:panGesture];
// [panGesture release];
//捏合手势
// UIPinchGestureRecognizer * pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)];
// [blueView addGestureRecognizer:pinchGesture];
// [pinchGesture release];
//旋转手势
// UIRotationGestureRecognizer * rotationgesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotationGesture:)];
// [blueView addGestureRecognizer:rotationgesture];
// [rotationgesture release];
//屏幕边缘手势
UIScreenEdgePanGestureRecognizer * screenEdgeGesture = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleScreenEdgeGesture:)];
screenEdgeGesture.edges = UIRectEdgeLeft;
[blueView addGestureRecognizer:screenEdgeGesture];
[screenEdgeGesture release];
}
- (void)btnClick:(UIButton *)btn
{
btn.superview.frame = [[UIScreen mainScreen] bounds];
}
- (void)setupSubviews
{
CGFloat y = 20;
for (int i = 0; i < 10; i++) {
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(20, y, 280, 50)];
view.backgroundColor = [UIColor orangeColor];
[self.view addSubview:view];
[view release];
y += 55;
//为循环创建的10view添加10个手势(10个人必须有10个苹果)
UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
[view addGestureRecognizer:tapGesture];
[tapGesture release];
}
}
//处理轻拍事件
- (void)handleTapGesture:(UITapGestureRecognizer *)tapGesture
{
tapGesture.view.backgroundColor = [UIColor randomColor];
}
//处理长按事件
- (void)handleLongPressGesture:(UILongPressGestureRecognizer *)longPressGesture
{
//当识别到长按手势时触发(也就是长按时间到达之后触发)
//在if语句中添加一个判断,可以避免松手后的触发
if (UIGestureRecognizerStateBegan == longPressGesture.state) {
longPressGesture.view.backgroundColor = [UIColor randomColor];
}
}
//处理清扫事件
- (void)handleSwipGesture:(UISwipeGestureRecognizer *)swipGesture
{
//手势识别器提供了一个手势添加在哪个视图上的方法
swipGesture.view.backgroundColor = [UIColor randomColor];
}
//处理平移手势
//使用transform处理平移方式
- (void)handlePanGesture:(UIPanGestureRecognizer *)panGesture
{
//获取平移前后的改变量
CGPoint point = [panGesture translationInView:panGesture.view];
panGesture.view.transform = CGAffineTransformTranslate(panGesture.view.transform, point.x, point.y);
//将之前增量清零(避免快速平移)
[panGesture setTranslation:CGPointZero inView:panGesture.view];
// panGesture.view.transform = CGAffineTransformMakeTranslation(point.x, point.y); //这个方法会导致第二次平移时回到最初的位置开始移动(以后不会再用)
}
//第一种处理平移方式
//- (void)handlePanGesture:(UIPanGestureRecognizer *)panGesture
//{
// //获取的是手指位置的改变量,存储在x以及y方向的该变量
// CGPoint point = [panGesture translationInView:panGesture.view];
// panGesture.view.center = CGPointMake(panGesture.view.center.x + point.x, panGesture.view.center.y + point.y);
// //将之前的增量清0
// //系统提供的设置之前的增量
// [panGesture setTranslation:CGPointZero inView:panGesture.view];
//}
//处理缩放手势
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinchGesture
{
pinchGesture.view.transform = CGAffineTransformScale(pinchGesture.view.transform, pinchGesture.scale, pinchGesture.scale);
//将之前变化的比例置1
// [pinchGesture setScale:1.0]; //同下
pinchGesture.scale = 1.0;
}
//处理旋转手势
- (void)handleRotationGesture:(UIRotationGestureRecognizer *)rotationGesture
{
rotationGesture.view.transform = CGAffineTransformRotate(rotationGesture.view.transform, rotationGesture.rotation);
//将之前的该变量清0
rotationGesture.rotation = 0;
}
//处理屏幕边缘手势
- (void)handleScreenEdgeGesture:(UIScreenEdgePanGestureRecognizer *)screenEdgePanGesture
{
CGPoint point = [screenEdgePanGesture translationInView:screenEdgePanGesture.view];
screenEdgePanGesture.view.transform = CGAffineTransformTranslate(screenEdgePanGesture.view.transform, point.x, 0);
[screenEdgePanGesture setTranslation:CGPointZero inView:screenEdgePanGesture.view];
}
- (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
// RootViewController.m
// LessonUIGestureRecognizer
//
// Created by 楠少轻狂 on 14-8-27.
// Copyright (c) 2014年 lanouhn. All rights reserved.
//
#import "RootViewController.h"
#import "UIColor+RandomColor.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
//手势识别器
//1.能够为系统提供的视图对象添加触摸事件响应方法(比如:UIVIew, UILabel, UIImageView等)
//2.内部封装了手势识别的过程,只需要把重心放到手势识别之后对应的操作上
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor greenColor];
// [self setupSubviews];
UIView * blueView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
blueView.backgroundColor = [UIColor blueColor];
blueView.tag = 100;
[self.view addSubview:blueView];
[blueView release];
UIButton * btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(20, 20, 60, 40);
btn.backgroundColor = [UIColor orangeColor];
[btn setTitle:@"滚回去" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[blueView addSubview:btn];
//UIGestureRecognizer是手势识别器的一个抽象的基类,只提供手势识别器的基本功能,使用时使用具体的子类(--7大子类)
//轻拍手势
// UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
// //设置轻拍的次数
// tapGesture.numberOfTapsRequired = 1;
// //设置轻拍时需要的手指的个数
// tapGesture.numberOfTouchesRequired = 2;
// //给视图添加轻拍手势
// [blueView addGestureRecognizer:tapGesture];
// [tapGesture release];
//长按手势
// UILongPressGestureRecognizer * longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)];
// //设置长按触发的最短时间
// longPressGesture.minimumPressDuration = 0.5;
//// longPressGesture.numberOfTapsRequired = 2;(一般不用的属性)
// [blueView addGestureRecognizer:longPressGesture];
// [longPressGesture release];
//清扫手势
// UISwipeGestureRecognizer * swipGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipGesture:)];
// //设置清扫手势清扫的方向,让左右方向都存在
// //只要枚举值中有左移运算符,代表枚举值可以通过按位或,让多个枚举值同时存在
// //warning:上下和左右方向在一个手势识别器中不能同时存在
// //如果想让上下左右四个方向都有,那只能创建两个清扫手势对象,一个支持上下,一个支持左右
// swipGesture.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
// [blueView addGestureRecognizer:swipGesture];
// [swipGesture release];
//平移手势
// UIPanGestureRecognizer * panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
// [blueView addGestureRecognizer:panGesture];
// [panGesture release];
//捏合手势
// UIPinchGestureRecognizer * pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)];
// [blueView addGestureRecognizer:pinchGesture];
// [pinchGesture release];
//旋转手势
// UIRotationGestureRecognizer * rotationgesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotationGesture:)];
// [blueView addGestureRecognizer:rotationgesture];
// [rotationgesture release];
//屏幕边缘手势
UIScreenEdgePanGestureRecognizer * screenEdgeGesture = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleScreenEdgeGesture:)];
screenEdgeGesture.edges = UIRectEdgeLeft;
[blueView addGestureRecognizer:screenEdgeGesture];
[screenEdgeGesture release];
}
- (void)btnClick:(UIButton *)btn
{
btn.superview.frame = [[UIScreen mainScreen] bounds];
}
- (void)setupSubviews
{
CGFloat y = 20;
for (int i = 0; i < 10; i++) {
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(20, y, 280, 50)];
view.backgroundColor = [UIColor orangeColor];
[self.view addSubview:view];
[view release];
y += 55;
//为循环创建的10view添加10个手势(10个人必须有10个苹果)
UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
[view addGestureRecognizer:tapGesture];
[tapGesture release];
}
}
//处理轻拍事件
- (void)handleTapGesture:(UITapGestureRecognizer *)tapGesture
{
tapGesture.view.backgroundColor = [UIColor randomColor];
}
//处理长按事件
- (void)handleLongPressGesture:(UILongPressGestureRecognizer *)longPressGesture
{
//当识别到长按手势时触发(也就是长按时间到达之后触发)
//在if语句中添加一个判断,可以避免松手后的触发
if (UIGestureRecognizerStateBegan == longPressGesture.state) {
longPressGesture.view.backgroundColor = [UIColor randomColor];
}
}
//处理清扫事件
- (void)handleSwipGesture:(UISwipeGestureRecognizer *)swipGesture
{
//手势识别器提供了一个手势添加在哪个视图上的方法
swipGesture.view.backgroundColor = [UIColor randomColor];
}
//处理平移手势
//使用transform处理平移方式
- (void)handlePanGesture:(UIPanGestureRecognizer *)panGesture
{
//获取平移前后的改变量
CGPoint point = [panGesture translationInView:panGesture.view];
panGesture.view.transform = CGAffineTransformTranslate(panGesture.view.transform, point.x, point.y);
//将之前增量清零(避免快速平移)
[panGesture setTranslation:CGPointZero inView:panGesture.view];
// panGesture.view.transform = CGAffineTransformMakeTranslation(point.x, point.y); //这个方法会导致第二次平移时回到最初的位置开始移动(以后不会再用)
}
//第一种处理平移方式
//- (void)handlePanGesture:(UIPanGestureRecognizer *)panGesture
//{
// //获取的是手指位置的改变量,存储在x以及y方向的该变量
// CGPoint point = [panGesture translationInView:panGesture.view];
// panGesture.view.center = CGPointMake(panGesture.view.center.x + point.x, panGesture.view.center.y + point.y);
// //将之前的增量清0
// //系统提供的设置之前的增量
// [panGesture setTranslation:CGPointZero inView:panGesture.view];
//}
//处理缩放手势
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinchGesture
{
pinchGesture.view.transform = CGAffineTransformScale(pinchGesture.view.transform, pinchGesture.scale, pinchGesture.scale);
//将之前变化的比例置1
// [pinchGesture setScale:1.0]; //同下
pinchGesture.scale = 1.0;
}
//处理旋转手势
- (void)handleRotationGesture:(UIRotationGestureRecognizer *)rotationGesture
{
rotationGesture.view.transform = CGAffineTransformRotate(rotationGesture.view.transform, rotationGesture.rotation);
//将之前的该变量清0
rotationGesture.rotation = 0;
}
//处理屏幕边缘手势
- (void)handleScreenEdgeGesture:(UIScreenEdgePanGestureRecognizer *)screenEdgePanGesture
{
CGPoint point = [screenEdgePanGesture translationInView:screenEdgePanGesture.view];
screenEdgePanGesture.view.transform = CGAffineTransformTranslate(screenEdgePanGesture.view.transform, point.x, 0);
[screenEdgePanGesture setTranslation:CGPointZero inView:screenEdgePanGesture.view];
}
- (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
0 0
- IOS开发之手势(UIGestureRecognizer)
- IOS开发之手势UIGestureRecognizer
- iOS开发之手势(UIGestureRecognizer)
- IOS开发之手势UIGestureRecognizer
- IOS开发之手势—UIGestureRecognizer 共存
- IOS开发之手势——UIGestureRecognizer
- IOS开发之手势——UIGestureRecognizer
- IOS开发之手势——UIGestureRecognizer 手势共存
- IOS开发手势-----UIGestureRecognizer 共存
- iOS开发 - 手势识别(UIGestureRecognizer)
- iOS开发 手势识别UIGestureRecognizer
- iOS之UIGestureRecognizer手势识别
- IOS开发之手势——UIGestureRecognizer 共存
- IOS开发之手势——UIGestureRecognizer 共存
- IOS开发之手势——UIGestureRecognizer 共存
- IOS开发之手势——UIGestureRecognizer 共存
- IOS开发之手势——UIGestureRecognizer 共存
- IOS开发之手势——UIGestureRecognizer 共存
- start a android App from scratch
- mongoDB的CURD操作
- initcall机制在应用程序中的使用
- SMTP、POP3协议的命令
- buffalo之hello world
- iOS开发之手势(UIGestureRecognizer)
- 处事感悟——给刚参加工作的朋友
- xcode 如何添加调试设备
- MATLAB——imhist函数
- listview滑到底时自动加载
- 读《你的知识需要管理》
- javascript随机生成颜色8种方法
- SpringMVC配置静态资源访问
- 开学了