[英]Scrolling UIWindow along with page scrolling in 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.
}
建议我如何达到我的要求?
截至目前,其静态显示并在滚动页面时在同一位置提醒。
但是,由于将弹出视图添加到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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.