ios中的6种手势

//轻拍

    UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeColor:)];

    tapGesture.numberOfTouchesRequired=1;

    tapGesture.numberOfTapsRequired=2;

    [view addGestureRecognizer:tapGesture];

    [tapGesture release];

- (void)changeColor:(UITapGestureRecognizer *)tap

{

    //NSLog(@"%d",tap.numberOfTouchesRequired);

    [self.view viewWithTag:100].backgroundColor=[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];

}


//平移

    UIView *view=[self.view viewWithTag:100];

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

    [view addGestureRecognizer:panGesture];

    [panGesture release];

- (void)pan:(UIPanGestureRecognizer *)pan

{

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

    //2D仿射变换

    //1.不完善的,下次不能从最初的frame开始

    //pan.view.transform=CGAffineTransformMakeTranslation(point1.x, point1.y);

    //2.无论移动多少次,当下一次再平移的时候都会冲最初的frame开始

    pan.view.transform=CGAffineTransformTranslate(pan.view.transform, point1.x,point1.y);


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

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

//    NSLog(@"%@",NSStringFromCGPoint([pan translationInView:pan.view]));

}


  //捏合

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

   [view addGestureRecognizer:pinchGesture];

   [pinchGesture release];

- (void)pinch:(UIPinchGestureRecognizer *)pinch

{

    //每次都会从最初的frame开始缩放

    //pinch.view.transform=CGAffineTransformMakeScale(pinch.scale, pinch.scale);

    

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

    pinch.scale=1;//将当前的scale1;

    

}


  //旋转

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

  [view addGestureRecognizer:rotationGesture];

   [rotationGesture release];

- (void)rotate:(UIRotationGestureRecognizer *)rotate

{

    //rotate.view.transform=CGAffineTransformMakeRotation(rotate.rotation);

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

    //rotate.rotation=0;

}


//长按

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

   [view addGestureRecognizer:longPress];

  [longPress release];

- (void)longPress:(UILongPressGestureRecognizer *)longPress

{

    if (longPress.state==UIGestureRecognizerStateBegan) {

        [self.view viewWithTag:100].backgroundColor=[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];

    }

   

}


//清扫

   UISwipeGestureRecognizer *swipeGesture1=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swip1:)];

   swipeGesture1.direction=UISwipeGestureRecognizerDirectionDown;

    [view addGestureRecognizer:swipeGesture1];

   [swipeGesture1 release];

//给当前的视图添加四个不同方向的清扫

   UISwipeGestureRecognizer *swipeGesture1=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swip1:)];

   swipeGesture1.direction=UISwipeGestureRecognizerDirectionDown;

   [view addGestureRecognizer:swipeGesture1];

    [swipeGesture1 release];

   

  

   UISwipeGestureRecognizer *swipeGesture2=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swip2:)];

  swipeGesture2.direction=UISwipeGestureRecognizerDirectionUp;

   [view addGestureRecognizer:swipeGesture2];

   [swipeGesture2 release];

   

  

   UISwipeGestureRecognizer *swipeGesture3=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swip3:)];

   swipeGesture3.direction=UISwipeGestureRecognizerDirectionRight;

    [view addGestureRecognizer:swipeGesture3];

    [swipeGesture3 release];

    

   UISwipeGestureRecognizer *swipeGesture4=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swip4:)];

   swipeGesture4.direction=UISwipeGestureRecognizerDirectionLeft;

   [view addGestureRecognizer:swipeGesture4];

   [swipeGesture4 release];

- (void)swip1:(UISwipeGestureRecognizer *)swip1

{

    NSLog(@"Down");

}


- (void)swip2:(UISwipeGestureRecognizer *)swip2

{

     NSLog(@"Up");

}

- (void)swip3:(UISwipeGestureRecognizer *)swip3

{

     NSLog(@"Right");

}

- (void)swip4:(UISwipeGestureRecognizer *)swip4

{

     NSLog(@"Left");

}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值