【问题标题】:What causes Subviews in iOS to not capture touches?是什么导致 iOS 中的子视图无法捕获触摸?
【发布时间】:2013-06-25 11:39:24
【问题描述】:

我在 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);
    }
}

【问题讨论】:

  • View.UserInteractionEnabled = true; 帮助我从第二个子视图中捕捉到我的触摸事件。谢谢! )

标签: ios xamarin.ios xamarin


【解决方案1】:

我发现了问题。我使用 UIView.SizeToFit() 来生成宽度和高度。

据此:Having trouble getting UIView sizeToFit to do anything meaningful 然而,UIView.SizeToFit() 没有做任何事情。

如果没有适当的帧大小,尽管可见,但子视图无法捕获触摸事件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多