在Xamarin.iOS中滚动UIWindow以及页面滚动

时间:2017-11-23 06:38:43

标签: xamarin xamarin.ios

我将自定义视图添加到UIWindow,就像弹出窗口(AlertView)一样。单击屏幕上的按钮时,我想显示自定义视图。我的要求是我想移动自定义视图和页面滚动。截至目前,它在静态显示并在滚动页面时在同一位置提醒。

public class CustomPopUpView : UIView
{
    public delegate void PopWillCloseHandler();
    public event PopWillCloseHandler PopWillClose;

    UIView effectView = new UIView();
    private UIButton btnClose = new UIButton(UIButtonType.System);
    public CustomPopUpView(CGSize size)
    {
        this.Frame = new CGRect(new CGPoint(20, 170), size);
        effectView.Alpha = 0;
        this.BackgroundColor = UIColor.LightGray;
        nfloat btnHeight = 40;
        btnClose.SetTitle("Close", UIControlState.Normal);
        btnClose.Frame = new CGRect(0, this.Frame.Height - btnHeight, this.Frame.Width, btnHeight);
        btnClose.TouchUpInside += delegate
        {
            Close();
        };
        this.AddSubview(btnClose);
    }



    public void PopUp(bool animated = true, Action popAnimationFinish = null)
    {
        UIWindow window = UIApplication.SharedApplication.KeyWindow;
        effectView.Frame = window.Bounds;
        window.EndEditing(true);
        window.AddSubview(this);

        if (animated)
        {
            UIView.Animate(0.15, delegate
            {
                effectView.Alpha = 0.8f;
            }, delegate
            {
                if (null != popAnimationFinish)
                    popAnimationFinish();
            });
        }
        else
        {
            effectView.Alpha = 0.8f;
        }
    }

    public void Close(bool animated = true)
    {
        if (animated)
        {
            UIView.Animate(0.15, delegate
            {
                effectView.Alpha = 0;
            }, delegate
            {
                this.RemoveFromSuperview();
                effectView.RemoveFromSuperview();
                if (null != PopWillClose) PopWillClose();
            });

            this.RemoveFromSuperview();
        }
        else
        {
            if (null != PopWillClose) PopWillClose();
        }
    }
}

public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        UIScrollView scrollView = new UIScrollView();
        scrollView.Frame = new CGRect(0, 0, UIScreen.MainScreen.Bounds.Width, 600);
        scrollView.ContentSize = new CGSize(UIScreen.MainScreen.Bounds.Width, 700);
        UIButton button = new UIButton();
        button.SetTitle("Click", UIControlState.Normal);
        button.BackgroundColor = UIColor.Red;
        button.Frame = new CGRect(20, 100, 200, 40);
        CustomPopUpView popup = new CustomPopUpView(new CoreGraphics.CGSize(200, 300));
        button.TouchDown += (sender, e) => {
            popup.PopUp(true, delegate {
               Console.WriteLine("Custom popup will open");
           });
        };
        scrollView.AddSubview(button);
        this.View.AddSubview(scrollView);
        // Perform any additional setup after loading the view, typically from a nib.
    }

建议我如何达到我的要求?

1 个答案:

答案 0 :(得分:0)

  

截至目前,它在静态显示并在滚动页面时在同一位置提醒。

但是,由于您将弹出视图添加到Windows,因此Windows处于静止状态!

您应该将其添加到scrollView而不是Window

更改您的代码如下

public class CustomPopUpView : UIView
{
    public delegate void PopWillCloseHandler();
    public event PopWillCloseHandler PopWillClose;

    UIView effectView = new UIView();
    private UIButton btnClose = new UIButton(UIButtonType.System);

    UIView superView;
    public CustomPopUpView(CGRect rect , UIView _superView)
    {
        this.Frame = rect;
        superView = _superView;
        effectView.Alpha = 0;
        this.BackgroundColor = UIColor.LightGray;
        nfloat btnHeight = 40;
        btnClose.SetTitle("Close", UIControlState.Normal);
        btnClose.Frame = new CGRect(0, this.Frame.Height - btnHeight, this.Frame.Width, btnHeight);
        btnClose.TouchUpInside += delegate
        {
            Close();
        };
        this.AddSubview(btnClose);
    }



    public void PopUp(bool animated = true, Action popAnimationFinish = null)
    {
        superView.AddSubview(this);

        if (animated)
        {
            UIView.Animate(0.15, delegate
            {
                effectView.Alpha = 0.8f;
            }, delegate
            {
                if (null != popAnimationFinish)
                    popAnimationFinish();
            });
        }
        else
        {
            effectView.Alpha = 0.8f;
        }
    }

    public void Close(bool animated = true)
    {
        if (animated)
        {
            UIView.Animate(0.15, delegate
            {
                effectView.Alpha = 0;
            }, delegate
            {
                this.RemoveFromSuperview();
                effectView.RemoveFromSuperview();
                if (null != PopWillClose) PopWillClose();
            });

            this.RemoveFromSuperview();
        }
        else
        {
            if (null != PopWillClose) PopWillClose();
        }
    }
}

public partial class ViewController1 : UIViewController
{
    public ViewController1() : base("ViewController1", null)
    {
    }

    public override void ViewDidLoad()
    {

        base.ViewDidLoad();
        UIScrollView scrollView = new UIScrollView();
        scrollView.Frame = new CGRect(0, 0, UIScreen.MainScreen.Bounds.Width, 600);
        scrollView.ContentSize = new CGSize(UIScreen.MainScreen.Bounds.Width, 700);
        UIButton button = new UIButton();
        button.SetTitle("Click", UIControlState.Normal);
        button.BackgroundColor = UIColor.Red;
        button.Frame = new CGRect(20, 100, 200, 40);
        CustomPopUpView popup = new CustomPopUpView(new CGRect(20,170,200,300),scrollView);
        button.TouchDown += (sender, e) => {
            popup.PopUp(true, delegate {
                Console.WriteLine("Custom popup will open");
            });
        };
        scrollView.AddSubview(button);
        this.View.AddSubview(scrollView);
    }  
}

测试

enter image description here