从 UIView 数组中检测触摸
我正在开发一个视图控制器,它在网格布局中有许多 UIview。这些视图有一些交互(如按钮等)。但是在触摸该视图时,我希望出现一个新的视图控制器。 我无法找到一种方法来检查这些视图中的触摸。有一个方法 CGRectContainsPoint...但我有多个 UIView。
请帮帮我!
I am working on a view controllers that has many UIview in a grid layout. Those views has some interaction (like buttons etc). But on touching that view, I want a new view controller to appear.
I cannot find a way to check for a touch from these views. There's a method CGRectContainsPoint... but I have multiple UIViews.
Please help me out!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你尝试过吗?
这样您应该能够确定哪个子视图被触摸。
这个想法是,首先根据 self.view 获取坐标,然后可以将它们与原始视图的子视图进行比较。
我认为其他可能的解决方案可能是:
1 使用 keyValueObserving http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html
或
2 NSNotificatonCenter 消息 http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html
使用这两种方法,您可以让您的 (当子视图检测到触摸时,子视图会向视图控制器发出信号,并让视图控制器做出相应的操作。
Have you tried?
This way you should be able to determine which subview is touched.
The idea is that first you get your coordinates based on the self.view and then you can compare them with the subviews of the original view.
I think other possible solutoions could be:
1 Use keyValueObserving http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html
or
2 NSNotificatonCenter messages http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html
With both methods you can let your (sub)views signal your viewcontroller when your subview detect a touch and let the viewcontroller act accordingly.
解决此问题的另一种方法是将手势识别器添加到您的视图中以拾取点击:
每当点击视图时,都会调用方法。
希望这有帮助。
Another way of tackling this problem would be to add gesture recognizers to your views to pick up taps:
This would call method whenever the view is tapped.
Hope this helps.