Touch觸摸
// 碰到屏幕就會觸發(fā)該方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"觸碰到了");
UITouch *touch = [touches anyObject];
// 獲取點擊坐標
CGPoint point = [touch locationInView:self.view];
NSLog(@"%f, %f", point.x, point.y);
}
Tap點按
UITapGestureRecognizer *doubleTgr = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTagr:)];
// 點擊的次數(shù)
//doubleTgr.numberOfTapsRequired = 2;
[self.imageView addGestureRecognizer:doubleTgr];
LongPress長按
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(lpgrStart:)];
[self.imageView addGestureRecognizer:lpgr];
Swipe輕掃
// 由于輕掃只能識別一個方向,所以把他的四個方向放在一個數(shù)組里留著以后辨別,這樣就能識別四個方向了
NSArray *arr = @[[NSNumber numberWithUnsignedInteger:UISwipeGestureRecognizerDirectionDown],[NSNumber numberWithUnsignedInteger:UISwipeGestureRecognizerDirectionUp],[NSNumber numberWithUnsignedInteger:UISwipeGestureRecognizerDirectionLeft],[NSNumber numberWithUnsignedInteger:UISwipeGestureRecognizerDirectionRight]];
for (int i=0; i<arr.count; i++) {
UISwipeGestureRecognizer *swiper = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swiper:)];
//把方向取出來
NSNumber *direction = arr[i];
// 把方向轉為Integer類型賦給輕掃的方向
swiper.direction = [direction unsignedIntegerValue];
[self.imageView addGestureRecognizer:swiper];
}
- (void)swiper:(UISwipeGestureRecognizer *)swiper
{
if (swiper.direction == UISwipeGestureRecognizerDirectionDown) {
NSLog(@"向下");
}
if (swiper.direction & UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"向左");
}
if (swiper.direction & UISwipeGestureRecognizerDirectionRight) {
NSLog(@"向右");
}
if (swiper.direction & UISwipeGestureRecognizerDirectionUp) {
NSLog(@"向上");
}
}
Rotation旋轉
// 創(chuàng)建一個手勢
UIRotationGestureRecognizer *rotion = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotion:)];
// 手勢添加在控件上
[self.imageView addGestureRecognizer:rotion];
- (void)rotion:(UIRotationGestureRecognizer *)rotion
{
NSLog(@"%f", rotion.rotation);
rotion.view.transform = CGAffineTransformRotate(rotion.view.transform, rotion.rotation);
// 由于此方法是旋轉過程中反復調用的,所以旋轉弧度很容易疊加,要及時清零才正常一點
rotion.rotation = 0;
}
Pinch捏合
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
[self.imageView addGestureRecognizer:pinch];
-
(void)pinch:(UIPinchGestureRecognizer *)pinch
{
// 放大縮小的比例
NSLog(@"%f", pinch.scale);pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
// 把伸縮比例及時變?yōu)?,防止伸縮效果疊加
pinch.scale = 1;
}
Pan拖拽
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
[self.imageView addGestureRecognizer:pan] ;
-
(void)pan:(UIPanGestureRecognizer *)pan
{
// 記錄原來的位置
static CGPoint center;if (pan.state == UIGestureRecognizerStateBegan) {
center = pan.view.center;
}// 獲取偏移量
CGPoint point = [pan translationInView:self.view];
// 最后拖拽手勢所在view的中心點,就是原來的位置加上變動的位置
pan.view.center = CGPointMake(center.x + point.x, center.y + point.y);
}
Double手勢疊加
//可以添加多個手勢
// 一個控件可以添加多個手勢,但一個手勢不能添加在多個控件上
添加多個手勢時要遵守<UIGestureRecognizerDelegate>協(xié)議
// 返回yes,允許同時添加多個手勢
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}