iOS - 제스처 세부 매개 변수 설명

5566 단어
손짓을 치다

    //     UILabel     (            )
    UILabel *demoLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    //         
    //   1.       
    //   2.           
    UITapGestureRecognizer *tapFs = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapFs:)];
    //          
    [demoLabel addGestureRecognizer:tapFs];
    //       
    tapFs.numberOfTapsRequired = 1; //  
    //         
    tapFs.numberOfTouchesRequired = 1;
    //         
    UITapGestureRecognizer *tapSe = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSe:)];
    //          
    [demoLabel addGestureRecognizer:tapSe];
    //       
    tapSe.numberOfTapsRequired = 2;
    //                  
    [tapFs requireGestureRecognizerToFail:tapSe];

-(void)tapGesture:(UITapGestureRecognizer *)tap{
    NSLog(@"   demoLabel");
}

-(void)twoTapGesture:(UITapGestureRecognizer *)tap{
    NSLog(@"   demoLabel");
}


손짓

    //       
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    //          
    [demoLabel addGestureRecognizer:longPress];
    //          
    longPress.minimumPressDuration = 3;//  0.5s
    //              ,   ,    
    longPress.allowableMovement = 50;

-(void)longPress:(UILongPressGestureRecognizer *)longPress{
    
    //【  】        ,         ,                          
    //  :          ,            
    
    if (longPress.state == UIGestureRecognizerStateEnded) {
        NSLog(@"   demoLabel");
    }
    
}


슬라이딩 제스처

//       
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
    [demoLabel addGestureRecognizer:swipe];
    /**    
     UISwipeGestureRecognizerDirectionRight
     UISwipeGestureRecognizerDirectionLeft
     UISwipeGestureRecognizerDirectionUp
     UISwipeGestureRecognizerDirectionDown
     */
    //   :            
    swipe.direction = UISwipeGestureRecognizerDirectionRight;

    -(void)swipe:(UISwipeGestureRecognizer *)sw{
    switch (sw.direction) {
        case UISwipeGestureRecognizerDirectionRight:
            NSLog(@"    ");
            break;
        case UISwipeGestureRecognizerDirectionLeft:
            NSLog(@"    ");
            break;
        case UISwipeGestureRecognizerDirectionUp:
            NSLog(@"    ");
            break;
        case UISwipeGestureRecognizerDirectionDown:
            NSLog(@"    ");
            break;
            
        default:
            NSLog(@"    ");
            break;
    }
}


드래그 제스처
    //       
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
    [demoLabel addGestureRecognizer:pan];
    //        (max  ,min  )
    pan.maximumNumberOfTouches = 3;
    pan.minimumNumberOfTouches = 1;

//          ,        
-(void)panGesture:(UIPanGestureRecognizer *)pan{
    //                  
    CGPoint point = [pan translationInView:self.view];
    //        
    demoLabel.center = CGPointMake(demoLabel.center.x + point.x, demoLabel.center.y + point.y);
    //                 
    [pan setTranslation:CGPointZero inView:self.view];
}


회전 제스처
    //      
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
    [demoLabel addGestureRecognizer:rotation];
    
//           
-(void)rotationGesture:(UIRotationGestureRecognizer *)rotation{
    
    demoLabel.transform = CGAffineTransformRotate(demoLabel.transform, rotation.rotation);
    
    //      
    rotation.rotation = 0;
}


손짓
//          
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
//             
    [demoLabel addGestureRecognizer:pinch];

//          
-(void)pinchGesture:(UIPinchGestureRecognizer *)pinch{
    
    demoLabel.transform = CGAffineTransformScale(_label.transform, pinch.scale, pinch.scale);
    
    //    
    pinch.scale = 1;
}


두 가지 제스처가 같은 보기에 작용한다

//              
    
    //      
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
    rotation.delegate = self;
    [demoLabel addGestureRecognizer:rotation];
    
    
    //      
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
    pinch.delegate = self;
    //         
    [demoLabel addGestureRecognizer:pinch];

//           
-(void)rotationGesture:(UIRotationGestureRecognizer *)rotation{
    
    demoLabel.transform = CGAffineTransformRotate(demoLabel.transform, rotation.rotation);
    
    //      
    rotation.rotation = 0;
}
//          
-(void)pinchGesture:(UIPinchGestureRecognizer *)pinch{
    
    demoLabel.transform = CGAffineTransformScale(demoLabel.transform, pinch.scale, pinch.scale);
    
    //    
    pinch.scale = 1;
}

//               ,           ,        ,                        
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    //                    ,         ,      YES
    return YES;
}

좋은 웹페이지 즐겨찾기