2010-07-26 79 views
2

當我在幾個UIButton子類實例後面添加一個UIImageView子類(全屏)時,這些按鈕停止接收觸摸並且不再起作用。我已經嘗試過的任何東西都沒有恢復到用戶觸摸的連接。UIImageView子類沒有通過觸摸到控制器子視圖

我曾嘗試:
a)用UIImageView子類的主視圖(其接收所述觸摸),並確保setUserInteractionEnabled是YES。結果:當UIView存在時,UIButtons未響應。

b)顯式地將前視圖中的接觸傳遞給視圖控制器,希望它能做正確的事情。結果:UIButtons沒有響應觸摸。

c)中直接調用上欲當接收在前UIView任何觸摸交互的按鈕touchesBegantouchesMovedtouchesEnded方法。結果:令人驚訝的是,即使我將觸摸事件壓在喉嚨上,按鈕也不會響應。

d)將觸摸從前視圖傳遞到UIViewController,並遍歷所有子視圖手動觸發觸摸。我包括一個touchesBegan方法,顯示了我如何嘗試這樣做。執行時,循環是無限的,它永遠不會超過第一個視圖(類型爲FrameUIView)。

我知道這個問題很可能是我對正確的視圖層次結構或響應者鏈缺乏理解的結果。

如果我評論按鈕後創建的最後一個視圖,那麼按鈕工作正常。 UIButtons在這方面有什麼特別的嗎?

這是代碼。

我有一個UIViewController子類,在它:

- (void)loadView { 

     mainAppView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
     self.view = mainAppView; 

     frameImageView = [[FrameUIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
     frameImageView.image = [UIImage imageNamed:[[[ThemeController sharedInstance] currentTheme] frameImageFilename]]; 
     [frameImageView setUserInteractionEnabled:YES]; 
     [mainAppView addSubview:frameImageView]; 

     zoomInButton = [[CustomUIButton alloc] initWithFrame:CGRectMake(controlsOriginX + zoomWidth + lightWidth, controlsOriginY, zoomWidth, controlsHeight)]; 
    [zoomInButton setTitle:@"+" forState:UIControlStateNormal]; 
    [[zoomInButton titleLabel] setFont:[UIFont systemFontOfSize:28]]; 
    [zoomInButton addTarget:self action:@selector(doMyAction) forControlEvents:UIControlEventTouchDown]; 
    [zoomInButton addTarget:self action:@selector(cancelMyAction) forControlEvents:UIControlEventTouchUpInside]; 
    [mainAppView addSubview:zoomInButton]; 


     FrameFrontUIView *frameFrontImageView = [[FrameFrontUIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
     frameFrontImageView.image = [UIImage imageNamed:[[[ThemeController sharedInstance] currentTheme] frameFrontImageFilename]]; 
     [frameFrontImageView setUserInteractionEnabled:YES]; 
    [mainAppView addSubview:frameFrontImageView]; 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    for (UIView *view in self.view.subviews) { 
     NSLog(@"view is a %@", [view description]); 
     [view touchesBegan:touches withEvent:event]; 
    } 
} 

任何人對如何做到這一點小費贏得了互聯網和餅乾。也接受俄羅斯輪盤賭混亂的左輪手槍。謝謝。

回答

0

您是否嘗試過使用沒有圖像視圖的按鈕?不建議子類UIButton。實例UIButtons的方法是用工廠方法:

UIButton* myButton = [UIButton buttonWithType:UIButtonTypeCustom]; 

所以我的猜測是,你所創建的CustomUIButton實例未正確設置。

+0

它絕對是一個很好的猜測。我分類UIButton解決問題,即我想能夠設置只有背景圖像的alpha,而不是標籤或按鈕的其餘部分。我通過創建一個子類按鈕來實現這一點,該按鈕在彼此頂部繪製兩個按鈕,頂部具有標籤和動作,底部是圖像按鈕阿爾法集。 我會進一步調查他們,我的下一步是按照你的建議去做,然後嘗試一個股票按鈕。 – Royce 2010-07-27 00:05:48

+0

這聽起來不是一個好方法。試試這個: zoomInButton.imageView.alpha = 0.5; – Felixyz 2010-07-27 10:29:11

+0

會做,謝謝。 暫時我通過setUserInteraction = NO發佈了這個問題;這將關閉事件並觸摸前景中的圖像視圖消耗,從而使應用程序正常工作。 至於如何使互動,但如果不消耗,傳遞這些事件/觸動....以及我無法弄清楚,我試了很多東西。 – Royce 2010-07-28 00:45:24

0

這個問題很舊,但我碰到了同樣的問題。

我使用的解決方案是創建一個容器UIView,然後添加UIImageView作爲子視圖,然後添加按鈕,文本框等。似乎運作良好。

我不明白爲什麼這是使用UIImageView作爲按鈕的超級視圖的問題。