我有一個UIImage
視圖響應觸摸事件。我想要取消觸摸順序,即,如果觸摸超出特定範圍,將進一步撥打touchesMoved:
。我怎樣才能做到這一點?如何取消一系列UITouch事件?
我知道在touchesMoved:
我可以檢查觸摸對象的座標並忽略它,但我不知道如何完全取消該序列。我沒有看到Apple Developer UIResponder
Reference中記錄的任何方法可以調用以取消觸摸順序。
我有一個UIImage
視圖響應觸摸事件。我想要取消觸摸順序,即,如果觸摸超出特定範圍,將進一步撥打touchesMoved:
。我怎樣才能做到這一點?如何取消一系列UITouch事件?
我知道在touchesMoved:
我可以檢查觸摸對象的座標並忽略它,但我不知道如何完全取消該序列。我沒有看到Apple Developer UIResponder
Reference中記錄的任何方法可以調用以取消觸摸順序。
這種解決方案可能有點缺憾,但你可以實現和手動調用
- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
我在哪裏我修改了對一些調整,我做了對蘋果的iPhone sample code網站了MoveMe示例應用程序鬆散立足這一解決方案touchesMoved
方法是這樣的:
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == placardView)
CGPoint location = [touch locationInView:self];
placardView.center = location;
// I added the following line:
[self touchesCancelled:touches withEvent:event];
return;
}
我最近遇到了同樣的問題,並發現了一個標準的方式來解決這個問題。 您可以使用[[UIApplication sharedApplication] beginIgnoringInteractionEvents]停止將touchesMoved事件傳遞到整個應用程序。當您需要再次接觸觸摸時,請確保使用[[UIApplication sharedApplication] endIgnoringInteractionEvents]啓用它們。
你需要調用[super touchesMoved:withEvent:]
爲了讓超級查看從事件,但更重要的是清理,你需要不通話[super touchesCancelled:withEvent:]
。
這是我在小區中使用,以保持它得到選擇時,我發現刷卡:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if (!showingEdit) {
if (IS_FAR_ENOUGH_TO_BE_A_SWIPE) {
RESPOND_TO_SWIPE
showingEdit = YES;
[super touchesCancelled:touches withEvent:event];
} else {
[super touchesMoved:touches withEvent:event];
}
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (!showingEdit) {
[super touchesEnded:touches withEvent:event];
}
}
這不適合我。 – ma11hew28 2011-11-17 19:17:40
我不認爲這是可能的,因爲我沒有看到它記錄和這些解決方案工作。
您可以檢查,觸摸點位置在的CGRect(即點都在矩形的ImageView的),或者沒有。如果他們不在該Rect中,觸摸將被取消。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if (CGRectContainsPoint(myImageView, touchLocation))
{
lastPoint = [touch locationInView: myImageView];
}
NSLog(@"Last :%.2f - %.2f",lastPoint.x, lastPoint.y);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint currentPoint;
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if (CGRectContainsPoint(myImageView.frame, touchLocation))
{
currentPoint = [touch locationInView: myImageView];
}
}
該代碼不會取消對「touchesMoved:」的進一步調用。它只是忽略它們。 – ma11hew28 2011-11-25 16:27:14
在iOS5中似乎有一個私有方法的UITouch
-(void)setSentTouchesEnded:(BOOL)ended;
根據蘋果的實現也可能停止發送事件
這樣做的另一種方法是使用關聯對象
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([(NSNumber *)objc_getAssociatedObject(touch, &outKey) boolValue])
return;
if (sometouchisoutsideofview) {
objc_setAssociatedObject(touch, &outKey, [NSNumber numberWithBool:YES], OBJC_ASSOCIATION_RETAIN);
}
}
嘗試臨時設置UIImageView的userInteraction啓用屬性爲NO
我只是想解決這樣的問題,並發現沒有列出的解決方案她爲我工作。我能做的最好的事情是暫時忽略了觸摸,但是當觸摸重新進入視圖時,他們又恢復了。
終於解決了。
我想這會爲你工作,除非你有一個特定的原因需要按字面順序取消。
「蠻力」解決方案取決於它是否適合您的需求:刪除並重新初始化接收觸摸或響應它的子視圖(當然要照顧框架和內容)。
您可以爲UITouch創建類別。你可以申報
@property (nonatomic,strong,readwrite) UIView *view;
當您更改touch.view爲零,例如,您可以模擬調度觸摸事件
此代碼將幫助你結束。我在touchesMoved:
方法禁用觸摸事件,我通過從它的父視圖並將其添加直背使觸摸事件touchesCancelled:
方法
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
.......
.......
Your code
.......
.......
//Condition when you want to disable touch event
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
.......
.......
Your code
.......
.......
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}
我實現這一目標。
[view retain];
UIView *sv = view.superview;
[view removeFromSuperview];
[sv addSubview:view];
[view release];
這將響應者鏈分解爲視圖,以便在視圖上不會接收任何剩餘的觸摸。下一次觸摸仍然會正常接收。
謝謝,當我回到我的Mac時,我會試試這個。但是這是否會取消隨後的觸動事件?在我看來,經過手動調用touchCancelled後,touchesMoved仍然會被系統調用。 – 2009-02-12 04:44:32
在MoveMe示例代碼的範圍內,它將取消觸摸,但我相信你是對的,touchesMoved仍然會被調用。我越想它,我認爲觸及得越少就是正確的召喚。 – 2009-02-12 21:55:51
這不起作用,「移動」事件不斷髮生。 – Andy 2018-01-31 14:04:27