关闭

手势

63人阅读 评论(0) 收藏 举报
分类:
//先建一个视图图片
    UIImageView *image1 =[[UIImageView alloc]initWithFrame:CGRectMake(30, 30, 300, 300)];
    image1.image =[UIImage imageNamed:@“12"];
    image1.center =self.view.center;
    [
self.view addSubview:image1];
    [image1
release];
    //
    //打开交互
    image1.userInteractionEnabled =YES;



 /************* 轻拍  ****************/
    UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
   
//点击次数
    tap.
numberOfTapsRequired =2;
   
//手指个数
    tap.
numberOfTouchesRequired =2;
   
    [image1 
addGestureRecognizer:tap];
    [tap release];
/*************  轻拍方法  ************/

- (
void)tapAction:(UITapGestureRecognizer *)tap{
   
   
   
NSLog(@"轻拍");
}


/*************  轻扫  ******************/
   
   
UISwipeGestureRecognizer *swipe =[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
     //默认向右扫
   
//方向只能正方向,不能斜着扫
   
//只能上下一起
    //或者左右一起
    swipe.direction =swipe.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;
    [image1
addGestureRecognizer:swipe];
    [swipe release];
//或是  | ,就是一个|

/*************  轻扫方法 swipe ************/
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
   
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
       
NSLog(@"向左");
    }
   
NSLog(@"轻扫");
   
}


 /************* 捏合 pinch *****************/
        UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
        [image1
addGestureRecognizer:pinch];
        [pinch release];
/************* 捏合 pinch方法 **************/
- (void)pinchAction:(UIPinchGestureRecognizer *)pin
{
      NSLog(@"捏合");
   
// 捏合时 等比例缩放图片
   
UIImageView *image1 = (UIImageView *)pin.view;
   
// 设置比例
   
// scale比例
   
// 参数1: 要缩放的视图的transform
   
// 参数2: 横向比例
    // 参数3: 纵向比例
    // scale:相对接触点的缩放比例在屏幕协调
    [image1 setTransform:CGAffineTransformScale(image1.transform, pin.scale, pin.scale)];
     //缩放结束之后,将缩放比例设置为1
    pin.scale = 1;
}


   /*************旋转 rotation ***********/
#if 0
    {
       
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
        [image1
addGestureRecognizer:rotation];
        [rotation
release];
       
       
    }
#endif
  /*************旋转 rotation ************/
- (void)rotationAction:(UIRotationGestureRecognizer *)rot
{
   
NSLog(@"旋转~跳跃~");
   
// 获取旋转手势时 让图片跟随旋转
   
// 1.获取图片对象
   
UIImageView *image1 = (UIImageView *)rot.view;
   
// 2.设置视图的transform属性
   
// transform视图的变形属性
    [image1
setTransform:CGAffineTransformMakeRotation(rot.rotation)];
   
NSLog(@"%@", image1);
   
}
 

 #if 0  /*************  拖拽pan *********/
{
       
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
        [image1
addGestureRecognizer:pan];
        [pan
release];
    }
#endif
/************** pan拖拽  ****************/
- (void)panAction:(UIPanGestureRecognizer *)pan
{
   
NSLog(@"拖拽");
   
// 拖拽视图
   
UIImageView *panView = (UIImageView *)pan.view;
   
// 获取拖拽时 经过的点
   
CGPoint p = [pan translationInView:panView];
   
// 设置transform
    [panView
setTransform:CGAffineTransformTranslate(panView.transform, p.x, p.y)];
   
// 设置跟随属性
    [pan
setTranslation:CGPointZero inView:panView];

}


#if 0 /**********  长按longPress***********/
    {
       
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
       
// 需要多长时间触发
        longPress.
minimumPressDuration = 2;
       
// 允许移动的距离
        longPress.
allowableMovement = 200;
       
// 添加
        [image1
addGestureRecognizer:longPress];
        [longPress
release];
    }
#endif

/********  长按longPress ******************/
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
   
// 其他手势也可以触发长按 可以通过状态来判断
   
//UIGestureRecognizerStateBegan  姿态识别器状态开始了
   
if (longPress.state == UIGestureRecognizerStateBegan) {
       
NSLog(@"长按");
    }

}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:471次
    • 积分:53
    • 等级:
    • 排名:千里之外
    • 原创:5篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档