using System;
|
using CoreGraphics;
|
using UIKit;
|
|
//两个问题,第一个是RCU协议,第二个是WIFI过多 23个
|
namespace Shared{
|
|
/// <summary>
|
/// 根视图,所有视图的父视图
|
/// </summary>
|
class RootView : ViewGroup { }
|
/// <summary>
|
/// 所有类的基类,已经全面检查
|
/// </summary>
|
public abstract class View
|
{
|
/// <summary>
|
/// 是否需要更新控件
|
/// </summary>
|
/// <value>The is need refresh.</value>
|
protected bool IsCanRefresh
|
{
|
get
|
{
|
View tempView = Parent;
|
while (tempView != null)
|
{
|
//如果父视图是RootView,那说明视图已经加载完成,可以刷新视图
|
if (tempView.GetType() == typeof(RootView))
|
{
|
return true;
|
}
|
tempView = tempView.Parent;
|
}
|
return false;
|
}
|
}
|
|
/// <summary>
|
/// 唯一的ID
|
/// </summary>
|
static int number = 1;
|
|
UIView tempUIView;
|
/// <summary>
|
/// 当前对应的IOS控件
|
/// </summary>
|
protected UIView uiView
|
{
|
get
|
{
|
return tempUIView;
|
}
|
set
|
{
|
tempUIView = value;
|
}
|
}
|
|
/// <summary>
|
/// 父视图
|
/// </summary>
|
public View()
|
{
|
ID = number++;
|
}
|
|
/// <summary>
|
/// 唯一的ID
|
/// </summary>
|
/// <value>The I.</value>
|
public int ID { get; internal set; }
|
|
/// <summary>
|
/// 内边距
|
/// </summary>
|
/// <value>The padding.</value>
|
public virtual Padding Padding
|
{
|
get;
|
set;
|
}
|
|
/// <summary>
|
/// 是否使能
|
/// </summary>
|
/// <value><c>true</c> if enable; otherwise, <c>false</c>.</value>
|
public virtual bool Enable
|
{
|
get;
|
set;
|
}
|
|
int height=LayoutParams.MatchParent;
|
/// <summary>
|
/// 控件的高度
|
/// </summary>
|
/// <value>The height.</value>
|
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)
|
{
|
if (SizeChangeEventHandler != null)
|
{
|
SizeChangeEventHandler(this, new Size((int)frame.Width, (int)frame.Height));
|
}
|
}
|
|
//如果父视图是VerticalScrolViewLayout,那需要重新刷新排列
|
if (Parent.GetType() == typeof(VerticalScrolViewLayout))
|
{
|
(Parent as VerticalScrolViewLayout).ReLocation();
|
}
|
}
|
}
|
|
int width = LayoutParams.MatchParent;
|
/// <summary>
|
/// 控件宽度
|
/// </summary>
|
/// <value>The width.</value>
|
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)
|
{
|
if (SizeChangeEventHandler != null)
|
{
|
SizeChangeEventHandler(this, new Size((int)frame.Width, (int)frame.Height));
|
}
|
}
|
|
//如果父视图是HorizontalScrolViewLayout,那需要重新刷新排列
|
if (Parent.GetType() == typeof(HorizontalScrolViewLayout))
|
{
|
(Parent as HorizontalScrolViewLayout).ReLocation();
|
}
|
|
}
|
}
|
|
Gravity gravity = Gravity.Frame;
|
/// <summary>
|
/// 当前视图对齐方式
|
/// </summary>
|
/// <value>The gravity.</value>
|
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;
|
}
|
}
|
}
|
|
/// <summary>
|
/// 创新需要创新的信息
|
/// </summary>
|
public virtual void Refresh()
|
{
|
Width = width;
|
Height = height;
|
Gravity = gravity;
|
Animate = animate;
|
Radius = radius;
|
}
|
|
/// <summary>
|
/// 左边线位置
|
/// </summary>
|
/// <value>The bottom.</value>
|
public int Bottom
|
{
|
get
|
{
|
return Height + Y;
|
}
|
}
|
|
/// <summary>
|
/// 右边线位置
|
/// </summary>
|
/// <value>The right.</value>
|
public int Right
|
{
|
get
|
{
|
return Width + X;
|
}
|
}
|
|
/// <summary>
|
/// 名称
|
/// </summary>
|
/// <value>The name.</value>
|
public string Name { get; set; }
|
|
int x;
|
/// <summary>
|
/// X轴坐标
|
/// </summary>
|
/// <value>The x.</value>
|
public virtual int X
|
{
|
get
|
{
|
if (Parent != null && Parent.GetType() == typeof(HorizontalScrolViewLayout))
|
{
|
return (int)uiView.Frame.X;
|
}
|
else
|
{
|
return x;
|
}
|
}
|
set
|
{
|
x = value;
|
|
//这个部局,不需要处理坐标
|
if (Parent != null && Parent.GetType() == typeof(HorizontalScrolViewLayout))
|
{
|
return;
|
}
|
else
|
{
|
var frame = uiView.Frame;
|
frame.X = value;
|
uiView.Frame = frame;
|
}
|
}
|
}
|
|
int y;
|
/// <summary>
|
/// Y轴坐标
|
/// </summary>
|
/// <value>The y.</value>
|
public virtual int Y
|
{
|
get
|
{
|
if (Parent != null && Parent.GetType() == typeof(VerticalScrolViewLayout))
|
{
|
return (int)uiView.Frame.Y;
|
}
|
else
|
{
|
return y;
|
}
|
}
|
set
|
{
|
y = value;
|
|
//这个部局,不需要处理坐标
|
if (Parent != null && Parent.GetType() == typeof(VerticalScrolViewLayout))
|
{
|
return;
|
}
|
else {
|
var frame = uiView.Frame;
|
frame.Y = value;
|
uiView.Frame = frame;
|
}
|
}
|
}
|
|
/// <summary>
|
/// 当前窗口大小及位置
|
/// </summary>
|
/// <value>The frame.</value>
|
public virtual Size Size
|
{
|
get
|
{
|
return new Size(Width, Height);
|
}
|
set
|
{
|
Width = value.Width;
|
Height = value.Height;
|
}
|
}
|
|
/// <summary>
|
/// 父容器
|
/// </summary>
|
/// <value>The parent.</value>
|
public ViewGroup Parent { get; set; }
|
|
/// <summary>
|
/// 点击事件
|
/// </summary>
|
/// <returns>反馈当前事件是否已经处理</returns>
|
/// <param name="e">事件</param>
|
/// <param name="point">当前点击位置</param>
|
internal virtual bool TouchEvent(EventActions eventAction, CGPoint point)
|
{
|
switch (eventAction)
|
{
|
case EventActions.Down:
|
if (null != MouseDownEventHandler)
|
{
|
MouseDownEventHandler(this, new MouseEventArgs { X = (int)point.X, Y = (int)point.Y });
|
}
|
break;
|
case EventActions.Move:
|
if (null != MouseMoveEventHandler)
|
{
|
MouseMoveEventHandler(this, new MouseEventArgs { X = (int)point.X, Y = (int)point.Y });
|
}
|
break;
|
case EventActions.Up:
|
//执行了长按事件后就不执行弹起事件
|
if (null != MouseUpEventHandler)
|
{
|
MouseUpEventHandler(this, new MouseEventArgs { X = (int)point.X, Y = (int)point.Y });
|
}
|
break;
|
}
|
return false;
|
}
|
|
/// <summary>
|
/// 点击按下事件
|
/// </summary>
|
public event EventHandler<MouseEventArgs> MouseDownEventHandler;
|
|
/// <summary>
|
/// 控件上移动事件
|
/// </summary>
|
public event EventHandler<MouseEventArgs> MouseMoveEventHandler;
|
|
/// <summary>
|
/// 点击弹起事件
|
/// </summary>
|
public event EventHandler<MouseEventArgs> MouseUpEventHandler;
|
|
UILongPressGestureRecognizer uILongPressGestureRecognizer;
|
//增加一个引用委托链表头部的私有字段
|
EventHandler<MouseEventArgs> mouseLongEventHandler;
|
/// <summary>
|
/// 长按点击事件
|
/// </summary>
|
public event EventHandler<MouseEventArgs> MouseLongEventHandler
|
{
|
//显式实现'add'方法
|
add
|
{
|
if(uILongPressGestureRecognizer==null){
|
uILongPressGestureRecognizer = new UILongPressGestureRecognizer((obj) =>
|
{
|
if (mouseLongEventHandler!=null&&obj.State == UIGestureRecognizerState.Began)
|
{
|
mouseLongEventHandler(this, new MouseEventArgs());
|
}
|
})
|
{ MinimumPressDuration = 0.5 };
|
}
|
|
mouseLongEventHandler = value;
|
uiView.RemoveGestureRecognizer(uILongPressGestureRecognizer);
|
uiView.AddGestureRecognizer(uILongPressGestureRecognizer);
|
}
|
//显式实现'remove'方法
|
remove
|
{
|
//从委托链表从中移除处理程序(以'value'为参数)
|
mouseLongEventHandler = null;
|
uiView.RemoveGestureRecognizer(uILongPressGestureRecognizer);
|
}
|
}
|
|
/// <summary>
|
/// 大小变化事件
|
/// </summary>
|
public event EventHandler<Size> SizeChangeEventHandler;
|
|
|
uint backgroundColor;
|
/// <summary>
|
/// 背景颜色
|
/// </summary>
|
/// <value>The color of the background.</value>
|
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);
|
}
|
}
|
|
/// <summary>
|
/// 获取真实的控件
|
/// </summary>
|
/// <value>The real view.</value>
|
internal UIKit.UIView RealView
|
{
|
get { return uiView; }
|
}
|
|
/// <summary>
|
/// 将控制移到最前
|
/// </summary>
|
public virtual void BringToFront()
|
{
|
if (!IsCanRefresh)
|
{
|
return;
|
}
|
uiView.Superview.BringSubviewToFront(uiView);
|
}
|
|
/// <summary>
|
/// 将控件移到最后
|
/// </summary>
|
public virtual void SendToBack()
|
{
|
if (!IsCanRefresh)
|
{
|
return;
|
}
|
uiView.Superview.SendSubviewToBack(uiView);
|
}
|
|
System.Collections.Generic.Dictionary<string, object> tag;
|
|
/// <summary>
|
/// 键值对,方便开发用
|
/// </summary>
|
/// <param name="key">Key.</param>
|
/// <param name="value">Value.</param>
|
public void AddTag(string key, object value)
|
{
|
if (tag == null)
|
{
|
tag = new System.Collections.Generic.Dictionary<string, object>();
|
}
|
tag.Remove(key);
|
tag.Add(key, value);
|
}
|
|
/// <summary>
|
/// 删除指定键值对
|
/// </summary>
|
/// <param name="key">Key.</param>
|
public void RemoveTag(string key)
|
{
|
if (tag == null)
|
{
|
return;
|
}
|
tag.Remove(key);
|
}
|
|
/// <summary>
|
/// 根据键获取值
|
/// </summary>
|
/// <returns>The tag by key.</returns>
|
/// <param name="key">Key.</param>
|
public object GetTagByKey(string key)
|
{
|
if (tag == null)
|
{
|
return null;
|
}
|
object value;
|
tag.TryGetValue(key, out value);
|
return value;
|
}
|
|
/// <summary>
|
/// 全部删除Tag
|
/// </summary>
|
/// <returns>The tag.</returns>
|
public void ClearTag()
|
{
|
if (tag != null)
|
{
|
tag.Clear();
|
}
|
}
|
|
/// <summary>
|
/// 方便开发者开发用
|
/// </summary>
|
public object Tag;
|
|
/// <summary>
|
/// 是否显示
|
/// </summary>
|
/// <value><c>true</c> if visible; otherwise, <c>false</c>.</value>
|
public bool Visible
|
{
|
get
|
{
|
return !uiView.Hidden;
|
}
|
set
|
{
|
uiView.Hidden = !value;
|
}
|
}
|
|
/// <summary>
|
/// 从父视图里面移除掉
|
/// </summary>
|
public virtual void RemoveFromParent()
|
{
|
if (Parent == null)
|
{
|
return;
|
}
|
|
Parent.Remove(this);
|
|
}
|
|
|
Animate animate = Animate.None;
|
/// <summary>
|
/// 动画方式
|
/// </summary>
|
/// <value>The animate.</value>
|
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.6f, 0.001f, UIViewAnimationOptions.CurveLinear, () =>
|
{
|
RealView.Frame = frameBefore;
|
}, null);
|
break;
|
case Animate.UpToDown:
|
frame.Y -= RealView.Frame.Height;
|
RealView.Frame = frame;
|
UIView.AnimateNotify(0.6f, 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();
|
});
|
}
|
|
/// <summary>
|
/// 透明度设置
|
/// </summary>
|
/// <value>The alpha.</value>
|
public nfloat Alpha
|
{
|
get
|
{
|
return uiView.Alpha;
|
}
|
set
|
{
|
uiView.Alpha = value;
|
}
|
}
|
|
|
uint radius;
|
/// <summary>
|
/// 圆角大小
|
/// </summary>
|
/// <value>The corner.</value>
|
public uint Radius
|
{
|
get
|
{
|
return radius;
|
}
|
set
|
{
|
radius = value;
|
if (!IsCanRefresh)
|
{
|
return;
|
}
|
refreshCorner();
|
}
|
}
|
|
|
/// <summary>
|
/// 更新圆角
|
/// </summary>
|
/// <returns>The corner.</returns>
|
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;
|
/// <summary>
|
/// 边框线大小
|
/// </summary>
|
/// <value>The width of the border.</value>
|
public uint BorderWidth
|
{
|
get
|
{
|
return borderWidth;
|
}
|
set
|
{
|
borderWidth = value;
|
Radius = radius;
|
}
|
}
|
|
uint borderColor = 0xFFCCCCCC;
|
/// <summary>
|
/// 边框线颜色
|
/// </summary>
|
public uint BorderColor
|
{
|
get
|
{
|
return borderColor;
|
}
|
set
|
{
|
borderColor = value;
|
Radius = radius;
|
}
|
}
|
|
}
|
|
/// <summary>
|
/// 视图大小
|
/// </summary>
|
public class Size
|
{
|
/// <summary>
|
/// 视图大小
|
/// </summary>
|
/// <param name="width">Width.</param>
|
/// <param name="height">Height.</param>
|
public Size(int width,int height)
|
{
|
Width = width;
|
Height = height;
|
}
|
|
|
/// <summary>
|
/// 宽度
|
/// </summary>
|
public int Width {
|
get;
|
set;
|
}
|
/// <summary>
|
/// 高度
|
/// </summary>
|
public int Height {
|
get;
|
set;
|
}
|
|
}
|
|
/// <summary>
|
/// 内边距
|
/// </summary>
|
public class Padding
|
{
|
/// <summary>
|
/// 内边距
|
/// </summary>
|
/// <param name="top">Top.</param>
|
/// <param name="left">Left.</param>
|
/// <param name="bottom">Bottom.</param>
|
/// <param name="right">Right.</param>
|
public Padding(int top,int left,int bottom,int right)
|
{
|
Top = top;
|
Left = left;
|
Bottom = bottom;
|
Right = right;
|
}
|
/// <summary>
|
/// 顶部
|
/// </summary>
|
public int Top {
|
get;
|
set;
|
}
|
/// <summary>
|
/// 左边
|
/// </summary>
|
public int Left {
|
get;
|
set;
|
}
|
/// <summary>
|
/// 底部
|
/// </summary>
|
public int Bottom {
|
get;
|
set;
|
}
|
/// <summary>
|
/// 右边
|
/// </summary>
|
public int Right {
|
get;
|
set;
|
}
|
}
|
|
/// <summary>
|
/// 点击时基本参数
|
/// </summary>
|
public class MouseEventArgs : EventArgs
|
{
|
/// <summary>
|
/// X坐标
|
/// </summary>
|
public float X;
|
/// <summary>
|
/// Y坐标
|
/// </summary>
|
public float Y;
|
}
|
|
/// <summary>
|
/// 点击事件
|
/// </summary>
|
enum EventActions
|
{
|
/// <summary>
|
/// Down
|
/// </summary>
|
Down,
|
/// <summary>
|
/// Move
|
/// </summary>
|
Move,
|
/// <summary>
|
/// UP
|
/// </summary>
|
Up,
|
/// <summary>
|
/// Cancel
|
/// </summary>
|
Cancel,
|
}
|
}
|