2011-01-27 8 views

답변

27

UILongPressGestureRecognizer이 필요합니다. 예를 들어,

UILongPressGestureRecognizer *longPress_gr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(doAction:)]; 
[longPress_gr setMinimumPressDuration:2]; // triggers the action after 2 seconds of press 
[yourButton addGestureRecognizer:longPress_gr]; 

액션을 수 있도록 한 번만 트리거 취득 (예. 2 초 지속 시간이 끝나면,), 당신이 당신의 doAction: 방법은 다음과 같이 보입니다 있는지 확인

- (void)doAction:(UILongPressGestureRecognizer *)recognizer { 

    if (recognizer.state == UIGestureRecognizerStateBegan) { 

     // Your code here 
    } 
} 
+1

나는 당신의 코드를 시험해 보았다. 하지만 내 행동이 두 번 방아쇠를 당기는 데 문제가 하나 있습니다. 2 초 후 첫 번째 동작 트리거 및 끝낼 때 두 번째. – Siddiqui

+0

@Arman : 오 .. 내 대답을 편집했습니다. 나는 그것이 작동해야한다고 생각한다. 확인 해봐. – EmptyStack

+1

Simon에게 감사드립니다. 그것은 나를 위해 정말로 도움이된다. – Siddiqui

2

다른 방법으로 this NBTouchAndHoldButton을 사용할 수 있습니다. 이것은 정확히 당신이 원하는 것이며 구현하기가 매우 쉽습니다.

TouchAndHoldButton * pageDownButton = [TouchAndHoldButton buttonWithType:UIButtonTypeCustom]; 
[pageDownButton addTarget:self action:@selector(pageDownAction:) forTouchAndHoldControlEventWithTimeInterval:0.2]; 

행운을 빈다!

관련 문제