using System; using System.Collections.Generic; using Shared.IO; using UIKit; namespace Shared { /// <summary> /// ViewGroup 按键 /// </summary> public class ViewGroup : View { /// <summary> /// 当å‰çœŸå®žçš„æŽ§ä»¶ /// </summary> internal UIView realViewGroup; /// <summary> /// 当剿ާ件 /// </summary> /// <value>The view group.</value> internal UIView viewGroup { get { return uiView as UIView; } set { uiView = value; uiView.AddSubview(backgroundImage); } } /// <summary> /// 当å‰èƒŒæ™¯å›¾ /// </summary> UIKit.UIImageView backgroundImage = new UIImageView() { Tag = int.MinValue}; /// <summary> /// 控件宽度 /// </summary> public override int Width { get { return base.Width; } set { base.Width = value; if (!IsCanRefresh) return; var frame = backgroundImage.Frame; frame.Width = base.Width; backgroundImage.Frame = frame; var frame2 = realViewGroup.Frame; frame2.Width = base.Width; realViewGroup.Frame = frame2; } } /// <summary> /// 控件的高度 /// </summary> /// <value>The height.</value> public override int Height { get { return base.Height; } set { base.Height = value; if (!IsCanRefresh) return; var frame = backgroundImage.Frame; frame.Height = base.Height; backgroundImage.Frame = frame; var frame2 = realViewGroup.Frame; frame2.Height = base.Height; realViewGroup.Frame = frame2; } } /// <summary> /// å¢žåŠ åæŽ§ä»¶ /// </summary> /// <param name="view">View.</param> public virtual void AddChidren(View view) { view.Parent = this; viewList.Add(view); realViewGroup.AddSubview(view.RealView); if (!IsCanRefresh) { return; } view.Refresh(); addParentToAllSubViews(view); if (view is ViewGroup) { var tempViewGroup = (ViewGroup)view; for (int i = 0; i < tempViewGroup.ChildrenCount; i++) { tempViewGroup.GetChildren(i).Refresh(); } } } internal void addParentToAllSubViews(View view) { if (view is ViewGroup) { var viewgroup = view as ViewGroup; for (int i = 0; i < viewgroup.viewList.Count; i++) { var _view = viewgroup.viewList[i]; if (_view == null) continue; _view.Parent = viewgroup; addParentToAllSubViews(_view);//é€’å½’ï¼Œå°†åæŽ§ä»¶æ‰€åŒ…å«çš„æŽ§ä»¶ä¹Ÿä¸€å¹¶ç§»é™¤ï¼› } } } /// <summary> /// æŽ§ä»¶æ•°é‡ /// </summary> /// <value>The children count.</value> public virtual int ChildrenCount { get { return viewList.Count; } } /// <summary> /// 控件列表 /// </summary> internal System.Collections.Generic.List<View> viewList = new System.Collections.Generic.List<View>(); string backgroundImagePath; /// <summary> /// 背景图片 /// </summary> /// <value>The background image path.</value> public virtual string BackgroundImagePath { get { return backgroundImagePath; } set { backgroundImagePath = value; if (!IsCanRefresh || Radius != 0) { return; } backgroundImage.Image = UIImage.FromFile(FileUtils.GetImageFilePath(backgroundImagePath)); } } /// <summary> /// åˆ·æ–°ç•Œé¢ /// </summary> public override void Refresh() { base.Refresh(); BackgroundImagePath = backgroundImagePath; } /// <summary> /// 移除控件 /// </summary> /// <param name="view">View.</param> internal virtual void Remove(View view) { if (view == null) { return; } viewList.Remove(view); if (view.RealView.Superview != null) { view.RealView.RemoveFromSuperview(); } view.Parent = null; } internal virtual void removeChildParent() { for (int i = 0; i < viewList.Count; i++) { var view = viewList[i]; if (view == null) continue; if (view is ViewGroup) { (view as ViewGroup).removeChildParent(); } view.Parent = null; } } public override void RemoveFromParent() { removeChildParent(); base.RemoveFromParent(); } /// <summary> /// 清空所有的控件 /// </summary> public virtual void RemoveAll() { while (0 < viewList.Count) { GetChildren(0)?.RemoveFromParent(); } } /// <summary> /// 移除指定索引对象 /// </summary> /// <param name="index">Index.</param> public virtual void RemoveAt(int index) { //Remove(GetChildren(index)); GetChildren(index)?.RemoveFromParent(); } /// <summary> /// èŽ·å–æŒ‡å®šç´¢å¼•对象 /// </summary> /// <returns>The children.</returns> /// <param name="index">Index.</param> public View GetChildren(int index) { if (index < 0 || viewList.Count - 1 < index) { return null; } return viewList[index]; } /// <summary> /// æ ¹æ®ç±»åž‹ç§»é™¤æŽ§ä»¶ /// </summary> /// <param name="type">Type.</param> public void RemoveViewByType(System.Type type) { for (int i = 0; i < viewList.Count; i++) { if (viewList[i].GetType() == type) { viewList[i].RemoveFromParent(); i--; } } } /// <summary> /// æ ¹æ®Tag移除控件 /// </summary> /// <param name="type">Type.</param> public void RemoveViewByTag(object tag) { for (int i = 0; i < viewList.Count; i++) { if (viewList[i].Tag == tag) { viewList[i].RemoveFromParent(); i--; } } } } }