iOS - 手势的学习

来源:互联网 发布:impress.js reveal.js 编辑:程序博客网 时间:2024/05/20 19:50



一:手势的类型

 

  1.    拍击UITapGestureRecognizer (任意次数的拍击)
  2.    长按UILongPressGestureRecognizer
  3.    向里或向外捏UIPinchGestureRecognizer (用于缩放)
  4.    旋转UIRotationGestureRecognizer(手指朝相反方向移动) 
  5.    划动UISwipeGestureRecognizer (以任意方向) 
  6.    摇动或者拖拽UIPanGestureRecognizer 

二:创建手势

// 手势识别是具有互斥的原则的,比如单击和双击,如果它识别出一种手势,其后的手势将不被识别

//   如果手势加在uiimageview上 记住 设置userInteractionEnabled = yes;

    

    // 添加单击的手势

    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]init];

    tapGestureRecognizer.numberOfTapsRequired = 1// 设置单击几次才触发方法

    [tapGestureRecognizer addTarget:self action:@selector(tapGestureAction:)]; // 添加点击手势的方法

    [self.view addGestureRecognizer:tapGestureRecognizer]; // 添加到当前的View

    [tapGestureRecognizer release], tapGestureRecognizer = nil// 释放内存


//手势传值

       img.userInteractionEnabled = YES;

        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer allocinitWithTarget:self action:@selector(pushWeiboInfo:)];

        [img addGestureRecognizer:singleTap];

        [singleTap setValue:la forKey:@"la"];

        [cell.contentView addSubview:img];

    }

}


-(void)pushWeiboInfo:(id)sender

{

    UITapGestureRecognizer *tap = (UITapGestureRecognizer*)sender;

    loadData *la = [tap valueForKey:@"la"];


}


    // 添加长按的手势 注意:会调用两次方法,开始长按调用一次  松开后再调用一次  当长按并且滑动的时候,会多次调用长按的方法

    UILongPressGestureRecognizer *pressLongGestureRecognizer = [[UILongPressGestureRecognizer alloc] init];

    [pressLongGestureRecognizer addTarget:selfaction:@selector(pressLongGestureAction:)]; // 给长按手势添加方法

    pressLongGestureRecognizer.minimumPressDuration =4.0;//长按时间限制

    [self.view addGestureRecognizer:pressLongGestureRecognizer]; // 添加到当前的View

    [pressLongGestureRecognizer release], pressLongGestureRecognizer = nil// 释放内存

    

    

    

    // 添加捏合的手势  注意:捏合手势不是捏合一次调用一次方法,而是在捏合的过程中不停的调用方法

    UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizeralloc] init];

    [pinchGestureRecognizer addTarget:self action:@selector(pinchGestureAction:)]; //添加捏合手势的方法

    [self.view addGestureRecognizer:pinchGestureRecognizer]; // 添加到当前的View

    [pinchGestureRecognizer release], pinchGestureRecognizer = nil// 释放内存

    

    

   

    // 添加旋转的手势 注意:旋转手势是两指同时进行旋转

    UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] init];

    [rotationGestureRecognizer addTarget:selfaction:@selector(rotationGestureAction:)]; // 给旋转手势添加方法

    [self.view addGestureRecognizer:rotationGestureRecognizer]; // 添加到当前的View

    [rotationGestureRecognizer release], rotationGestureRecognizer = nil;

    

    

    

    // 添加滑动的手势  注意: 快速移动,是用于监测滑动的方向的

    UISwipeGestureRecognizer *swipGestureRecognizer = [[UISwipeGestureRecognizeralloc] init];

    swipGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp; // 添加手势的方法

    // 以下是设置滑动的方向

    // typedef NS_OPTIONS(NSUInteger,UISwipeGestureRecognizerDirection) {

    //    UISwipeGestureRecognizerDirectionRight= 1 << 0, // 从左向右滑动

    //    UISwipeGestureRecognizerDirectionLeft  =1 << 1, // 从右向左滑动

    //    UISwipeGestureRecognizerDirectionUp    =1 << 2, // 从下向上滑动

    //    UISwipeGestureRecognizerDirectionDown  =1 << 3  // 从上向下滑动

    // };

    [swipGestureRecognizer addTarget:self action:@selector(swipGestureAction:)]; // 给滑动手势添加方法

    [self.view addGestureRecognizer:swipGestureRecognizer]; // 添加到当前的View

    [swipGestureRecognizer release], swipGestureRecognizer = nil// 释放内存

    

    

    

    // 添加拖移手势  注意:慢速移动,是用于监测偏移的量的

    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]init];

    [panGestureRecognizer addTarget:self action:@selector(panGestureAction:)]; // 添加托移收拾的方法

    [self.view addGestureRecognizer:panGestureRecognizer]; // 添加到当前的View

    [panGestureRecognizer release], panGestureRecognizer = nil// 释放内存

 

 

#pragmamark - 实现单击手势的方法

- (void)tapGestureAction:(UITapGestureRecognizer *) sender {

    NSLog(@"  轻拍   了屏幕");

}

 

#pragmamark - 实现长按手势的方法

- (void)pressLongGestureAction:(UILongPressGestureRecognizer *) sender {

    NSLog(@"   长按   了屏幕");

// 为了让代码执行一次 

       if ([senderstate] ==UIGestureRecognizerStateBegan) {

           NSLog(@"长按事件");

   

         }

}

 

#pragmamark - 实现了捏合手势的方法

- (void)pinchGestureAction:(UIPinchGestureRecognizer *) sender {

    NSLog(@"   捏合   了屏幕");

}

 

#pragmamark - 实现旋转手势的方法

- (void)rotationGestureAction:(UIRotationGestureRecognizer *) sender {

    NSLog(@"您使用了   旋转   手势");

}

 

#pragmamark - 实现滑动手势的方法

- (void)swipGestureAction:(UISwipeGestureRecognizer *) sender {   

    NSLog(@"   滑动    了屏幕");

}

 

#pragmamark - 实现了托移手势的方法

- (void)panGestureAction:(UIPanGestureRecognizer *) sender {

    NSLog(@"   托移   了。。。。");

}




0 0
原创粉丝点击