iOS
UIGestureRecognizer
수색…
UITapGestureRecognizer
초기화 UITapGestureRecognizer
대상으로 self
이 경우에, 및 action
A : 단일 파라미터가있는 방법이다 UITapGestureRecognizer
.
초기화 후에는 탭을 인식해야하는보기에 추가하십시오.
빠른
override func viewDidLoad() {
super.viewDidLoad()
let recognizer = UITapGestureRecognizer(target: self,
action: #selector(handleTap(_:)))
view.addGestureRecognizer(recognizer)
}
func handleTap(recognizer: UITapGestureRecognizer) {
}
목표 -C
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *recognizer =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleTap:)];
[self.view addGestureRecognizer:recognizer];
}
- (void)handleTap:(UITapGestureRecognizer *)recognizer {
}
UITapGestureRecognizer를 통한 키보드 해지의 예 :
먼저 키보드를 닫는 함수를 만듭니다.
func dismissKeyboard() {
view.endEditing(true)
}
그런 다음 탭 컨트롤러에 탭 제스처 인식기를 추가하여 방금 만든 메서드 호출
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
view.addGestureRecognizer(tap)
제스처 위치 가져 오기 예제 UITapGestureRecognizer (Swift 3) :
func handleTap(gestureRecognizer: UITapGestureRecognizer) {
print("tap working")
if gestureRecognizer.state == UIGestureRecognizerState.recognized
{
print(gestureRecognizer.location(in: gestureRecognizer.view))
}
}
UIPanGestureRecognizer
팬 제스처 인식기는 드래그 동작을 감지합니다. 다음 예제에서는 이미지를보기 컨트롤러에 추가하고 사용자가 화면에서 이미지를 드래그 할 수 있도록합니다.
목표 -C
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imageToDrag"]];
[imageView sizeToFit];
imageView.userInteractionEnabled = YES;
[self.view addSubview:imageView];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[imageView addGestureRecognizer:pan];
}
- (void)handlePan:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointZero inView:self.view];
}
빠른
override func viewDidLoad() {
super.viewDidLoad()
let imageView = UIImageView.init(image: UIImage.init(named: "imageToDrag"))
imageView.sizeToFit()
imageView.isUserInteractionEnabled = true
self.view.addSubview(imageView)
let pan = UIPanGestureRecognizer.init(target: self, action: #selector(handlePan(recognizer:)))
imageView.addGestureRecognizer(pan)
}
func handlePan(recognizer: UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
if let view = recognizer.view {
view.center = CGPoint(x: view.center.x + translation.x, y: view.center.y + translation.y)
}
recognizer.setTranslation(CGPoint.zero, in: self.view)
}
참고 :
UIPanGestureRecognizer
가 드래그 동작을 감지하는 데 유용하지만 사용자가 손가락을 왼쪽 / 오른쪽 또는 위 / 아래로 드래그하는 등의 기본 동작을 감지하려는 경우UISwipeGestureRecognizer
사용UISwipeGestureRecognizer
.UIPanGestureRecognizer
는translationInView:
또는velocityInView:
와 같은 메소드에 액세스해야하는 경우 더 나은 선택입니다.
UITapGestureRecognizer (더블 탭)
단일 탭과 마찬가지로 더블 탭은 UITapGestureRecognizer
사용합니다. numberOfTapsRequired
를 2
설정하기 만하면됩니다.
빠른
override func viewDidLoad() {
super.viewDidLoad()
// Double Tap
let doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap))
doubleTapGesture.numberOfTapsRequired = 2
doubleTapView.addGestureRecognizer(doubleTapGesture)
}
// Double tap action
func handleDoubleTap() {
label.text = "Double tap recognized"
}
노트
UILongPressGestureRecognizer
UILongPressGestureRecognizer
사용하면보기를 길게 누를 수 있습니다. 작업 메서드가 호출되기 전에 지연 시간을 설정할 수 있습니다.
빠른
override func viewDidLoad() {
super.viewDidLoad()
// Long Press
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(_:)))
longPressView.addGestureRecognizer(longPressGesture)
}
// Long press action
func handleLongPress(gesture: UILongPressGestureRecognizer) {
if gesture.state == UIGestureRecognizerState.Began {
label.text = "Long press recognized"
}
}
노트
UISwipeGestureRecognizer
스 와이프 제스처를 사용하면 손가락을 특정 방향으로 빠르게 움직이는 사용자의 말을들을 수 있습니다.
빠른
override func viewDidLoad() {
super.viewDidLoad()
// Swipe (right and left)
let swipeRightGesture = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(_:)))
let swipeLeftGesture = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(_:)))
swipeRightGesture.direction = UISwipeGestureRecognizerDirection.Right
swipeLeftGesture.direction = UISwipeGestureRecognizerDirection.Left
swipeView.addGestureRecognizer(swipeRightGesture)
swipeView.addGestureRecognizer(swipeLeftGesture)
}
// Swipe action
func handleSwipe(gesture: UISwipeGestureRecognizer) {
label.text = "Swipe recognized"
// example task: animate view off screen
let originalLocation = swipeView.center
if gesture.direction == UISwipeGestureRecognizerDirection.Right {
label.text = "Swipe right"
} else if gesture.direction == UISwipeGestureRecognizerDirection.Left {
label.text = "Swipe left"
}
}
목표 -C
- (void)viewDidLoad
{
[super viewDidLoad];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
// Setting the swipe direction.
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
// Adding the swipe gesture on image view
[self.view addGestureRecognizer:swipeLeft];
[self.view addGestureRecognizer:swipeRight];
}
//Handling Swipe Gesture Events
- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"Left Swipe");
}
if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"Right Swipe");
}
}
노트
UIPinchGestureRecognizer
핀치는 손가락이 서로 가깝게 또는 멀리 움직이는 두 손가락 제스처입니다. 이 제스처는 일반적으로보기의 크기를 조정하는 데 사용됩니다.
빠른
override func viewDidLoad() {
super.viewDidLoad()
// Pinch
let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(handlePinch(_:)))
pinchView.addGestureRecognizer(pinchGesture)
}
// Pinch action
func handlePinch(gesture: UIPinchGestureRecognizer) {
label.text = "Pinch recognized"
if gesture.state == UIGestureRecognizerState.Changed {
let transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale)
pinchView.transform = transform
}
}
노트
UIRotationGestureRecognizer
UIRotationGestureRecognizer
를 사용하여 중심 주위를 회전하는 두 손가락을들을 수 있습니다. 이것은 일반적으로보기를 회전하는 데 사용됩니다.
빠른
override func viewDidLoad() {
super.viewDidLoad()
// Rotate
let rotateGesture = UIRotationGestureRecognizer(target: self, action: #selector(handleRotate(_:)))
rotateView.addGestureRecognizer(rotateGesture)
}
// Rotate action
func handleRotate(gesture: UIRotationGestureRecognizer) {
label.text = "Rotate recognized"
if gesture.state == UIGestureRecognizerState.Changed {
let transform = CGAffineTransformMakeRotation(gesture.rotation)
rotateView.transform = transform
}
}
노트
인터페이스 작성기에 제스처 인식기 추가
제스처 인식기를 객체 라이브러리에서보기로 드래그하십시오.
Outlet 및 Action을 만들기 위해 Document Outline의 제스처에서 View Controller 코드로 드래그 앤 드롭을 제어하십시오.
노트
- 이 예제는 제스처 인식기를 시연하는 이 풀러 샘플 프로젝트 에서 가져온 것입니다.