七种手势,下面就是我的项目中写的,直接往ViewController.m里粘贴就行了,可以自己看运行结果。
//
// ViewController.m
// 手势
//
// Created by zxx on 16-7-27.
// Copyright (c) 2016年 123. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//一个控件可以绑定多个手势,但有些时候手势会右冲突。比如点击一次和点击二次
//手势 苹果封装好的一些事件类型,方便我们进行不同事件的处理
//Gesture手势 Recognizer识别
//UIGestureRecognizer手势父类是一个抽象类,需要子类完成一些功能
// UITapGestureRecognizer;点击手势
// UIPanGestureRecognizer;滑动
// UIRotationGestureRecognizer;旋转
// UIPinchGestureRecognizer;缩放
// UILongPressGestureRecognizer;长按
// UIScreenEdgePanGestureRecognizer;边缘滑动
// UISwipeGestureRecognizer;轻扫
//可以给指定的控件添加手势,而不像touchbegan 事件的处理方式
UIView *bgView=[[UIView alloc]initWithFrame:CGRectMake(100, 100, 150, 200)];
bgView.backgroundColor=[UIColor redColor];
[self.view addSubview:bgView];
//点击手势
//创建点击手势并且绑定触发事件
UITapGestureRecognizer *tapgestureRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture:)];
//双击事件 连点2次
//tapgestureRecognizer.numberOfTapsRequired=2;
//触摸手指的个数
//tapgestureRecognizer.numberOfTouchesRequired=2;
//给bgView添加手势对象
[bgView addGestureRecognizer:tapgestureRecognizer];
//双击
UITapGestureRecognizer *tapgestureRecognizer2=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture2:)];
tapgestureRecognizer2.numberOfTapsRequired=2;
[bgView addGestureRecognizer:tapgestureRecognizer2];
//单击 等待双击失败 才执行
[tapgestureRecognizer requireGestureRecognizerToFail:tapgestureRecognizer2];
//滑动手势 手指一直在屏幕上触摸移动
UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)];
[bgView addGestureRecognizer:panGesture];
//旋转手势
UIRotationGestureRecognizer *rotationGesture=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGesture:)];
[bgView addGestureRecognizer:rotationGesture];
//缩放
UIPinchGestureRecognizer *pinchGesture=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGesture:)];
[bgView addGestureRecognizer:pinchGesture];
//长按
UILongPressGestureRecognizer *longGesture=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longGesture:)];
[bgView addGestureRecognizer:longGesture];
//边缘滑动
UIScreenEdgePanGestureRecognizer *screenGesture=[[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screenGesture:)];
screenGesture.edges=UIRectEdgeLeft;
[self.view addGestureRecognizer:screenGesture];
//轻扫
UISwipeGestureRecognizer *swipeGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGesture:)];
//默认往右轻扫
swipeGesture.direction=UISwipeGestureRecognizerDirectionLeft;
[bgView addGestureRecognizer:swipeGesture];
}
//所有事件都有状态 比如:开始 变化 结束 失败
/*
UIGestureRecognizerStatePossible, 默认状态:手势在未识别时的状态
UIGestureRecognizerStateBegan, 开始:手势触发时
UIGestureRecognizerStateChanged, 改变:手势变化时
UIGestureRecognizerStateEnded, 结束
UIGestureRecognizerStateCancelled,取消
UIGestureRecognizerStateFailed, 失败
*/
#pragma mark- 点击手势
- (void)tapGesture:(UITapGestureRecognizer *)tapGesture{
NSLog(@"点击手势");
}
- (void)tapGesture2:(UITapGestureRecognizer *)tapGesture2{
NSLog(@"点击2次");
}
#pragma mark- 滑动手势
- (void)panGesture:(UIPanGestureRecognizer *)panGesture{
//NSLog(@"滑动手势");
switch (panGesture.state) {
case UIGestureRecognizerStatePossible:
NSLog(@"默认状态:手势在未识别时的状态");
break;
case UIGestureRecognizerStateBegan:
NSLog(@"滑动开始");
break;
case UIGestureRecognizerStateChanged:
{
NSLog(@"滑动进行中");
//获得添加手势的对象
//panGesture.view
//获得滑动的距离 包含x,y移动的距离
//NO1:添加移动的视图
CGPoint point=[panGesture translationInView:panGesture.view];
NSLog(@"---%@",NSStringFromCGPoint(point));
//CGRectOffset是以视图的原点为起始移动,dx x移动,dy y移动
panGesture.view.frame=CGRectOffset(panGesture.view.frame, point.x, point.y);
//清空每次移动的距离 因为每次移动,它的距离会累加
[panGesture setTranslation:CGPointZero inView:panGesture.view];
}
break;
case UIGestureRecognizerStateEnded:
NSLog(@"滑动结束");
break;
default:
break;
}
}
#pragma mark- 旋转手势
- (void)rotationGesture:(UIRotationGestureRecognizer *)rotationGesture{
switch (rotationGesture.state) {
case UIGestureRecognizerStateBegan:
break;
case UIGestureRecognizerStateChanged:
rotationGesture.view.transform=CGAffineTransformMakeRotation(rotationGesture.rotation);
break;
case UIGestureRecognizerStateEnded:
//回到初始的位置
rotationGesture.view.transform=CGAffineTransformIdentity;
break;
default:
break;
}
}
#pragma mark- 缩放手势
- (void)pinchGesture:(UIPinchGestureRecognizer *)pinchGesture{
switch (pinchGesture.state) {
case UIGestureRecognizerStateBegan:
break;
case UIGestureRecognizerStateChanged:
{
//起始的缩放比例
CGFloat scale=pinchGesture.scale;
//比例缩放后的宽度和高度
CGFloat height=pinchGesture.view.frame.size.height*(1-scale);
CGFloat width=pinchGesture.view.frame.size.width*(1-scale);
//根据视图的中心点缩放
pinchGesture.view.frame=CGRectInset(pinchGesture.view.frame, width/2, height/2);
//比例的缩放也要回到初始
pinchGesture.scale=1.0;
}
break;
case UIGestureRecognizerStateEnded:
break;
default:
break;
}
}
#pragma mark- 长按手势
- (void)longGesture:(UILongPressGestureRecognizer *)longGesture{
NSLog(@"长按");
}
#pragma mark- 边缘滑动
- (void)screenGesture:(UIScreenEdgePanGestureRecognizer *)screenGesture{
NSLog(@"边缘滑动");
}
#pragma mark- 轻扫
- (void)swipeGesture:(UISwipeGestureRecognizer *)swipeGesture{
NSLog(@"轻扫");
}
@end
网友评论