2012-01-05 90 views
0

我正在处理网格布局中有很多UIview的视图控制器。这些视图有一些交互(如按钮等)。但是在触及该视图时,我想要出现一个新的视图控制器。 我找不到从这些视图中检查触摸的方法。有一个方法CGRectContainsPoint ...但我有多个UIViews。从UIViews数组中检测触摸

请帮我一把!

回答

1

你试过了吗?

-(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获取坐标,然后将它们与原始视图的子视图进行比较。

我认为,其他可能solutoions可能是:

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

使用这两种方法可以让你的(子)的意见显示你的ViewController时您的子视图会检测触摸并让视图控制器相应地执行操作。

+0

我的视图包含两个按钮,所以点击这些按钮将touchesBegan被调用或touchUpInside?此外,网格包含12个视图,因此我必须在touchesBegan方法中使用循环...但是,每次检测到触摸时运行该循环的有效方法是什么? – SamG 2012-01-05 13:57:23

+0

有两件事情可能发生,当你的按钮被触摸时,与之相连的选​​择器将被调用。否则,如果视图被触摸,touchesbegan将被调用。我同意这可能不是解决这个问题的最有效方式。我将编辑我的答案以描述这样做的两种不同方式。 – 2012-01-05 14:10:03

0

解决这一问题将是手势识别器添加到您的意见拿起水龙头的另一种方式:

UITapGestureRecognizer *tapGestureRecogniser = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_method_:)]; 
    [_view_ addGestureRecognizer:tapGestureRecogniser]; 
    [tapGestureRecogniser release]; 

这就需要方法每当视图被窃听。 希望这有助于。

+0

我试过这个。但问题仍然存在,这些观点有按钮被挖掘。在添加点击获取以完成视图时,与这些按钮耦合的动作不会被调用,而是会崩溃。 – SamG 2012-01-06 07:39:52