手势与触控_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > 手势与触控

手势与触控

 2015/4/16 23:31:07  jsonUserList  程序员俱乐部  我要评论(0)
  • 摘要:UIImageView*imgView=[[UIImageViewalloc]initWithFrame:CGRectMake(100,100,75,75)];imgView.image=[UIImageimageNamed:@"a"];imgView.tag=200;[self.viewaddSubview:imgView];//设置允许触控和执行手势imgView.userInteractionEnabled=YES
  • 标签:

 UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 75, 75)];

    imgView.image=[UIImage imageNamed:@"a"];

    imgView.tag=200;

    [self.view addSubview:imgView];

    

    //设置允许触控和执行手势

    imgView.userInteractionEnabled=YES;

    

    //创建点击手势

    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureClick:)];

    //将创建的点击手势添加到图片对象

    [imgView addGestureRecognizer:tap];

    

    

    //创建拖拽手势

    UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureClick:)];

    [imgView addGestureRecognizer:pan];

    

    

    //创建长按手势

    UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressGestureClick:)];

    [imgView addGestureRecognizer:longPress];

    

    

    //创建旋转手势

    UIRotationGestureRecognizer *rotate=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotateGestureClick:)];

    [imgView addGestureRecognizer:rotate];

    

    

    

    //创建捏合手势

    UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGestureClick:)];

    //捏合手势可以与其他手势同时执行

    pinch.delegate=self;

    [imgView addGestureRecognizer:pinch];

    

    

    //创建横扫和纵扫手势

    UISwipeGestureRecognizer *hs=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(hsClick:)];

    hs.direction=UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight;

    [self.view addGestureRecognizer:hs];

    

    

    

    UISwipeGestureRecognizer *vs=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(vsClick:)];

    vs.direction=UISwipeGestureRecognizerDirectionDown|UISwipeGestureRecognizerDirectionUp;

    [self.view addGestureRecognizer:vs];

    

    

    

    

}

-(void)vsClick:(UISwipeGestureRecognizer *)vs

{

    NSLog(@"纵扫");

}

 

 

-(void)hsClick:(UISwipeGestureRecognizer *)hs

{

    NSLog(@"横扫");

}

 

 

 

//当执行捏合手势时调用该方法

-(void)pinchGestureClick:(UIPinchGestureRecognizer *)pinch

{

    pinch.view.transform=CGAffineTransformScale(pinch.view.transform, pinch.scale,pinch.scale);

    [pinch setScale:1];

    

 

}

 

 

 

 

//当执行旋转手势时调用该方法

-(void)rotateGestureClick:(UIRotationGestureRecognizer *)rotate

{

    //roate.rotation手指的旋转弧度

    rotate.view.transform=CGAffineTransformRotate(rotate.view.transform, rotate.rotation);

    //保持图片和手指为相同弧度

    [rotate setRotation:0];

}

 

 

 

 

//当执行长按时调用该方法

//参数名称.view为被添加手势的对象

-(void)longPressGestureClick:(UILongPressGestureRecognizer*)loginPress

{

    loginPress.view.center=self.view.center;

}

 

 

 

//当执行拖拽手势时执行该方法

-(void)panGestureClick:(UIPanGestureRecognizer*)pan

{

    //pan.view为手指点击的对象

    pan.view.center= [pan locationInView:self.view];

}

 

 

 

 

 

//参数为图片的手势类型

-(void)tapGestureClick:(UITapGestureRecognizer *)tap

{

    NSLog(@"tap");

}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

//return YES,允许添加手势的对象多个手势同时执行  

//遵守UIGestureRecognizerDelegate协议

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

    return YES;

}

 (触控)

 

//添加图片到页面上

    UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 75, 75)];

    imgView.image=[UIImage imageNamed:@"a"];

    [self.view addSubview:imgView];

    imgView.tag=100;

    //允许当前屏幕支持多点触控

    self.view.multipleTouchEnabled=YES;

    //允许图片对象支持手势与触控

    imgView.userInteractionEnabled=YES;

    

    

    

    //设置导航条右侧按钮

    UIBarButtonItem *rightBtn=[[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(rightBtnClick:)];

    self.navigationItem.rightBarButtonItem=rightBtn;

    

}

 

-(void)rightBtnClick:(UIBarButtonItem*)bt

{

    _secondController=[[SecondViewController alloc]init];

    _secondController.navigationItem.title=@"Gesture";

    [self.navigationController pushViewController:_secondController animated:YES];

}

 

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

//触控屏幕时调用该方法

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

 

    NSLog(@"touhesBegan");

    //获得屏幕上的手指个数

    NSLog(@"%i",[touches count]);

}

 

 

//当手指在屏幕上滑动调用该方法

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

    UIImageView *imgView=(UIImageView *)[self.view viewWithTag:100];

    

    for (UITouch *t in touches) {

        //t.view获得手指点击的空件对象

        if (t.view==imgView) {

            //图片根据点到图片上的手指的位置而移动

            imgView.center=[t locationInView:self.view];

        }

    }

    

    

    NSLog(@"moving");

}

 

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

    NSLog(@"end");

}

 

  • 相关文章
发表评论
用户名: 匿名