iOS学习笔记(10下)六种手势

来源:互联网 发布:java调用db2存储过程 编辑:程序博客网 时间:2024/06/11 11:22

一、触摸类(UITouch)

//通过触摸类,我们可以实现各式的自定义手势

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

{

#if 0

    NSLog(@"开始触摸!!!");

    //单点触摸

    UITouch *touch=[touches anyObject];

    CGPoint point=[touch locationInView:self.view];

    NSLog(@"===%@",NSStringFromCGPoint(point));


#else

    //多点触摸

   NSSet *touchsSet=[event allTouches];

   for(UITouch *touchin touchsSet)

    {

       CGPoint point=[touch locationInView:self.view];

        NSLog(@"多点%@",NSStringFromCGPoint(point));

    }

#endif

}

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

{

   NSLog(@"触摸结束");

}

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

{

   NSLog(@"触摸移动");

}

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

{

   NSLog(@"来电终断");

}

二、六种手势

1.UITapGestureRecognizer  Tap(敲击手势)

这里演示,在视图中添加了一个ImageView方便演示

-(void)createImageView

{

   UIImageView *imageView=[[UIImageViewalloc]initWithFrame:CGRectMake(100,100, 100, 100)];

    imageView.tag=1;

    NSString *path=[[NSBundlemainBundle] pathForResource:@"10_0"ofType:@"jpg"];

    NSData *data=[NSDatadataWithContentsOfFile:path];

    imageView.image=[UIImageimageWithData:data];

    [self.viewaddSubview:imageView];

    [imageViewrelease];

    //创建敲击手势

    UITapGestureRecognizer *tap=[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tapOnClick:)];

    [imageViewaddGestureRecognizer:tap];//将手势添加道imageView视图上

    imageView.userInteractionEnabled=YES;//UIImageView的与响应用户的touch值默认关闭的这里开启

    [taprelease];

    

    //添加双击手势

    //一个试图可以有多个手势,但是一个手势只能添加道一个试图上

   UITapGestureRecognizer *doubleTap=[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(doubleTapOnClick:)];

    doubleTap.numberOfTapsRequired=2;//指定敲击次数

    [imageViewaddGestureRecognizer:doubleTap];

    [doubleTaprelease];

    //设置优先响应双击,如果双击失败在响应单击手势

    [tap requireGestureRecognizerToFail:doubleTap];

     

}

-(void)tapOnClick:(UITapGestureRecognizer *)tap

{

    NSLog(@"图片被敲击了");

}

-(void)doubleTapOnClick:(UITapGestureRecognizer *)doubleTab

{

    NSLog(@"图片被双击了");

}


2.UIPinchGestureRecognizer Pinch(二指往內或往外拨动,平时经常用到的缩放)捏合


-(void)createImageView

{

   UIImageView *imageView=[[UIImageViewalloc]initWithFrame:CGRectMake(100,100, 100, 100)];

    imageView.userInteractionEnabled=YES;

    imageView.tag=1;

    imageView.image=[UIImageimageNamed:@"girl.png"];

    imageView.center=self.view.center;

    [self.viewaddSubview:imageView];

    [imageViewrelease];

   //创建一个捏合手势

    UIPinchGestureRecognizer *pich=[[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(pichClick:)];

    [imageViewaddGestureRecognizer:pich];

    [pichrelease];

    

}

-(void)pichClick:(UIPinchGestureRecognizer *)pich

{

   static CGFloat scale=1;//设置比例值

    pich.view.transform=CGAffineTransformMakeScale(scale*pich.scale, scale*pich.scale);//将捏合手势的变形属性设置为CGAffineTransformMakeScale(CGFloat sx, CGFloat sy)(缩放:设置缩放比例)仅通过设置缩放比例就可实现视图扑面而来和缩进频幕的效果。

    if(pich.state==UIGestureRecognizerStateEnded)//获取结束后手势的比例值

    {

        scale*=pich.scale;

    }

}



3.UIRotationGestureRecognizer Rotation(旋转


-(void)createImageView

{

   UIImageView *imageView=[[UIImageViewalloc]initWithFrame:CGRectMake(100,100, 100, 100)];

    imageView.userInteractionEnabled=YES;

    imageView.tag=1;

    imageView.image=[UIImageimageNamed:@"girl.png"];

    imageView.center=self.view.center;

    [self.viewaddSubview:imageView];

    [imageViewrelease];

    

    //添加旋转手势

    UIRotationGestureRecognizer *rgr=[[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(rotatonClick:)];

    [imageViewaddGestureRecognizer:rgr];

    [rgrrelease];

    

}

-(void)rotatonClick:(UIRotationGestureRecognizer*)rgr

{

   static CGFloat roatation=0;

    //设置手势的变形属性

    rgr.view.transform=CGAffineTransformMakeRotation(rgr.rotation+roatation);

     //获取弧度

    if(rgr.state==UIGestureRecognizerStateEnded)

    {

        roatation=rgr.rotation;

    }

}




4.UISwipeGestureRecognizerSwipe(滑动,快速移动)


-(void)createImageView

{

   UIImageView *imageView=[[UIImageViewalloc]initWithFrame:CGRectMake(100,100, 100, 100)];

    imageView.userInteractionEnabled=YES;

    imageView.tag=1;

    imageView.image=[UIImageimageNamed:@"10_0.jpg"];

    [self.viewaddSubview:imageView];

    [imageViewrelease];

    

    //创建滑动手势,滑动手势只能设置一个方向

    UISwipeGestureRecognizer *leftSwipe=[[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(SwipeOnClick:)];

    //设置滑动方向 左滑动

    leftSwipe.direction=UISwipeGestureRecognizerDirectionLeft;

    [imageViewaddGestureRecognizer:leftSwipe];

    [leftSwiperelease];

    //设置滑动手势右滑动

    UISwipeGestureRecognizer *rightSwip=[[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(rightOnClick:)];

    rightSwip.direction=UISwipeGestureRecognizerDirectionRight;

     [imageViewaddGestureRecognizer:rightSwip];

    [rightSwiprelease];

}

-(void)rightOnClick:(UISwipeGestureRecognizer *)sgr

{

    if(sgr.direction&&UISwipeGestureRecognizerDirectionRight)

    {

       NSLog(@"相右滑动");

    }

}

-(void)SwipeOnClick:(UISwipeGestureRecognizer*)sgr

{

    if(sgr.direction&UISwipeGestureRecognizerDirectionLeft)

    {

       NSLog(@"向左滑动");

    }

    

}




5.UIPanGestureRecognizer Pan (拖移,慢速移动)


-(void)createImageView

{

   UIImageView *imageView=[[UIImageViewalloc]initWithFrame:CGRectMake(100,100, 100, 100)];

    imageView.userInteractionEnabled=YES;

    imageView.tag=1;

    imageView.image=[UIImageimageNamed:@"girl.png"];

    [self.viewaddSubview:imageView];

    [imageViewrelease];

    //添加拖动手势

    UIPanGestureRecognizer *pan=[[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(panClick:)];

    [imageViewaddGestureRecognizer:pan];

    [panrelease];

    

}

-(void)panClick:(UIPanGestureRecognizer*)pan

{

    //获取拖动时坐标

   CGPoint point=[pan translationInView:pan.view];

    

   static CGPoint center;

    if(pan.state==UIGestureRecognizerStateBegan)

    {

       //记录起始坐标

        center=pan.view.center;

    }

    //视图坐标的偏移量

    pan.view.center=CGPointMake(point.x+center.x, point.y+center.y);

    

}





6.UILongPressGestureRecognizer LongPress(长按)

-(void)createImageView

{

   UIImageView *imageView=[[UIImageViewalloc]initWithFrame:CGRectMake(100,100, 100, 100)];

    imageView.userInteractionEnabled=YES;

    imageView.tag=1;

    imageView.image=[UIImageimageNamed:@"10_0.jpg"];

    [self.viewaddSubview:imageView];

    [imageViewrelease];

    //添加长安手势

    UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(longPressOnClick:)];

    //设置几根手指触摸有效

    longPress.numberOfTouchesRequired=1;

    //设置最小响应时间

    longPress.minimumPressDuration=3;

    [imageViewaddGestureRecognizer:longPress];

    [longPressrelease];

}

-(void)longPressOnClick:(UILongPressGestureRecognizer*)longPress

{

    NSLog(@"图片被长按!");

}


三.手势的四种常用状态(state)


//刚开始生效

UIGestureRecognizerStateBegan,

//每次移动

UIGestureRecognizerStateChanged,

//手势结束

UIGestureRecognizerStateEnded, 

//手势被取消

UIGestureRecognizerStateCancelled,









0 0
原创粉丝点击