iOS学习笔记——手势(Gesture)
2013-12-30 09:26
274 查看
手势是指你用一个或多个手指接触屏幕开始,直到你的手指全部离开屏幕为止所发生的所有事件。手势识别器(UIGestureRecognizer)是一个对象,知道如何观察用户生成的事件流,并识别用户何时以与预定义的手势相匹配的方式进行拉触摸和拖动。UIGestureRecognizer类封装了查找手势的工作。在模拟器中,按“option”键,可模拟两个手指的手势。
在.h文件里创建一个视图对象,此对象是模拟拖动手势时的目标:
在.h文件里创建一个视图对象,此对象是模拟拖动手势时的目标:
@interface LinViewController : UIViewController //创建视图对象 @property (retain, nonatomic) UIView * mView; @end在.m文件里,创建各个手势的对象,编写各个手势的实现方法:
@implementation LinViewController - (void)viewDidLoad { [super viewDidLoad]; //---点击手势识别器---// //创建点击手势的对象 UITapGestureRecognizer * pSingleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleGesture:)]; //将手势添加到当前视图中 [self.view addGestureRecognizer:pSingleTap]; //创建点击手势的对象 UITapGestureRecognizer * pDoubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleGesture:)]; //设置点击次数为2次,默认为1次,属于单击操作 pDoubleTap.numberOfTapsRequired = 2; //将手势添加到当前视图中 [self.view addGestureRecognizer:pDoubleTap]; //双击的时候让单击放弃响应 [pSingleTap requireGestureRecognizerToFail:pDoubleTap]; //释放创建的对象 [pDoubleTap release]; [pSingleTap release]; //---滑动手势识别器---// //创建滑动手势的对象 UISwipeGestureRecognizer * pSwip = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipGesture:)]; //设置滑动的方向,此处为向右,共有4个方向 pSwip.direction = UISwipeGestureRecognizerDirectionRight; //将手势添加到当前视图中 [self.view addGestureRecognizer:pSwip]; //释放创建的对象 [pSwip release]; //---拖动手势识别器--// //创建一个拖动手势动作的目标,假定为一个视图块 self.mView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 50, 50)]; self.mView.backgroundColor = [UIColor redColor]; [self.view addSubview:self.mView]; //创建拖动手势对象 UIPanGestureRecognizer * pPan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)]; //将手势添加到当前视图中 [self.view addGestureRecognizer:pPan]; //释放创建的对象 [pPan release]; //---长按手势识别器--// //创建长按手势的对象 UILongPressGestureRecognizer * pLongPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressGesture:)]; //设置长按的时间,2秒以上判定为长按 pLongPress.minimumPressDuration = 2; //将手势添加到当前视图中 [self.view addGestureRecognizer:pLongPress]; //释放创建的对象 [pLongPress release]; //---旋转手势识别器---// //创建旋转手势对象 UIRotationGestureRecognizer * pRotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGesture:)]; //将手势添加到当前视图中 [self.view addGestureRecognizer:pRotation]; //释放创建的对象 [pRotation release]; } #pragma mark--------tapGesture>>>single 单击 - (void)singleGesture:(UITapGestureRecognizer *)tap { //单击手势对应的操作 NSLog(@"单击操作"); } #pragma mark--------tapGesture>>>double 双击 - (void)doubleGesture:(UITapGestureRecognizer *)tap { //双击手势对应的操作 NSLog(@"双击操作"); } #pragma mark--------tapGesture>>>Swip 滑动 - (void)swipGesture:(UISwipeGestureRecognizer *)swip { //滑动,划屏手势对应的操作 NSLog(@"滑动操作,分方向,此处向右滑动"); } #pragma mark--------tapGesture>>>Pan 平移 - (void)panGesture:(UIPanGestureRecognizer *)pan { //获取平移手势在视图上的坐标 CGPoint point = [pan locationInView:self.view]; //把视图的中心点确定在平移手势的坐标上,即把视图拖动到平移点 self.mView.center = point; NSLog(@"%@",NSStringFromCGPoint(point)); } #pragma mark--------tapGesture>>>LongPress 长按 - (void)longPressGesture:(UILongPressGestureRecognizer *)longPress { //长按手势对应的操作 NSLog(@"长按操作"); } #pragma mark--------tapGesture>>>Rotation 旋转 - (void)rotationGesture:(UIRotationGestureRecognizer *)rotation { //确定手势旋转的大小,角度,使视图做出相应的反应 float degree = rotation.rotation *(180/M_PI); NSLog(@"旋转的角度为%.2f",degree); } //释放创建的对象 - (void)dealloc { [_mView release]; [super dealloc]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end
相关文章推荐
- iOS学习笔记12--纯代码实现原生UITabBarController,手势滑动切换视图
- iOS学习笔记06-手势识别
- iOS学习笔记-064.手势解锁
- 斯坦福大学iOS应用开发教程学习笔记(第五课)Protocols,手势识别
- IOS学习笔记(14)手势识别
- ios学习笔记3 平移的手势的方向
- ios自用学习笔记-触摸事件与手势识别
- iOS学习笔记18—UITapGestureRecognizer 的用法
- IOS学习笔记之触摸与手势
- iPhone/iPad高级应用与手游开发学习笔记:多点触摸与手势检测(三:UIPinchGestureRecognizer和UIRotationGestureRecognizer)
- ios学习笔记----实现一个带滑动手势的tabBarViewController,并可自定义tabBar
- 《疯狂Android讲义》学习笔记 -- 手势(Gesture)
- 2011斯坦福大学iOS应用开发教程学习笔记(第五课)Protocols,手势识别
- iOS开发 UI学习笔记 UIGestureRecognizer手势加载
- iOS 开发笔记(一)——iOS Gesture Recognizer 手势识别
- iOS 学习笔记 手势识别
- iOS学习笔记-067.手势识别01——敲击、长按、轻扫手势
- [ios学习笔记之视图、绘制和手势识别]
- ios点击手势学习笔记
- iOS学习笔记-068.手势识别02——捏合、拖拽、旋转手势