當我在幾個UIButton
子類實例後面添加一個UIImageView
子類(全屏)時,這些按鈕停止接收觸摸並且不再起作用。我已經嘗試過的任何東西都沒有恢復到用戶觸摸的連接。UIImageView子類沒有通過觸摸到控制器子視圖
我曾嘗試:
a)用UIImageView
子類的主視圖(其接收所述觸摸),並確保setUserInteractionEnabled
是YES。結果:當UIView
存在時,UIButtons
未響應。
b)顯式地將前視圖中的接觸傳遞給視圖控制器,希望它能做正確的事情。結果:UIButtons
沒有響應觸摸。
c)中直接調用上欲當接收在前UIView
任何觸摸交互的按鈕touchesBegan
,touchesMoved
和touchesEnded
方法。結果:令人驚訝的是,即使我將觸摸事件壓在喉嚨上,按鈕也不會響應。
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];
}
}
任何人對如何做到這一點小費贏得了互聯網和餅乾。也接受俄羅斯輪盤賭混亂的左輪手槍。謝謝。
它絕對是一個很好的猜測。我分類UIButton解決問題,即我想能夠設置只有背景圖像的alpha,而不是標籤或按鈕的其餘部分。我通過創建一個子類按鈕來實現這一點,該按鈕在彼此頂部繪製兩個按鈕,頂部具有標籤和動作,底部是圖像按鈕阿爾法集。 我會進一步調查他們,我的下一步是按照你的建議去做,然後嘗試一個股票按鈕。 – Royce 2010-07-27 00:05:48
這聽起來不是一個好方法。試試這個: zoomInButton.imageView.alpha = 0.5; – Felixyz 2010-07-27 10:29:11
會做,謝謝。 暫時我通過setUserInteraction = NO發佈了這個問題;這將關閉事件並觸摸前景中的圖像視圖消耗,從而使應用程序正常工作。 至於如何使互動,但如果不消耗,傳遞這些事件/觸動....以及我無法弄清楚,我試了很多東西。 – Royce 2010-07-28 00:45:24