2013-07-29 18 views
32

Bir UIImageView'ı bu kodla kaydırdığımda NSLog'a çalışıyorum ancak bazı nedenlerle çalışmaz. Herhangi bir fikir ? UIImageView, UIViews diğerinden daha farklıHareket algılayıcısı (hızlıca kaydırma) üzerinde 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"); 
} 

cevap

102

Enable UIImage kullanıcı etkileşimini görüntüle varsayılan olarak devre dışıdır.

[imageView setUserInteractionEnabled:YES]; 

eklenmesi Swipe Hareket Etkinlikleri

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]; 

Taşıma Swipe Hareket Etkinlikleri

Swift 3'te
- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe { 

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

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

} 
+0

'handleSwipe' işlevinde' if if 'seçeneğinin olması daha verimli olmaz mıydı? – shadowmoses

+0

Evet ... Yukarıdaki kod sadece gösterim amaçlıydı. – icodebuster

3

, varsayılan olarak, kullanıcı etkileşimi disabled ile geliyor. Kodunuzdaki bu satırı ekleyin ve beklendiği gibi jest çalışması gerekir:

imageView.userInteractionEnabled = YES; 
9

Eğer UIImageView oluşturduktan sonra imageView.userInteractionEnabled = YES; eklemeyi unutmayın.

Bu, kullanıcıların aracılığıyla, dokunma, sürükleme, hızlıca kaydırma veya diğer genel hareketler gibi görünümleriyle etkileşime girmesine olanak sağlar.

here belgelerine bakın.

5

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

elimden Bununla belaya koşuyordu UISwipeGestureRecognizer'ı al Ben self.view ekledim, ancak bireysel UIImageView için işe yaramazsa. Swift 3 kod örneği için teşekkürler Jeff Ayan. Arabirim oluşturucu kullanıyorsanız, öznitelik denetçisinde UIImageView için Kullanıcı Etkileşim Etkinleştir kutusunu da işaretleyebilirsiniz.

İlgili konular