我正在开发一个在网格布局中有许多UI视图的视图控制器。这些视图有一些交互(如按钮等)。但在触摸该视图时,我希望出现一个新的视图控制器。我找不到从这些观点中寻找灵感的方法。有一种方法CGRectContainsPoint。。。但是我有多个UIViews。
请帮帮我!
你试过吗?
-(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event {
CGPoint point = [[touches anyObject] locationInView:self.view];
if ( CGRectContainsPoint(subview1.frame, point))
{
//show viewcontroller1
}
if ( CGRectContainsPoint(subview2.frame, point))
{
//show viewcontroller2
}
}
通过这种方式,您应该能够确定哪个子视图被触摸。
这个想法是,首先你可以根据self.view获得坐标,然后你可以将它们与原始视图的子视图进行比较。
我认为其他可能的解决方案可能是:
1使用keyValueObservinghttp://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html
或
2 NSNotificationCenter消息http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html
使用这两种方法,当子视图检测到触摸时,您可以让(子)视图向视图控制器发出信号,并让视图控制器相应地动作。
解决这个问题的另一种方法是在视图中添加手势识别器来获取点击:
UITapGestureRecognizer *tapGestureRecogniser = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_method_:)];
[_view_ addGestureRecognizer:tapGestureRecogniser];
[tapGestureRecogniser release];
每当点击视图时,这将调用方法。希望这能有所帮助。