2014-07-15 2 views
6

2 초 동안 이미지를 길게 터치하면 경고 상자가 표시됩니다. 여기에 지금까지 가지고있는 작업은 다음과 같습니다UIImageView에서 탭 및 보류를 구현하는 방법은 무엇입니까?

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

이 아무것도 영향을 미친다하지만 이미지보기가 프로그래밍 나중에이 아닌 부하에 생성 된 경우 확실하지. 또한

.. 어떤 도움이 감사로 사전에 감사합니다, 나는 다음과 같은 링크를 살펴 보았다 :

Long press gesture on UICollectionViewCell

Long press gesture recognizer on UIButton?

Apple Link 1

Apple Link 2

+0

이미지 뷰에 제스처를 추가 할 위치로 다시 설정해야합니다. 핸들러 메서드 만 볼 수 있습니다. –

+0

내 실수 ... viewDidLoad. 감사. –

답변

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

여전히 나를 위해 작동하지 않습니다 ... 내 이미지 22x22 중요합니까? –

+0

나는 그렇게 생각하지 않는다. 유효성 검사 userInteractionEnabled는 YES (true) 여야합니다. 클래스 참조 기준 : userInteractionEnabled 사용자 이벤트를 무시하고 이벤트 대기열에서 제거할지 여부를 결정하는 부울 값. @property (nonatomic, getter = isUserInteractionEnabled) BOOL userInteractionEnabled 토론 이 속성은 UIView 부모 클래스에서 상속됩니다. 이 클래스는이 속성의 기본값을 NO로 변경합니다. – gabriel

+0

줄을 추가하지 않고 나를 위해 일했습니다 * lpHandler.delegate = self; * – LuAndre

2

UIImageViews의 기본값은 userInteractionEnabled = NO입니다. 제스처 인식기를 UIImageView의 인스턴스에 추가하는 경우 예 : myImageView.userInteractionEnabled = YES