2012-06-07 61 views
0

所以我有一個用IB製作的UIView。我將該類設置爲我的自定義UIView類,以便我可以從該視圖獲得觸動。但出於某種原因,我屏幕上的UIView不是我的自定義類。我試圖鑄造它:Subclassed UIView,但沒有觸及

myView = (CustomUIView *)myView; 

我也嘗試將其設置等於CustomUIView:

myView = [[CustomUIView alloc] init]; 

(這導致當我對象添加到屏幕中的錯誤(我猜這是因爲我已經覆蓋了IBOutlet中或東西。

所以,我怎麼能得到觸摸(觸摸開始,觸摸取消等),從這個觀點?

回答

0

洛爾方式愚蠢,我有另一個UIView編程添加編輯到填充customUIView的customUIView,以防止觸及任何接觸。

0

看看加入UIGestureRecognizersŧ你的看法。

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIGestureRecognizer_Class/Reference/Reference.html

我認爲這是你最好的選擇。

+0

但我不需要手勢識別器,我需要觸摸。 – ManOx

+0

GestureRecognizer爲您提供觸摸。 UITapGestureRecognizer會告訴你何時點擊視圖。 UIPanGestureRecognizer告訴你何時點擊視圖,觸摸移動,觸摸結束。手勢識別器是Apple提供的更新的首選解決方案(touchesBegan,Moved,並且仍然可以工作,但已經過時)。他們也比較容易理解,這就是我提出這個建議的原因。 – WendiKidd

0

確保電源插座也符合新CustomUIView類在你的頭:

#import "CustomUIView.h" 

..... 

IBOutlet CustomUIView *myView; 
+0

我的插座符合CustomUIView類,但它仍然不會觸及。 – ManOx

+0

您的'CustomUIView'類是否實現了觸摸方法,'-touchesBegan:','-touchesEnded:','-touchesCancelled:'。如果沒有,那就是你的問題。你需要實現這些方法才能真正接收'touch'事件。顯示你的'CustomUIView''.m'文件的外觀。 – skram

+0

我確實在我的customUIView.m中實現了那些 – ManOx

0

你可以做到這一點編程,

CustomUIView *custom = [[CustomUIView alloc] initWithFrame:self.view.frame]; 
self.view = custom; 
[custom release]; 

和執行關於CustomUIView

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

} 

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

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

} 
的UIResponder
+0

所以我嘗試了這一點,並用customUIView(我知道它被替換,因爲init方法被調用)替換屏幕上的UIView,所以它的工作原理,但觸摸仍然沒有得到通過。 – ManOx

+0

可以發佈代碼在哪裏初始化自定義視圖?以及自定義視圖的實現 – dianz