码迷,mamicode.com
首页 > 移动开发 > 详细

ios手势操作,四个基本事件与六个常用事件

时间:2015-08-25 16:28:33      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

基本事件包括begin,canceled,move,ended四项,如果对象的hidden属性为yes,则无效果,hidden属性必须为no;才能使用:

 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{//触碰开始

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

    if ([[event allTouches]count]==2) {

        NSArray * one =[[event allTouches]allObjects];

        _tiLabel.hidden=NO;

        _yLabel.hidden=NO;

        _tiLabel.center=[[one objectAtIndex:0] locationInView:self.view];

        _yLabel.center=[[one objectAtIndex:1] locationInView:self.view];

    }

}

 

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{//触控发生意外终止是

_tiLabel.hidden=YES;_yLabel.hidden=YES;

}

 

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{//触控结束时

     _tiLabel.hidden=YES;_yLabel.hidden=YES;

}

 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event//触控移动时

{

//    NSLog(@"%ld",[[event allTouches]count]);

 

    if ([[event allTouches]count]==2) {

        NSArray * one =[[event allTouches] allObjects];

        _tiLabel.hidden=NO;

        _yLabel.hidden=NO;

        _tiLabel.center=[[one objectAtIndex:0] locationInView:self.view];

        _yLabel.center=[[one objectAtIndex:1] locationInView:self.view];

    }

 

}

下面是六大常用事件,包括:点击,拖动,捏合,旋转,长按以及轻扫

  点击事件:顾名思义(UITapGestureRecognizer)

  拖动事件:拖动view内的对象(UIPanGestureRecognizer)

  捏合事件:主要用于操作对象的方法以及缩小(UIPinchGestureRecognizer)

  旋转事件:主要用于控制对象的旋转角度(UIRotationGestureRecognizer)

  长按事件:顾名思义(UILongPressGestureRecognizer)

  清扫事件:主要add在view内,轻扫又可以按照属性分为上下左右四向的清扫(UISwipeGestureRecognizer)

想对某个对象添加六大事件,对象的userinteractionEnable属性必须为yes;否则六大事件会无响应:

 

 

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    UIImage * pic =[UIImage imageNamed:@"rmb.jpg"];

    UIImageView * imgView = [[UIImageView alloc]initWithImage:pic];

    imgView . backgroundColor = [UIColor blackColor];

    imgView.frame =CGRectMake(0, 0, 300, 200);

    imgView . userInteractionEnabled=YES;/**********该项设置必须为yes************/

    [self.view addSubview:imgView];//imageview对象的代码创建

   

/*------------------------下面是事件对象的创建以及add操作---------------------*/

    //点击

    UITapGestureRecognizer * tapGestureRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];

    tapGestureRecognizer.numberOfTapsRequired=1;

    tapGestureRecognizer.numberOfTouchesRequired=1;

  //设置点击事件的单击次数以及手指个数

    [imgView addGestureRecognizer:tapGestureRecognizer];

 

    //拖动

    UIPanGestureRecognizer * panGestureRecognizer=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];

    [imgView addGestureRecognizer:panGestureRecognizer];

 

    //旋转

    UIRotationGestureRecognizer * rotationGestureRecognizer =  [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotation:)];

    [imgView addGestureRecognizer:rotationGestureRecognizer];

 

    //捏合

    UIPinchGestureRecognizer * pinchGestureRecognizer= [[UIPinchGestureRecognizer alloc]initWithTarget:self  action:@selector(handlePinch:)];

    [imgView addGestureRecognizer:pinchGestureRecognizer];

 

    //长按

    UILongPressGestureRecognizer * longGestureRecognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLong:)];

    [imgView addGestureRecognizer:longGestureRecognizer];

 

    //清扫

    UISwipeGestureRecognizer * leftGestureRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipe:)];

    leftGestureRecognizer.direction=UISwipeGestureRecognizerDirectionLeft;//设置清扫的方向

    UISwipeGestureRecognizer * rightGestureRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipe:)];

    rightGestureRecognizer.direction=UISwipeGestureRecognizerDirectionRight;//设置清扫的方向

    [self.view addGestureRecognizer:leftGestureRecognizer];

    [self.view addGestureRecognizer:rightGestureRecognizer];

}

 

-(void)handleTap:(UITapGestureRecognizer * )recognizer {

//     NSLog(@"向左清扫");

    [[[UIAlertView alloc]initWithTitle:@"提示" message:@"点击事件发生" delegate:self cancelButtonTitle:nil otherButtonTitles:@"ok",nil] show];

}//点击处理

 

 

-(void)handlePan:(UIPanGestureRecognizer *) recognizer{

    UIImageView * current=(UIImageView *)recognizer.view;//获取imageview对象

    CGPoint translaation=[recognizer translationInView:recognizer.view];//获得移动的坐标

    current.center=CGPointMake(current.center.x+translaation.x, current.center.y+translaation.y);

    //使用原先坐标加上移动后的坐标,赋值给imageview对象

    [recognizer setTranslation:CGPointZero inView:self.view];

    //清零,防止再次移动

}//拖动处理

 

 

 

-(void)handleRotation:(UIRotationGestureRecognizer *) recognizer{

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

}//旋转处理

-(void) handlePinch:(UIPinchGestureRecognizer *) recognizer{

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

    recognizer.scale=1;

}//捏合处理

 

-(void) handleSwipe:(UISwipeGestureRecognizer *) recognizer{

    if (recognizer.direction==UISwipeGestureRecognizerDirectionRight) {

        NSLog(@"向右清扫");

    }else if(recognizer.direction==UISwipeGestureRecognizerDirectionLeft){

        NSLog(@"向左清扫");

    }

}//清扫处理

 

 

-(void) handleLong:(UILongPressGestureRecognizer *) recognizer{

    NSLog(@"长按事件");

}//长按处理

 

 

ios手势操作,四个基本事件与六个常用事件

标签:

原文地址:http://www.cnblogs.com/thxios/p/4757548.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!