2014-07-15 24 views
6

Bir resme 2 saniye boyunca dokunduğumda ve basılı tuttuğumda bir uyarı kutusu aramaya çalışıyorum. İşte ben bugüne kadar ne var:UIImageView'da bir dokunma ve bekletme nasıl uygulanır?

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UILongPressGestureRecognizer *tapAndHoldGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapAndHoldGesture:)]; 
    tapAndHoldGesture.minimumPressDuration = 0.1; 
    tapAndHoldGesture.allowableMovement = 600; 
    [self.view addGestureRecognizer:tapAndHoldGesture]; 
} 

- (void) handleTapAndHoldGesture:(UILongPressGestureRecognizer *)gestureRecognizer{ 
    if (gestureRecognizer.state != UIGestureRecognizerStateEnded) { 
     return; 
    } 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Gesture:" message:@"hold it" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
} 

bu şey etkileyen, ancak Resim Görünümü programlı sonradan değil, yük oluşturulur emin değilim. Ayrıca

.. herhangi bir yardım takdir olarak şimdiden teşekkürler, ben aşağıdaki bağlantılardan baktım:

Long press gesture on UICollectionViewCell

Long press gesture recognizer on UIButton?

Apple Link 1

Apple Link 2

+0

'a ayarladığınızdan emin olun. Yalnızca işleyici yöntemini –

+0

görüyorum Hatam ... viewDidLoad içinde. Teşekkürler. –

cevap

6
-(void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self setupGesture]; 
} 

-(void) setupGesture 
{ 
    UILongPressGestureRecognizer *lpHandler = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleHoldGesture:)]; 
    lpHandler.minimumPressDuration = 1; //seconds 
    lpHandler.delegate = self; 
    //myUIImageViewInstance - replace for your instance/variable name 
    [**myUIImageViewInstance** addGestureRecognizer:lpHandler]; 
} 

- (void) handleHoldGesture:(UILongPressGestureRecognizer *)gesture 
{ 
    if(UIGestureRecognizerStateBegan == gesture.state) 
    { 
     // Called on start of gesture, do work here 
    } 

    if(UIGestureRecognizerStateChanged == gesture.state) 
    { 
     // Do repeated work here (repeats continuously) while finger is down 
    } 

    if(UIGestureRecognizerStateEnded == gesture.state) 
    { 
     // Do end work here when finger is lifted 
    } 

} 
+0

Hala benim için çalışmıyor ... Görüntüümün 22x22 olması önemli mi? –

+0

Ben öyle düşünmüyorum. userInteractionEnabled yerindelik kontrol Sınıf Referans dayanarak EVET (doğru) olması gerekir: kullanıcı olayları görmezden ve olay kuyruğundan kaldırılır belirler Boolean değeri userInteractionEnabled. @property (nonatomic, getter = isUserInteractionEnabled) BOOL userInteractionEnabled Tartışma Bu özelliğe UIView ana sınıfından miras alınır. Bu sınıf, bu özelliğin varsayılan değerini NO olarak değiştirir. – gabriel

+0

, satır eklemeye gerek kalmadan çalıştı * lpHandler.delegate = self; * – LuAndre

2

UIImageViews varsayılan olarak userInteractionEnabled = NO var. Eğer hareket algılayıcınızı UIImageView örneğine ekliyorsanız, bunu tekrar YES:

İlgili konular