添加子视图(如果不存在)

时间:2015-02-18 08:21:29

标签: ios ios7 xamarin xamarin.forms

单击项目时我将子视图(UIButton)添加到我的晚餐视图中,并希望在用户再次单击时删除旧视图或仅更改标题,而不是第一次单击。

这是我的自定义渲染器:

    var text = new UIButton ();
 protected override void OnElementChanged (ElementChangedEventArgs<Image> e)
            {
                base.OnElementChanged (e);

                if (Control == null)
                    return;
                if (this.Element != null) {
                    Original = (SpecLogoImage)this.Element;
                }
                TargetView = Control as UIImageView;        



                text.Layer.CornerRadius = 5;
                text.Font = UIFont.FromName ("DIN Condensed", text.Font.PointSize);


                text.TouchUpInside += delegate {
                    text.RemoveFromSuperview ();
                };


                var rec = new UILongPressGestureRecognizer (() => {     
                    text.SetTitle(Original._hint, UIControlState.Normal);
                    text.TitleLabel.LineBreakMode = UILineBreakMode.WordWrap; 
                    text.SizeToFit ();


                    text.Frame = new CGRect ((UIApplication.SharedApplication.KeyWindow.Frame.Width / 2.0f) - (250 / 2.0f),
                        this.Superview.Superview.Superview.Frame.Height / 2.0f, 250, text.Frame.Height);

                    if (text.Superview==null) {

                        Animate (0.6f, () => {
                            this.Superview.Superview.Superview.AddSubview (text);
                        }, () => {
                            text.Layer.ZPosition = int.MaxValue;
                        }); 
                    }

                });

                text.SetTitleColor (UIColor.White, UIControlState.Normal);
                text.BackgroundColor = UIColor.Black;

                TargetView.UserInteractionEnabled = true;
                TargetView.AddGestureRecognizer (rec);
            }


        }

每次用户点击Superview为空时,有什么问题?

0 个答案:

没有答案