// 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.
}
IOS 中的手势
最新推荐文章于 2024-09-26 15:35:56 发布