手勢

手勢分爲點擊手勢、捏合、滑動、左右滑動、長按、旋轉等手勢。ide

點擊手勢:動畫

初始化
ui

UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizeralloc] initWithTarget:self
                                           action:@selector(fingerIncident:)];

設置手指數代理

singleFingerOne.numberOfTouchesRequired = 1;

設置點擊數code

singleFingerOne.numberOfTapsRequired = 1;

設置代理orm

singleFingerOne.delegate= self;

添加到所屬的view事件

[self.imageView addGestureRecognizer:singleFingerOne];

當單指單擊與雙擊同時存在時,雙擊時會首先觸發單擊手勢,所以須要對雙擊作註冊ip

[singleFingerOne requireGestureRecognizerToFail:singleFingerTwo];

同理,觸發雙指點擊時也會有先觸發單指手勢的狀況ci

[doubleFingerOne requireGestureRecognizerToFail:doubleFingerTwo];
//手指點擊事件
-
(void)fingerIncident:(UITapGestureRecognizer *)sender
{
   if(sender.numberOfTouchesRequired==1)
{
    //單指點擊事件
    if(sender.numberOfTapsRequired == 1) {
      //單指單擊
      NSLog(@"單指單擊");
      [sender.view.layer removeAllAnimations];
    }
    elseif(sender.numberOfTapsRequired ==2 ){
      //單指雙擊
      NSLog(@"單指雙擊");
    }
  }
   elseif (sender.numberOfTouchesRequired==2)
{
    //雙指點擊事件
    if(sender.numberOfTapsRequired == 1) {
      //雙指單擊
      NSLog(@"雙指單擊");
    }
    elseif(sender.numberOfTapsRequired ==2 ){
      //雙指雙擊
      NSLog(@"雙指雙擊");
    }
  }
}

2.捏合手勢rem

UIPinchGestureRecognizer*pinchGesture=[[UIPinchGestureRecognizer alloc]initWithTarget:self
                                       action:@selector(handlePinchGesture:)];
//imageView添加手勢識別 
[self.imageView addGestureRecognizer:pinchGesture];
//手指捏事件
-(void)handlePinchGesture:(UIPinchGestureRecognizer *)sender{
  NSLog(@"捏");
   
   if([sender state] ==UIGestureRecognizerStateEnded)
{
    lastScale =1.0;
    return;
  }
   
  CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)senderscale]);
   
  CGAffineTransform newTransform =CGAffineTransformScale(sender.view.transform, scale, scale);
   
  [sender.view setTransform:newTransform];
  lastScale = [sender scale];
}

3.旋轉手勢

UIRotationGestureRecognizer *rotateGesture=[[UIRotationGestureRecognizeralloc]initWithTarget:
                                           self
                                           action:@selector(handleRotateGesture:)];
[self.imageView addGestureRecognizer:rotateGesture];
//手指旋轉事件
-(void)handleRotateGesture:(UIRotationGestureRecognizer *)sender{
  NSLog(@"旋轉");
   
   NSLog(@"sender.scale=%f",sender.rotation);
   NSLog(@"sender.velocity=%f",sender.velocity);

  sender.view.transform=CGAffineTransformRotate(sender.view.transform,
sender.rotation);
  sender.rotation=0;
}

4.拖手勢

UIPanGestureRecognizer*panGesture=[[UIPanGestureRecognizer alloc]initWithTarget:self 
                                   action:@selector(handlePanGesture:)];  
[self.imageView addGestureRecognizer:panGesture];
//手指拖事件
-(void)handlePanGesture:(UIPanGestureRecognizer *)sender{
  NSLog(@"拖");
  NSLog(@"sender=%@",sender);
   CGPoint translation=[(UIPanGestureRecognizer*)sendertranslationInView:self.view];
   
  //開始
   if(sender.state==UIGestureRecognizerStateBegan) {
    x=sender.view.center.x;
    y=sender.view.center.y;
  }
   //進行中
   if(sender.state==UIGestureRecognizerStateChanged)
{
    sender.view.center=CGPointMake(x+translation.x, y+translation.y);
  }
  //結束
   if(sender.state==UIGestureRecognizerStateEnded) {
    sender.view.center=CGPointMake(x+translation.x, y+translation.y);
  }
}

5.左劃

UISwipeGestureRecognizer*swipeLeftGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self
                                           action:@selector(handleSwipeGesture:)]; 
swipeGesture.direction=UISwipeGestureRecognizerDirectionLeft;//不設置默認是右
[self.imageView addGestureRecognizer:swipeLeftGesture];

6.右劃

UISwipeGestureRecognizer*swipeGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self 
                                      action:@selector(handleSwipeGesture:)]; 
 [self.imageView addGestureRecognizer:swipeGesture];
//手指左右滑動事件
-(void)handleSwipeGesture:(UISwipeGestureRecognizer *)sender{
   if(sender.direction==UISwipeGestureRecognizerDirectionLeft)
{
    NSLog(@"左滑");
  }
  else{
    NSLog(@"右滑");
  }
}

7.長按

UILongPressGestureRecognizer *longpressGesutre=[[UILongPressGestureRecognizeralloc]initWithTarget:self
                                                action:@selector(handleLongpressGesture:)];
   //長按時間爲1秒
  longpressGesutre.minimumPressDuration=0.5;
   //容許15秒中運動
  longpressGesutre.allowableMovement=3;
   //所需觸摸1次
  longpressGesutre.numberOfTouchesRequired=1;
  [self.imageView addGestureRecognizer:longpressGesutre];
  [longpressGesutrerelease];
}
//手指長按事件
-(void)handleLongpressGesture:(UILongPressGestureRecognizer *)sender{
  NSLog(@"長按");
   if(sender.state==UIGestureRecognizerStateBegan) {
    CABasicAnimation* rotationAnimation_ =
[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    //角度轉弧度
    rotationAnimation_.fromValue = [NSNumber numberWithFloat:(-0.02)];
    rotationAnimation_.toValue = [NSNumber numberWithFloat:(0.02)];
    //動畫時間
    rotationAnimation_.duration = 0.1f;
    //動畫次數
    rotationAnimation_.repeatCount=FLT_MAX;
    //回退動畫
    rotationAnimation_.autoreverses = YES;
    //動畫開始結束的快慢,設置爲加速
    rotationAnimation_.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [[sender.view layer] addAnimation:rotationAnimation_forKey:@"revItUpAnimation"];
  }
}
相關文章
相關標籤/搜索