简体   繁体   中英

Scrolling UIWindow along with page scrolling in Xamarin.iOS

I am adding my custom view to UIWindow for act like a popup(AlertView). When clicking a button on my screen I would like to show my custom view. My requirement is i want to move my custom view along with page scroll. As of now its displayed statically and reminds in the same place while scrolling the page.

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.
    }

Suggest me how to achieve my requirement?

As of now its displayed statically and reminds in the same place while scrolling the page.

Since you add the pop view to the Windows, however, the Windows is stationary !

You should add it to scrollView instead of Window .

Change your code as below

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

Test

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM