2013-07-29 34 views
32

Tôi đang cố gắng NSLog khi tôi vuốt qua UIImageView bằng mã này, nhưng nó không hoạt động vì một lý do nào đó. Bất kỳ ý tưởng ?Trình nhận dạng cử chỉ (vuốt) trên UIImageView

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UIImage *image = [UIImage imageNamed:@"image2.png"]; 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 

    imageView.frame = [[UIScreen mainScreen] bounds]; 

    [self.view addSubview:imageView]; 

    UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)]; 
    [recognizer setNumberOfTouchesRequired:1]; 
    [imageView addGestureRecognizer:recognizer]; 

} 

- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer { 
    NSLog(@"right swipe"); 
} 

Trả lời

102

Bật Tương tác người dùng chế độ xem theo số điện thoại bị tắt theo mặc định.

[imageView setUserInteractionEnabled:YES]; 

Thêm một Swipe Gesture Sự kiện

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 
[imageView addGestureRecognizer:swipeLeft]; 
[imageView addGestureRecognizer:swipeRight]; 

Xử lý Swipe Gesture Sự kiện

- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe { 

    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) { 
     NSLog(@"Left Swipe"); 
    } 

    if (swipe.direction == UISwipeGestureRecognizerDirectionRight) { 
     NSLog(@"Right Swipe"); 
    } 

} 
+0

Sẽ không hiệu quả hơn nếu có 'else if' trong hàm' handleSwipe'? – shadowmoses

+0

Có, bạn có thể ... Mã trên chỉ dành cho mục đích đại diện. – icodebuster

3

khác nhau hơn khác UIViews, UIImageView, như mặc định, đi kèm với tương tác người dùng disabled. Bao gồm dòng này trong mã của bạn và cử chỉ nên làm việc như mong đợi:

imageView.userInteractionEnabled = YES; 
9

Hãy chắc chắn để thêm imageView.userInteractionEnabled = YES; sau khi bạn tạo UIImageView của bạn.

Điều này cho phép người dùng tương tác với chế độ xem của bạn chẳng hạn như nhấn, kéo, vuốt hoặc các cử chỉ chung khác.

Xem tài liệu here.

5

Trong Swift 3

ImageView.isUserInteractionEnabled = true 
let swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(self.getSwipeAction(_:))) 
self.ImageView.addGestureRecognizer(swipeGesture) 

func getSwipeAction(_ recognizer : UISwipeGestureRecognizer){ 

    if recognizer.direction == .right{ 
     print("Right Swiped") 
    } else if recognizer.direction == .left { 
     print("Left Swiped") 
    } 
} 
0

tôi đã chạy vào rắc rối với điều này, nơi tôi có thể lấy UISwipeGestureRecognizer để làm việc nếu tôi đã thêm nó vào self.view, chứ không phải cho UIImageView riêng lẻ. Cảm ơn Jeff Ayan vì ví dụ về mã Swift 3. Nếu bạn sử dụng trình tạo giao diện, bạn cũng có thể chọn hộp Người dùng tương tác đã bật cho UIImageView trong trình kiểm tra thuộc tính.

Các vấn đề liên quan