您的位置:首页 > 移动开发 > IOS开发

手势学习

2015-06-29 15:21 447 查看
//
//  GestureViewController.m
//  AppUI组件学习
//
//  Created by 麦子 on 15/6/29.
//  Copyright (c) 2015年 麦子. All rights reserved.
//

#import "GestureViewController.h"

@interface GestureViewController ()

@end

@implementation GestureViewController

- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"手势识别";
self.view.backgroundColor = [UIColor whiteColor];
// 识别手势类
[self testPan];

}

// 点击类测试
- (void)testTap{
// 点击
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(grEvent:)];
gr.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:gr];

UITapGestureRecognizer *gr2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dbGrEvent:)];
gr2.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:gr2];

// 当单击失败的时候,才双机
[gr requireGestureRecognizerToFail:gr2];// 区分单机和双击问题。

}

// 滑动
- (void)testSwipe{
UISwipeGestureRecognizer *sw = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swEvent:)];
[sw setDirection:UISwipeGestureRecognizerDirectionLeft];//分别4个方向
[self.view addGestureRecognizer:sw];

}

// 长按
- (void)testLogTap{
UILongPressGestureRecognizer *longPr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTap:)];
longPr.minimumPressDuration = 3;// 最小3秒时间
[self.view addGestureRecognizer:longPr];
}

// 捏合  放大缩小
- (void)testPin{
UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinEvent:)];
[self.view addGestureRecognizer:pin];
}

// 旋转
- (void)testRota{
UIRotationGestureRecognizer *rota = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotaEvent:)];
[self.view addGestureRecognizer:rota];
}

// 拖拽
- (void)testPan{
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panEvent:)];
[self.view addGestureRecognizer:pan];
}

// 拖拽
- (void)panEvent:(UIPanGestureRecognizer *)sender{
// 这一点到前面一点的移动的距离
CGPoint point = [sender translationInView:self.view];
// 调整视图的中心位置
sender.view.center = CGPointMake(sender.view.center.x+point.x, sender.view.center.y+point.y);
// 恢复位置
[sender setTranslation:CGPointZero inView:self.view];

}

// 这个是两个之间的距离
- (void)rotaEvent:(UIRotationGestureRecognizer *)obj{

NSLog(@"旋转。。。。%g度",obj.rotation*180/M_PI); // 相对上一次的增量。
}

// 这个是两个之间的距离
- (void)pinEvent:(UIPinchGestureRecognizer *)obj{

NSLog(@"捏合。。。。%f",obj.scale);
}

// 长按监听---长按会进来方法两次,所以要通过状态来进行区别
- (void)longTap:(UILongPressGestureRecognizer *)longTapEvent{
NSLog(@"3秒显示一次,抬起的时候,显示一次。");
if (longTapEvent.state == UIGestureRecognizerStateBegan) {

NSLog(@"时间到,触发吧");

}else if(longTapEvent.state == UIGestureRecognizerStateEnded){

NSLog(@"时间到,消失吧");
}

}

- (void)swEvent:(id)obj{

NSLog(@"滑动监听。。。。。");
}

- (void)grEvent:(id)obj{

NSLog(@"单击了。。。。。");

}

- (void)dbGrEvent:(id)obj{

NSLog(@"双击了。。。。");

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

//// 触摸开始
//- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//
//    // 位置
//    UITouch  *touch = [touches anyObject];
//    long count = [touch tapCount]; // 点击的个数
//
//
//    NSLog(@"触摸开始----手指个数-%ld-----点击个数%ld",touches.count,count);
//
//}

//// 触摸结束
//- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
//
//    NSLog(@"触摸结束----%ld",touches.count); // 手指个数
//
//
//}
//
//// 移动
//- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
//    UITouch *touch = [touches anyObject];
//    CGPoint point = [touch locationInView:self.view];
//
//    NSLog(@"触摸移动----%ld",touches.count);
//    NSLog(@"触摸移动--X:--%f----Y:--%f",point.x,point.y);
//}
//
//// 触摸事件取消
//- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
//
//    NSLog(@"触摸取消");
//
//}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS 手势