我在Xamarin Studio(MonoTouch)中进行开发,但概念是一样的。
我有一个UIViewController,在这个UIViewController中,我添加了几个子视图,每个子视图都需要响应tap事件。我将UIMapGestureRecognizer附加到我的子视图,但它们不会激发。查看"AddExploreButton()"one_answers"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);
}
}
我发现了问题。我使用UIView.SizeToFit()生成宽度和高度。
根据这一点:在让UIView sizeToFit做任何有意义的事情时遇到了困难,然而,UIView.sizeToFit()什么都不做。
如果没有适当的帧大小,尽管子视图是可见的,但它无法捕捉触摸事件。