我在 Xamarin Studio (MonoTouch) 中开发,但概念是一样的。
我有一个 UIViewController,在那个 UIViewController 中我添加了几个子视图,每个子视图都需要响应点击事件。我将 UITapGestureRecognizer 附加到我的子视图,但它们不会触发。查看“AddExploreButton()”和“AddFollowingButton()”。
如果我将手势添加到包含视图(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);
}
}