iOS 触摸阻塞问题

标签 ios objective-c iphone touch

我正在处理一个与触摸有关的 iOS 应用程序,遇到一个奇怪的问题。

例如,应用程序的布局看起来像这样(非常基本,没有什么花哨的,只有一些按钮和一个触摸区域):

Application layout

A 是触摸区域(它是一个容器,里面有一个 View ,显示图像的 View ,图像受我触摸该区域的方式的影响)。

B 是一个简单的 iOS 按钮(您默认使用 XCODE 创建的按钮,只是更改了背景颜色和大小(宽度/高度)。

现在,问题是:它只发生过几次,我还不知道为什么以及什么时候发生,但有时我可以重现它。基本上,当单击 B 时,触摸 A(用左手的手指)表现得很奇怪:我正在接收 touchesMoved 事件,但触摸位置是触摸 B 的位置,因此 A 中图像的移动确实不会发生,当它发生时(从 B 移开右手手指后),移动的行为就好像触摸是在 B 位置。

这是附加到 A View 的 ViewController 中的部分代码,即处理触摸的部分。我删除了一些我认为与我的问题无关的代码,以使内容更清晰。

-(void)touchesBegan:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{

    NSLog(@"A View touched : %d", [touches count]);
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView: _theAView];
    NSLog(@"Touch at %f %f", touchLocation.x, touchLocation.y);
    initialTouchX = touchLocation.x;
    initialTouchY = touchLocation.y;    


}

-(void)touchesMoved:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{

    /*
    some code was here handling time, to only handle moving every X milliseconds
    */
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:theAView];


    /*
    code handling the display of the image , removed
    */
    initialTouchX = touchLocation.x;
    initialTouchY = touchLocation.y;

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {

}

至于B键的代码,就是一个基本的IBAction,调用一个带参数的函数:

- (IBAction)btnDown:(id)sender {
    //call some function here 
}

需要注意两点:

1) 上述代码中,“theAView”指的是A区的 View 。 2)代码的第一部分(有触摸的部分)位于不同的 ViewController 中;代码的第二部分,即按钮,位于 XCODE 在创建项目时创建的默认 ViewController 文件中。

我也尝试在 A View 和 B 按钮上启用多点触控,但问题仍然存在。

感谢任何帮助。

提前致谢。

编辑:我几乎每次都可以重现该行为的一种情况是,当我几乎同时触摸 A 和 B 时,先是 B,然后是 A。将此添加到问题中的想法可能会有所帮助。

最佳答案

i am receiving the touchesMoved event, but the touch position is the position of the touch on B

问题在于,在 View A 的触摸处理中,您正在检查错误的触摸。这行代码每次出现都是错误的:

UITouch *touch = [[event allTouches] anyObject];

[[event allTouches] anyObject] 包括对所有 View 的所有 触摸。因此,触摸位置是 View B 上的触摸位置,因为这是 View B 上的触摸。

相反,检查[touches anyObject];这些是针对 View ( View A)的触摸。

关于iOS 触摸阻塞问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31105823/

相关文章:

iphone - 将常量语言设置为 CLPlacemark 实例类

ios - 如何将数据从先前的 segue 传递给 subview Controller ?

ios - 更改 Storyboard 中已存在的 UIView 的宽度? swift

ios - iOS 中的事件指示器应该阻止用户访问其他 UI 元素,直到它停止动画

objective-c - 单击状态项应该开始操作,但没有

iphone - 导航堆栈中缺少 View Controller

ios - 如何使用 RCT_EXPORT_MODULE React Native

iOS 调整图像大小的更简单方法

objective-c - 在 xcode 中创建和使用汇编程序库

iphone - 播放从保存的 persistentid 列表中检索到的 iPod 播放列表