iPhone에서 손가락에 대한 긴 홀드를 추적하는 방법은 무엇입니까?
문제
2 초 동안 iPhone 화면의 터치와 같은 이벤트를 어떻게 추적 할 수 있습니까? uiwebview에 추가 된 이미지의 이미지를 저장하기 위해 Safari와 마찬가지로?
해결책
Nstimer를 사용하십시오 +scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
당신의 견해에서 -touchesBegan:withEvent:
방법을 취소하고 (사용하십시오 -invalidate
) 안에 -touchesEnded:withEvent:
. 메소드가 선택기가 호출되는 경우 사용자는 타이머 간격을 설정하는 기간 동안 손가락을 뷰에 잡았습니다. 예시:
인터페이스 (.H) :
@interface MyView : UIView
...
NSTimer *holdTimer;
@end
구현 (.m) :
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)evt
{
[holdTimer invalidate];
holdTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(touchWasHeld) userInfo:nil repeats:NO];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)evt
{
[holdTimer invalidate];
holdTimer = nil;
}
- (void)touchWasHeld
{
holdTimer = nil;
// do your "held" behavior here
}
제휴하지 않습니다 StackOverflow