作为一名 iOS 编程新手,我正在努力解决iPhone 上的文字游戏 https://github.com/afarber/ios-newbie/tree/master/ScrollContent.
应用程序结构是:scrollView -> contentView -> imageView -> image 1000 x 1000
(here 全屏 https://i.stack.imgur.com/R8gUv.png):
我想我终于明白了如何使用UIScrollView
在 Xcode 5.1 中启用自动布局:
我只是为contentView
这定义了_scrollView.contentSize
(我不必明确设置它)-之后我的游戏板滚动和缩放就可以了。
但是,我在实现可拖动字母图块时遇到了麻烦Tile.m https://github.com/afarber/ios-newbie/blob/master/ScrollContent/ScrollContent/Tile.m.
I use touchesBegan, touchesMoved, touchesEnded, touchesCancelled
而不是手势识别器(正如 StackOverflow 用户经常建议的那样),因为我显示带有阴影的较大字母图块图像(bigImage
) on touchesBegan
.
我的拖动是通过以下方式实现的:
- In
touchesBegan
我把瓷砖从contentView
(并将其添加到主应用程序视图)并显示bigImage
与阴影。
- In
touchesMoved
我移动瓷砖
- In
touchesEnded
or touchesCancelled
我显示smallImage
再次添加阴影并 - 将图块添加到contentView
或将其保留在主视图中(如果图块位于应用程序的底部)。
我的问题:
大多数情况下这是有效的,但有时(经常)我看到只有touchesBegan
被称为,但另一个touchesXXXX
方法永远不会被调用:
2014-03-22 20:20:20.244 ScrollContent[8075:60b] -[Tile touchesBegan:withEvent:]: Tile J 10 {367.15002, 350.98877} {57.599998, 57.599998}
相反,scrollView
用手指在大图块下方滚动。
这会导致我的应用程序屏幕上出现许多带有阴影的大图块,而滚动视图则被拖动到它们下面:
请问如何解决这个问题?
我确信我的应用程序结构(带有自定义UIView
s 被拖入/拖出UIScrollView
)是可能的——通过查看流行的文字游戏。
I use tile.exclusiveTouch = YES
and contentView 的自定义 hitTest 方法 https://github.com/afarber/ios-newbie/blob/master/ScrollContent/ScrollContent/GameBoard.m- 但这没有帮助:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView* result = [super hitTest:point withEvent:event];
return result == self ? nil : result;
}
更新1:
我尝试添加以下代码handleTileTouched
:
_contentView.userInteractionEnabled = NO;
_scrollView.userInteractionEnabled = NO;
_scrollView.scrollEnabled = NO;
然后将其设置回YES
in handleTileReleased
of 视图控制器.m https://github.com/afarber/ios-newbie/blob/master/ScrollContent/ScrollContent/ViewController.m- 但这没有帮助,而且对我来说看起来更像是一个黑客。
更新2:
可能已经阅读了与以下内容相关的所有内容UIScrollView
, hitTest:withEvent:
and pointInside:withEvent:
- 在网络上(例如黑客响应链 http://bynomial.com/blog/?p=74和马特·纽伯格的iOS 编程 http://www.apeth.com/iOSBook/ch18.html#_hit_testing书),StackOverflow 和 Safari,在我看来,解决方案是实现hitTest:withEvent:
我的应用程序主视图的方法:
If a Tile
物体被击中,它应该被返回。否则 -scrollView
应该被退回。
不幸的是,这不起作用——我可能遗漏了一些小东西。
我确信存在一个好的解决方案 - 通过研究 iOS 上流行的文字游戏。例如,在 Zynga 中,字母图块的拖动和放置工作非常顺利与朋友的话® http://www.wordswithfriends.com应用程序,在下面的屏幕截图中您可以看到他们可能使用UIScrollView
(滚动条在角落可见)并显示平铺阴影(可能在touchesBegan
方法):
更新3:
我创建了一个新项目来测试手势识别器 https://github.com/afarber/ios-newbie/tree/master/Recognizer由 TomSwift 建议,它显示了我在手势识别器方面遇到的问题:图块大小更改得太晚 - 当用户开始移动图块时而不是在他触摸它的那一刻,就会发生这种情况: