我在Xamarin Studio(MonoTouch)中開發,但概念是相同的。是什麼導致iOS中的Subviews無法捕捉觸摸?
我有一個UIViewController,並在該UIViewController中添加了幾個子視圖,每個子視圖都需要響應點擊事件。我將UITapGestureRecognizer附加到我的子視圖中,但它們不會觸發。看看「AddExploreButton()」和「AddFollowingButton()」。
如果我將手勢添加到包含View(MyMenu.View),它確實捕獲了輕敲手勢,但我需要子視圖來處理事件。
是什麼導致我的子視圖不能接收到手勢? (注意,我幾乎在所有方面都設置了UserInteractionEnabled = true)。其次,如果這是不正確的編碼模式,我會如何將子視圖連接到觸摸事件?
public class MyMenu : UIViewController
{
private UIImageView _settingsButton;
private TrendingMenuButton _trendingButton;
private FollowingMenuButton _followingButton;
private ExploreMenuButton _exploreButton;
private NotificationsMenuButton _notificationsButton;
public MyMenu() { }
public override void ViewDidLoad() {
base.ViewDidLoad();
View.Hidden = true;
View.UserInteractionEnabled = true;
View.BackgroundColor = UIColor.FromPatternImage(UIImage.FromBundle("images/menu_bg.png"));
View.Frame = new RectangleF(0, CityTitleBar.NAV_HEIGHT, 320, 349);
AddSettingsButton();
AddTrendingButton();
AddFollowingButton();
AddExploreButton();
AddNotificationsButton();
}
private void AddSettingsButton() {
_settingsButton = new UIImageView(UIImage.FromBundle("images/icon_cogwheel.png"));
_settingsButton.Frame = new RectangleF(new PointF(269, 12), _settingsButton.Frame.Size);
View.AddSubview(_settingsButton);
}
private void AddTrendingButton() {
_trendingButton = new TrendingMenuButton(42, 33);
View.AddSubview(_trendingButton);
}
private void AddFollowingButton() {
_followingButton = new FollowingMenuButton(182, 33);
_followingButton.AddGestureRecognizer(new UITapGestureRecognizer((g) => {
MenuHelper.HideMenu();
NavigationHelper.LoadScreen(new FavoritesScreen());
}));
View.AddSubview(_followingButton);
}
private void AddExploreButton() {
_exploreButton = new ExploreMenuButton(42, 187);
_exploreButton.AddGestureRecognizer(new UITapGestureRecognizer((g) => {
MenuHelper.HideMenu();
NavigationHelper.LoadScreen(new ExploreScreen());
}));
View.AddSubview(_exploreButton);
}
private void AddNotificationsButton() {
_notificationsButton = new NotificationsMenuButton(182, 187);
View.AddSubview(_notificationsButton);
}
}
* View.UserInteractionEnabled = true; *幫助我捕捉到第二個子視圖的觸摸事件。謝謝! ) –