-1

我想創建並看到一個uiimageview,當我點擊屏幕。 在我擡起手指之前,我想在屏幕周圍移動uiimageview,並且只在手指離開時才移動圖像。繼承人我做了什麼:UITapGestureRecognizer和UIPanGestureRecognizer

- (IBAction)tap:(UITapGestureRecognizer *)recognizer { 

    CGPoint location = [recognizer locationInView:self.view]; 

    UIImageView *circle = [[UIImageView alloc] initWithFrame:CGRectMake(location.x, location.y, 50, 50)]; 
    circle.userInteractionEnabled = YES; 
    [circle setImage:[UIImage imageNamed:@"monkey_1.png"]]; 
    [self.view addSubview:circle]; 

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:circle action:nil]; 
    [recognizer requireGestureRecognizerToFail:pan]; 

    CGPoint translation = [pan translationInView:circle]; 
    pan.view.center = CGPointMake(pan.view.center.x + translation.x, pan.view.center.y + translation.y); 
    [pan setTranslation:CGPointMake(0, 0) inView:self.view]; 
} 
+0

這對於單個手勢可能會更容易。做一個長按手勢,並在它的動作檢測狀態。一開始你會顯示圖像,改變你會移動圖像,然後清理結束。 – farski

回答

0

你可以只是一個UIPanGestureRecognizerUILongPressGestureRecognizer做到這一點。在手勢處理方法中,檢查識別器的state屬性,並在UIGestureRecognizerStateEnded(即用戶將手指從屏幕上擡起)時顯示圖像。例如:

- (void)handleGesture:(UILongPressGestureRecognizer *)recognizer { 
    if(recognizer.state == UIGestureRecognizerStateEnded) { 
     // gesture ended: show the image 
    } 
    else if(recognizerState == UIGestureRecognizerStateBegan) { 
     // this code runs when the gesture is started. 
    } 
    else if(recognizerState == UIGestureRecognizerStateChanged) { 
     // gesture is in progress 
    } 
} 
相關問題