using System; using System.Collections.Generic; using CoreAnimation; using CoreGraphics; using UIKit; //两个问题,第一个是RCU协议,第二个是WIFI过多 23个 namespace Shared{ /// /// 根视图,所有视图的父视图 /// class RootView : ViewGroup { } /// /// 所有类的基类,已经全面检查 /// public abstract class View { ~View() { #if DEBUG System.Console.WriteLine("=====" + GetType() + " " + Name); #endif if (this is ViewGroup) { var viewGroup = this as ViewGroup; if (viewGroup.realViewGroup != RealView) { foreach (var view in viewGroup.viewList) { view.RealView.Dispose(); } viewGroup.realViewGroup?.Dispose(); } } RealView?.Dispose(); } /// /// 是否需要更新控件 /// /// The is need refresh. protected bool IsCanRefresh { get { View tempView = Parent; while (tempView != null) { //如果父视图是RootView,那说明视图已经加载完成,可以刷新视图 if (tempView.GetType() == typeof(RootView)) { return true; } tempView = tempView.Parent; } return false; } } /// /// 当前对应的IOS控件 /// protected UIView uiView; /// /// 内边距 /// /// The padding. public virtual Padding Padding { get; set; } /// /// 是否使能 /// /// true if enable; otherwise, false. public virtual bool Enable { get; set; } int height=LayoutParams.MatchParent; /// /// 控件的高度 /// /// The height. public virtual int Height { get { return IsCanRefresh ? (int)uiView.Frame.Height : height; } set { height = value; if (!IsCanRefresh) return; var frame = uiView.Frame; var beforeValue = frame.Height; if (value == LayoutParams.MatchParent) { frame.Height = Parent.Height; uiView.Frame = frame; } else { frame.Height = value; uiView.Frame = frame; } if (frame.Height != beforeValue) { SizeChangeEventHandler?.Invoke(this, new Size((int)frame.Width, (int)frame.Height)); } //如果父视图是VerticalScrolViewLayout,那需要重新刷新排列 if (Parent is VerticalScrolViewLayout) { (Parent as VerticalScrolViewLayout).ReLocation(); } if (Parent is VerticalRefreshLayout) { (Parent as VerticalRefreshLayout).ReLocation(); } } } int width = LayoutParams.MatchParent; /// /// 控件宽度 /// /// The width. public virtual int Width { get { return IsCanRefresh ? (int)uiView.Frame.Width : width; } set { width = value; if (!IsCanRefresh) return; var frame = uiView.Frame; var beforeValue = frame.Width; if (value == LayoutParams.MatchParent) { frame.Width = Parent.Width; uiView.Frame = frame; } else { frame.Width = value; uiView.Frame = frame; } if (frame.Width != beforeValue) { SizeChangeEventHandler?.Invoke(this, new Size((int)frame.Width, (int)frame.Height)); } //如果父视图是HorizontalScrolViewLayout,那需要重新刷新排列 if (Parent is HorizontalScrolViewLayout) { (Parent as HorizontalScrolViewLayout).ReLocation(); } } } Gravity gravity = Gravity.Frame; /// /// 当前视图对齐方式 /// /// The gravity. public Gravity Gravity { get { return gravity; } set { gravity = value; if (uiView.Superview == null) { return; } switch (value) { case Gravity.TopLeft: X = 0; Y = 0; break; case Gravity.TopCenter: X = ((int)uiView.Superview.Frame.Width - Width) / 2; Y = 0; break; case Gravity.TopRight: X = (int)uiView.Superview.Frame.Width - Width; Y = 0; break; case Gravity.CenterLeft: X = 0; Y = ((int)uiView.Superview.Frame.Height - Height) / 2; break; case Gravity.Center: X = ((int)uiView.Superview.Frame.Width - Width) / 2; Y = ((int)uiView.Superview.Frame.Height - Height) / 2; break; case Gravity.CenterRight: X = (int)uiView.Superview.Frame.Width - Width; Y = ((int)uiView.Superview.Frame.Height - Height) / 2; break; case Gravity.BottomLeft: X = 0; Y = (int)uiView.Superview.Frame.Height - Height; break; case Gravity.BottomCenter: X = ((int)uiView.Superview.Frame.Width - Width) / 2; Y = (int)uiView.Superview.Frame.Height - Height; break; case Gravity.BottomRight: X = (int)uiView.Superview.Frame.Width - Width; Y = (int)uiView.Superview.Frame.Height - Height; break; case Gravity.CenterVertical: Y = ((int)uiView.Superview.Frame.Height - Height) / 2; break; case Gravity.CenterHorizontal: X = ((int)uiView.Superview.Frame.Width - Width) / 2; break; default: break; } } } /// /// 创新需要创新的信息 /// public virtual void Refresh() { Width = width; Height = height; Gravity = gravity; Animate = animate; Radius = radius; } /// /// 左边线位置 /// /// The bottom. public int Bottom { get { return Height + Y; } } /// /// 右边线位置 /// /// The right. public int Right { get { return Width + X; } } /// /// 名称 /// /// The name. public string Name { get; set; } int x; /// /// X轴坐标 /// /// The x. public virtual int X { get { if (Parent != null && Parent is HorizontalScrolViewLayout) { return (int)uiView.Frame.X; } else { return x; } } set { x = value; //这个部局,不需要处理坐标 if (Parent != null && Parent is HorizontalScrolViewLayout) { return; } else { var frame = uiView.Frame; frame.X = value; uiView.Frame = frame; } } } int y; /// /// Y轴坐标 /// /// The y. public virtual int Y { get { if (Parent != null && (Parent is VerticalScrolViewLayout || Parent is VerticalRefreshLayout)) { return (int)uiView.Frame.Y; } else { return y; } } set { y = value; //这个部局,不需要处理坐标 if (Parent != null && (Parent is VerticalScrolViewLayout || Parent is VerticalRefreshLayout)) { return; } else { var frame = uiView.Frame; frame.Y = value; uiView.Frame = frame; } } } /// /// 当前窗口大小及位置 /// /// The frame. public virtual Size Size { get { return new Size(Width, Height); } set { Width = value.Width; Height = value.Height; } } /// /// 父容器 /// /// The parent. public ViewGroup Parent; bool isUp; bool longClicked; System.Threading.Thread thread; /// /// 点击事件 /// /// 反馈当前事件是否已经处理 /// 事件 /// 当前点击位置 internal virtual bool TouchEvent(EventActions eventAction, CGPoint point) { switch (eventAction) { case EventActions.Down: isUp = false; longClicked = false; MouseDownEventHandler?.Invoke(this, new MouseEventArgs { X = (int)point.X, Y = (int)point.Y }); if (MouseLongEventHandler != null) { try { if (thread != null && thread.IsAlive) { thread.Abort(); } thread = new System.Threading.Thread(() => { System.Threading.Thread.Sleep(800); if (!isUp) { longClicked = true; Application.RunOnMainThread(() => { MouseLongEventHandler(this, new MouseEventArgs { X = (int)point.X, Y = (int)point.Y }); }); } }) { IsBackground = true }; thread.Start(); } catch { } } break; case EventActions.Move: MouseMoveEventHandler?.Invoke(this, new MouseEventArgs { X = (int)point.X, Y = (int)point.Y }); break; case EventActions.Up: isUp = true; if (!longClicked) { MouseUpEventHandler?.Invoke(this, new MouseEventArgs { X = (int)point.X, Y = (int)point.Y }); } break; case EventActions.Cancel: isUp = true; break; } return false; } /// /// 点击按下事件 /// public EventHandler MouseDownEventHandler; /// /// 控件上移动事件 /// public EventHandler MouseMoveEventHandler; /// /// 点击弹起事件 /// public EventHandler MouseUpEventHandler; public EventHandler MouseLongEventHandler; /// /// 大小变化事件 /// public EventHandler SizeChangeEventHandler; uint backgroundColor; /// /// 背景颜色 /// /// The color of the background. public virtual uint BackgroundColor { get { return backgroundColor; } set { backgroundColor = value; if (!IsCanRefresh) { return; } byte r, g, b, a; r = (byte)(backgroundColor / 256 / 256 % 256); g = (byte)(backgroundColor / 256 % 256); b = (byte)(backgroundColor % 256); a = (byte)(backgroundColor / 256 / 256 / 256 % 256); uiView.BackgroundColor = UIKit.UIColor.FromRGBA(r, g, b, a); } } /// /// 获取真实的控件 /// /// The real view. internal UIKit.UIView RealView { get { return uiView; } } /// /// 将控制移到最前 /// public virtual void BringToFront() { if (!IsCanRefresh) { return; } uiView.Superview.BringSubviewToFront(uiView); } /// /// 将控件移到最后 /// public virtual void SendToBack() { if (!IsCanRefresh) { return; } uiView.Superview.SendSubviewToBack(uiView); } System.Collections.Generic.Dictionary tag; /// /// 键值对,方便开发用 /// /// Key. /// Value. public void AddTag(string key, object value) { if (tag == null) { tag = new System.Collections.Generic.Dictionary(); } tag.Remove(key); tag.Add(key, value); } /// /// 删除指定键值对 /// /// Key. public void RemoveTag(string key) { if (tag == null) { return; } tag.Remove(key); } /// /// 根据键获取值 /// /// The tag by key. /// Key. public object GetTagByKey(string key) { if (tag == null) { return null; } object value; tag.TryGetValue(key, out value); return value; } /// /// 全部删除Tag /// /// The tag. public void ClearTag() { if (tag != null) { tag.Clear(); } } /// /// 方便开发者开发用 /// public object Tag; /// /// 是否显示 /// /// true if visible; otherwise, false. public bool Visible { get { return !uiView.Hidden; } set { uiView.Hidden = !value; } } /// /// 从父视图里面移除掉 /// public virtual void RemoveFromParent() { if (Parent == null) { return; } Parent.Remove(this); } Animate animate = Animate.None; /// /// 动画方式 /// /// The animate. public Animate Animate { get { return animate; } set { animate = value; if (!IsCanRefresh) { return; } var frameBefore = RealView.Frame; var frame = frameBefore; switch (animate) { case Animate.DownToUp: frame.Y += RealView.Frame.Height; RealView.Frame = frame; UIView.AnimateNotify(0.2f, 0.001f, UIViewAnimationOptions.CurveLinear, () => { RealView.Frame = frameBefore; }, null); break; case Animate.UpToDown: frame.Y -= RealView.Frame.Height; RealView.Frame = frame; UIView.AnimateNotify(0.2f, 0.001f, UIViewAnimationOptions.CurveLinear, () => { RealView.Frame = frameBefore; }, null); break; case Animate.LeftToRight: System.Threading.Tasks.Task.Run(() => { System.Threading.Thread.Sleep(1); Application.RunOnMainThread(() => { UIView.BeginAnimations(""); UIView.SetAnimationCurve(UIViewAnimationCurve.EaseInOut); UIView.SetAnimationDuration(1.0); UIView.SetAnimationTransition(UIViewAnimationTransition.FlipFromLeft, RealView, true);// cache:YES]; UIView.CommitAnimations(); }); }); break; case Animate.RightToLeft: System.Threading.Tasks.Task.Run(() => { System.Threading.Thread.Sleep(1); Application.RunOnMainThread(() => { UIView.BeginAnimations(""); UIView.SetAnimationCurve(UIViewAnimationCurve.EaseInOut); UIView.SetAnimationDuration(1.0); UIView.SetAnimationTransition(UIViewAnimationTransition.FlipFromRight, RealView, true);// cache:YES]; UIView.CommitAnimations(); }); }); break; case Animate.Rotation: rotation(); break; } } } void rotation() { UIView.Animate(4, 0, UIViewAnimationOptions.CurveLinear, () => { RealView.Transform = CGAffineTransform.Rotate(RealView.Transform, 3.1415926535898f / 2); }, () => { if (IsCanRefresh) rotation(); }); } /// /// 透明度设置 /// /// The alpha. public nfloat Alpha { get { return uiView.Alpha; } set { uiView.Alpha = value; } } //4个圆角分别对应的值 //float topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius; ///未实现,代完善 //public void SetAloneRadius(float topLeft, float topRight, float bottomLeft, float bottomRight) //{ // IsSetAloneRadius = true; // topLeftRadius = topLeft; // topRightRadius = topRight; // bottomLeftRadius = bottomLeft; // bottomRightRadius = bottomRight; // RefreshAloneCorner(); //} //是否单独设置圆角 private bool IsSetAloneRadius; float mAloneRadius; float mRadiusId; /// /// 指定位置 设置相同的圆角 /// public void SetCornerWithSameRadius(float mRadius, int mSetID) { //bool btopLeft, btopRight, bbottomLeft, bbottomRight; IsSetAloneRadius = true; mAloneRadius = mRadius; mRadiusId = mSetID; RefreshAloneCorner(); } /// /// 更新圆角 /// /// The corner. private void RefreshAloneCorner() { BackgroundColor = backgroundColor; uiView.Layer.MasksToBounds = true; UIRectCorner corner = (UIRectCorner)(mRadiusId); if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0)) { uiView.Layer.CornerRadius = mAloneRadius; uiView.Layer.MaskedCorners = (CACornerMask)corner; } else { UIBezierPath mUIBezierPath = UIBezierPath.FromRoundedRect(uiView.Bounds, corner, new CoreGraphics.CGSize(mAloneRadius, mAloneRadius)); CAShapeLayer mCAShapeLayer = new CAShapeLayer(); mCAShapeLayer.Frame = uiView.Bounds; mCAShapeLayer.Path = mUIBezierPath.CGPath; uiView.Layer.Mask = mCAShapeLayer; } uiView.Layer.BorderWidth = BorderWidth; byte r, g, b, a; r = (byte)(borderColor / 256 / 256 % 256); g = (byte)(borderColor / 256 % 256); b = (byte)(borderColor % 256); a = (byte)(borderColor / 256 / 256 / 256 % 256); uiView.Layer.BorderColor = new CGColor(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f); } uint radius; /// /// 圆角大小 /// /// The corner. public uint Radius { get { return radius; } set { IsSetAloneRadius = false; radius = value; if (!IsCanRefresh) { return; } refreshCorner(); } } /// /// 更新圆角 /// /// The corner. void refreshCorner() { BackgroundColor = backgroundColor; if (Radius == 0) { return; } uiView.Layer.MasksToBounds = true; uiView.Layer.CornerRadius = Radius; uiView.Layer.BorderWidth = BorderWidth; byte r, g, b, a; r = (byte)(borderColor / 256 / 256 % 256); g = (byte)(borderColor / 256 % 256); b = (byte)(borderColor % 256); a = (byte)(borderColor / 256 / 256 / 256 % 256); uiView.Layer.BorderColor = new CGColor(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f); } uint borderWidth; /// /// 边框线大小 /// /// The width of the border. public uint BorderWidth { get { return borderWidth; } set { borderWidth = value; Radius = radius; } } uint borderColor = 0xFFCCCCCC; /// /// 边框线颜色 /// public uint BorderColor { get { return borderColor; } set { borderColor = value; Radius = radius; } } /// /// 旋转View /// /// 旋转角度 public void SetRotation(float mRotation) { if (mRotation > 180) { mRotation = mRotation - 360f; } var mCGAffineTransform = CGAffineTransform.MakeRotation((nfloat)(mRotation * Math.PI / 180.0)); uiView.Transform = mCGAffineTransform; } /// /// 设置阴影效果 /// /// 是否显示阴影 public void SetViewShadow(bool bShow, float mOffsetY = 5.0f) { if (bShow) { uiView.Layer.ShadowOffset = new CGSize(0, mOffsetY); uiView.Layer.ShadowOpacity = 0.5f; uiView.Layer.ShadowColor = UIColor.Gray.CGColor; uiView.ClipsToBounds = false; } else { //uiView.Layer.ShadowOffset = new CGSize(0, 0); //uiView.Layer.ShadowOpacity = 0f; uiView.ClipsToBounds = true; } } } /// /// 视图大小 /// public class Size { /// /// 视图大小 /// /// Width. /// Height. public Size(int width,int height) { Width = width; Height = height; } /// /// 宽度 /// public int Width { get; set; } /// /// 高度 /// public int Height { get; set; } } /// /// 内边距 /// public class Padding { /// /// 内边距 /// /// Top. /// Left. /// Bottom. /// Right. public Padding(int top,int left,int bottom,int right) { Top = top; Left = left; Bottom = bottom; Right = right; } /// /// 顶部 /// public int Top { get; set; } /// /// 左边 /// public int Left { get; set; } /// /// 底部 /// public int Bottom { get; set; } /// /// 右边 /// public int Right { get; set; } } /// /// 点击时基本参数 /// public class MouseEventArgs : EventArgs { /// /// X坐标 /// public float X; /// /// Y坐标 /// public float Y; } /// /// 点击事件 /// enum EventActions { /// /// Down /// Down, /// /// Move /// Move, /// /// UP /// Up, /// /// Cancel /// Cancel, } }