點擊手勢和雙擊手勢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