我想知道如果有人知道如何實現「觸摸裏面」的反應,當用戶按下然後舉起他們的手指touchesBegan
,touchesEnded
方法。我知道這可以通過UITapGestureRecognizer
來完成,但實際上我正在努力使其僅適用於快速點擊(使用UITapGestureRecognizer
,如果您長時間握住手指,然後擡起,它仍會執行)。任何人都知道如何實現這一點?如何實現在觸摸觸摸內部touckingBegin,touchesEnded
2
A
回答
4
使用UILongPressGesturizer
實際上是一個更好的解決方案,以模仿所有的UIButton
(touchUpInside
,touchUpOutside
,touchDown
等)的功能:
- (void) longPress:(UILongPressGestureRecognizer *)longPressGestureRecognizer
{
if (longPressGestureRecognizer.state == UIGestureRecognizerStateBegan || longPressGestureRecognizer.state == UIGestureRecognizerStateChanged)
{
CGPoint touchedPoint = [longPressGestureRecognizer locationInView: self];
if (CGRectContainsPoint(self.bounds, touchedPoint))
{
[self addHighlights];
}
else
{
[self removeHighlights];
}
}
else if (longPressGestureRecognizer.state == UIGestureRecognizerStateEnded)
{
if (self.highlightView.superview)
{
[self removeHighlights];
}
CGPoint touchedPoint = [longPressGestureRecognizer locationInView: self];
if (CGRectContainsPoint(self.bounds, touchedPoint))
{
if ([self.delegate respondsToSelector:@selector(buttonViewDidTouchUpInside:)])
{
[self.delegate buttonViewDidTouchUpInside:self];
}
}
}
}
2
您可以通過創建一個UIView子類並在其中實現來實現touchesBegan和touchesEnded。
但是,您也可以使用UILongPressGestureRecognizer並獲得相同的結果。
0
你可以創建一些BOOL
變量然後在-touchesBegan
檢查什麼樣的視圖或任何你需要被觸摸,並將此BOOL
變量設置爲YES
。之後,在-touchesEnded
檢查這個變量是否爲YES
,並且您的視圖或任何您需要的內容被觸及,這將是您的-touchUpInside
。之後當然設置BOOL
變量爲NO
。
0
您可以添加一個UTapGestureRecognizer
和一個UILongPressGestureRecognizer
,並使用[tap requiresGestureRecognizerToFail:longPress];
(點擊並長按是添加的識別器的對象)添加依賴項。
這樣,如果長時間按下,水龍頭將不會被檢測到。
2
我通過在touchesBegan中觸發一個計時器來做到這一點。如果這個計時器在touchesEnded被調用時仍在運行,那麼執行你想要的任何代碼。這給了touchUpInside的效果。
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSTimer *tapTimer = [[NSTimer scheduledTimerWithTimeInterval:.15 invocation:nil repeats:NO] retain];
self.tapTimer = tapTimer;
[tapTimer release];
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([self.tapTimer isValid])
{
}
}
相關問題
- 1. 觸摸UIButton觸摸
- 2. 在可可觸摸中實現基於觸摸的旋轉
- 3. 基本觸摸ID實現
- 4. 觸摸內部的UIImageView
- 5. 觸摸後如何檢測觸摸
- 6. 如何在應用程序中實現單點觸摸和多點觸摸?
- 7. 如何檢測UITableViewCell內部的觸摸
- 8. 實現多點觸摸......需要存儲的第二觸摸
- 9. 在JPA實體上實現「觸摸」?
- 10. UIButton同時觸摸內部和觸摸重複?
- 11. 觸摸時更大觸摸
- 12. 觸摸ID實施
- 13. 可可觸摸 - 在UIImageView中觸摸
- 14. 觸摸
- 15. 觸摸
- 16. 觸摸
- 17. 觸摸在UIScrollView
- 18. 在觸摸
- 19. 如何在CSS中只觸摸非觸摸設備?
- 20. 如何重置NSTimer的觸摸,觸摸移動在ios
- 21. 如何使在支持3D觸摸(力觸摸)
- 22. 如何在一次觸摸後禁用CGRect/Sprite上的觸摸
- 23. 如何在觸摸UIButton時通過UIView檢測觸摸?
- 24. 如何在煎茶觸摸
- 25. 觸摸開始與多點觸摸讓
- 26. 觸摸和觸摸之間的時間
- 27. 可可觸摸:動畫上的觸摸
- 28. 事件觸摸屏輕輕觸摸
- 29. 取消觸摸它時的UIButton觸摸
- 30. 當我觸摸TextView時觸摸按鈕
這是很聰明的,易於理解。 –