点击手势和双击手势ui
#import "TapViewController.h"spa
@interface TapViewController ().net
@end 3d
@implementation TapViewControllerorm
- (void)viewDidLoad {对象
[super viewDidLoad];继承
// Do any additional setup after loading the view.事件
//点击手势:图片
//=================单击===========ip
//1.建立一个点击手势对象
//UIGestureRecognizer是全部手势的父类,通常不会直接使用哪一个它
//而是使用它的子类
//参数1:响应手势的对象;
//参数2:响应的消息
//功能:发生点击动做,对象去响应这个手势
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer
alloc]initWithTarget:self action:@selector(tapGesture:)];
//2.添加手势到指定的视图上(任何手势均可以添加到任何继承自UIView的类的对象上)
[self.view addGestureRecognizer:tapGesture];
//=================双击===========
//1.建立点击对象
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(doubleTap:)];
//2.设置点击次数
doubleTap.numberOfTapsRequired = 2;
//4.忽略其余手势
//单击的时候忽略双击;
[tapGesture requireGestureRecognizerToFail:doubleTap];
//3.将手势添加到指定的视图控制器上
[self.view addGestureRecognizer:doubleTap];
}
#pragma mark -单击手势响应
- (void)tapGesture:(UITapGestureRecognizer *)tap{
//获取点击手势的坐标
CGPoint point = [tap locationInView:self.view];
// NSLog(@"%@",NSStringFromCGPoint(point));
NSLog(@"单击");
}
#pragma mark - 双击事件响应
- (void)doubleTap:(UIGestureRecognizer *)doubleTap{
//获取点击手势的坐标
CGPoint point = [doubleTap locationInView:self.view];
//NSLog(@"%@",NSStringFromCGPoint(point));
NSLog(@"双击");
}
@end
长按手势:关键在于设置手势的时间
#import "LongPressViewController.h"
@interface LongPressViewController ()
@end
@implementation LongPressViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1.建立长按手势对象
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
//3.设置长按时间
longPress.minimumPressDuration = 1;
//2.将手势添加到视图控制器上;
[self.view addGestureRecognizer:longPress];
}
#pragma mark -响应长按手势
//这个方法在手势的状态发生改变的时候调用
- (void)longPress : (UILongPressGestureRecognizer *)longPress{
//手势的状态:(针对全部的手势都适用)
//UIGestureRecognizerStateBegan:手势开始
//UIGestureRecognizerStateChanged:手势改变
//UIGestureRecognizerStateEnded:手势结束
//longPress setState:<#(UIGestureRecognizerState)#>
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"长按");
}
// NSLog(@"长按");
}
@end
滑动手势:关键在于设置滑动的方向
#import "SwipeViewController.h"
@interface SwipeViewController ()
@end
@implementation SwipeViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1.建立滑动手势对象
UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:@selector(swipGesture:)];
//3.滑动方向
//注意:若是想要能够两个方向均可以,滑动方向必须添加一个右划的手势
[swip setDirection:UISwipeGestureRecognizerDirectionLeft];
//2.将手势添加到视图控制器上
[self.view addGestureRecognizer:swip];
//=============右划默认是从左向右滑动==========
UISwipeGestureRecognizer *swipRight = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:@selector(swipGesture:)];
// [swip setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:swipRight];
}
#pragma mark - 滑动手势响应事件
- (void)swipGesture:(UISwipeGestureRecognizer *)swipGesture{
if (swipGesture.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"右滑回到下一页");
}else{
NSLog(@"左滑到上一页");
}
}
@end
拖动手势
#import "PanViewController.h"
@interface PanViewController ()
@end
@implementation PanViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1.建立拖动手势对象
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:@selector(pan:)];
//2.添加到视图
[self.imageView addGestureRecognizer:panGesture];
}
//这个方法会在拖动过程当中时时调用;
#pragma mark - 拖动手势方法
- (void)pan:(UIPanGestureRecognizer *)pan{
self.imageView.center = [pan locationInView:self.view];
NSLog(@"拖动");
}
@end
旋转手势:
#import "RotationViewController.h"
@interface RotationViewController ()
@end
@implementation RotationViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//1.建立旋转手势对象
UIRotationGestureRecognizer *rotatioin = [[UIRotationGestureRecognizer alloc]
initWithTarget:self action:@selector(rotationG:)];
//2.添加到视图上
[self.imageView addGestureRecognizer:rotatioin];
}
#pragma mark - 旋转手势事件
- (void) rotationG:(UIRotationGestureRecognizer *)ro{
static CGFloat lastRotation = 0;
//® self.imageView.center = [rotation locationInView:self.view];
CGFloat triangle = ro.rotation + lastRotation;
self.imageView.transform = CGAffineTransformMakeRotation(triangle);
if (ro.state == UIGestureRecognizerStateEnded) {
lastRotation = ro.rotation + lastRotation;
}
NSLog(@"旋转");
}
@end
缩放手势
#import "PinchViewController.h"
@interface PinchViewController ()
@end
@implementation PinchViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//1.建立pinch手势对象
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]
initWithTarget:self action:@selector(pinchGesture:)];
//2.添加到视图上
[self.view addGestureRecognizer:pinch];
}
- (void)pinchGesture:(UIPinchGestureRecognizer *)pinch{
static CGFloat lastScale =1;
CGFloat scale = pinch.scale;
//3.经过手势的缩放比例去改变图片的缩放形变;
[self.imageView setTransform:CGAffineTransformMakeScale(scale, scale)];
if (pinch.state == UIGestureRecognizerStateEnded) {
lastScale = pinch.scale * lastScale;
}
NSLog(@"缩放");
}
@end