Gesture 기본 사용 요약

4574 단어
1. 퍼팅 제스처(TapGestureRecognizer) 추가
초기화 코드TapGestureRecongnizer 코드는 다음과 같습니다.
//  tap   
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)]; 
//             
tapGesture.numberOfTapsRequired = 1; //     
tapGesture.numberOfTouchesRequired = 1; //      
[self.view addGestureRecognizer:tapGesture]; 

콜백 방법에 적절한 비즈니스 논리를 추가하려면 다음과 같이 하십시오.
//         
-(void)tapGesture: (id)sender 
{ //        
}

2. 길게 제스처(LongPressGestureRecognizer)
코드 초기화
//       
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)]; 
//       
longPressGesture.minimumPressDuration = 0.5; //(2 ) 
[self.view addGestureRecognizer:longPressGesture]; 
                (        ):
 
//         
-(void)longPressGesture:(id)sender 
{ 
UILongPressGestureRecognizer *longPress = sender; 
if (longPress.state == UIGestureRecognizerStateBegan) 
{ 
 
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@“  ” message:@“    ” delegate:nil cancelButtonTitle:@“  ” otherButtonTitles: nil]; 
[alter show]; 
} 
}

코드 설명: 제스처의 상용 상태는 다음과 같습니다. UIGestureRecognizerStateBegan 변경: UIGestureRecognizerStateChanged 종료: UIGestureRecognizerStateEnded 취소: UIGestureRecognizerStateCancelled 실패: UIGestureRecognizerStateFailed 실패: UIGestureRecognizerStateFailed
3. 스윙(SwipeGestureRecognizer)
스쳐를 초기화할 때는 스쳐의 방향을 위아래 좌우로 지정해야 한다.여러 개의 스크랩 방향을 추가하려면 여러 개의 스크랩 제스처를 추가해야 하지만, 리셋은 같은 방법입니다.가볍게 스크래치 제스처를 추가하고 하나는 왼쪽, 하나는 오른쪽, 코드는 다음과 같습니다.
1 //       
2 UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)]; 
3 //        
4 swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; //     
5 [self.view addGestureRecognizer:swipeGesture]; 
 
 
7 //       
8 UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)]; 
9 //        
10 swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft; //     
11 [self.view addGestureRecognizer:swipeGestureLeft];

콜백 방법은 다음과 같습니다.
//         
-(void)swipeGesture:(id)sender 
{ 
UISwipeGestureRecognizer *swipe = sender; 
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) 
{ 
//         
} 
if (swipe.direction == UISwipeGestureRecognizerDirectionRight) 
{ 
 //         
} 
} 

4. 손 모으기(PinchGestureRecognizer)
반죽 초기화
//       
2 UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)]; 
3 [self.view addGestureRecognizer:pinchGesture]; 
          (        ):
 
 ////         
-(void) pinchGesture:(id)sender 
{ 
UIPinchGestureRecognizer *gesture = sender; 
 
 
//      
if (gesture.state == UIGestureRecognizerStateChanged) 
{ 
//     scale          
_imageView.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale); 
} 
 
 
//      
if(gesture.state==UIGestureRecognizerStateEnded) 
{ 
[UIView animateWithDuration:0.5 animations:{ 
_imageView.transform = CGAffineTransformIdentity;//       
}]; 
} 
} 

5. 제스처 드래그(PanGestureRecognizer)
드래그 제스처 초기화
//       
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)]; 
[self.view addGestureRecognizer:panGesture]; 

제스처를 드래그하는 방법 (translation InView를 통해 이동하는 점을 가져오는 것은 터치스 Moved 방법과 유사합니다)
//     
-(void) panGesture:(id)sender 
{ 
UIPanGestureRecognizer *panGesture = sender; 

CGPoint movePoint = [panGesture translationInView:self.view]; 

//        
} 

6. 제스처 회전(RotationGestureRecognizer)
회전 제스처의 초기화
//       
 UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)]; 
 [self.view addGestureRecognizer:rotationGesture]; 

제스처 호출을 회전하는 방법:
1 //     
-(void)rotationGesture:(id)sender 
 { 
 
 
UIRotationGestureRecognizer *gesture = sender; 
 
 
if (gesture.state==UIGestureRecognizerStateChanged) 
{ 
_imageView.transform=CGAffineTransformMakeRotation(gesture.rotation); 
} 
 
 
if(gesture.state==UIGestureRecognizerStateEnded) 
{ 
[UIView animateWithDuration:1 animations:{ 
_imageView.transform=CGAffineTransformIdentity;//     
}]; 
} 

}

좋은 웹페이지 즐겨찾기