using System;
|
using UIKit;
|
using Shared.IO;
|
using CoreGraphics;
|
using Foundation;
|
|
namespace Shared
|
{
|
/// <summary>
|
/// 位置布局
|
/// </summary>
|
public class CoverFlowLayout : ViewGroup
|
{
|
readonly MyCoverFlowLayout iosUIScrolView;
|
|
readonly UIPageControl iosUIPageControl;
|
|
/// <summary>
|
/// 页面变化事件
|
/// </summary>
|
public Action<CoverFlowLayout,int> PageChange;
|
|
/// <summary>
|
/// 垂直方向滚动视图
|
/// </summary>
|
class MyCoverFlowLayout : UIScrollView
|
{
|
[Weak] CoverFlowLayout CoverFlowLayout;
|
public MyCoverFlowLayout(CoverFlowLayout CoverFlowLayout)
|
{
|
|
this.CoverFlowLayout = CoverFlowLayout;
|
DecelerationEnded += (s, e) =>
|
{
|
var tempUIScrolView = s as MyCoverFlowLayout;
|
//获取当前界面的索引
|
var tempPageIndex = Convert.ToInt32(tempUIScrolView.ContentOffset.X / (tempUIScrolView.Frame.Width - this.CoverFlowLayout._RowPadding));
|
//通知界面索引变化
|
if (this.CoverFlowLayout.PageIndex != tempPageIndex)
|
{
|
this.CoverFlowLayout.PageIndex = tempPageIndex;
|
Shared.HDLUtils.WriteLine($"pageIndex-------{tempPageIndex}");
|
this.CoverFlowLayout.iosUIPageControl.CurrentPage = tempPageIndex;
|
this.CoverFlowLayout.PageChange?.Invoke(this.CoverFlowLayout, this.CoverFlowLayout.pageIndex);
|
}
|
};
|
}
|
/// <summary>
|
/// 点击开始
|
/// </summary>
|
/// <param name="touches">Touches.</param>
|
/// <param name="evt">Evt.</param>
|
public override void TouchesBegan(NSSet touches, UIEvent evt)
|
{
|
CoverFlowLayout?.TouchEvent(EventActions.Down, (touches.AnyObject as UITouch).LocationInView(this));
|
}
|
/// <summary>
|
/// 移动
|
/// </summary>
|
/// <param name="touches">Touches.</param>
|
/// <param name="evt">Evt.</param>
|
public override void TouchesMoved(NSSet touches, UIEvent evt)
|
{
|
CoverFlowLayout?.TouchEvent(EventActions.Move, (touches.AnyObject as UITouch).LocationInView(this));
|
}
|
|
/// <summary>
|
/// 点击弹起
|
/// </summary>
|
/// <param name="touches">Touches.</param>
|
/// <param name="evt">Evt.</param>
|
public override void TouchesEnded(NSSet touches, UIEvent evt)
|
{
|
CoverFlowLayout?.TouchEvent(EventActions.Up, (touches.AnyObject as UITouch).LocationInView(this));
|
}
|
|
public override void TouchesCancelled(NSSet touches, UIEvent evt)
|
{
|
CoverFlowLayout?.TouchEvent(EventActions.Cancel, (touches.AnyObject as UITouch).LocationInView(this));
|
}
|
/// <summary>
|
/// 框架默认加了两个UIImageView ,这样可以去掉默认添加
|
/// </summary>
|
/// <param name="view">View.</param>
|
public override void AddSubview(UIView view)
|
{
|
if (view.GetType() == typeof(UIImageView) && view.Tag != int.MinValue)
|
{
|
return;
|
}
|
|
if (view.ToString().Contains("UIScrollViewScrollIndicator"))
|
{
|
return;
|
}
|
base.AddSubview(view);
|
}
|
}
|
|
/// <summary>
|
/// 构造函数
|
/// </summary>
|
public CoverFlowLayout()
|
{
|
viewGroup = new UIView();
|
iosUIPageControl = new UIPageControl { Enabled = false };
|
iosUIScrolView = new MyCoverFlowLayout(this) { };
|
|
realViewGroup = iosUIScrolView;
|
|
var frame = iosUIPageControl.Frame;
|
frame.Height = 16;
|
iosUIPageControl.Frame = frame;
|
//启动翻页功能
|
//iosUIScrolView.PagingEnabled = true;
|
|
//iosUIScrolView.Bounces = false;
|
iosUIScrolView.ShowsHorizontalScrollIndicator = false;
|
viewGroup.AddSubview(iosUIScrolView);
|
viewGroup.AddSubview(iosUIPageControl);
|
|
|
_PagePadding = Application.GetRealWidth(41);
|
_RowPadding = Application.GetRealWidth(108);
|
|
|
}
|
|
//两个Page之间的距离
|
int _PagePadding;
|
public int PagePadding
|
{
|
get
|
{
|
return _PagePadding;
|
}
|
set
|
{
|
_PagePadding = value;
|
ReLocation();
|
}
|
}
|
//page的外边距
|
int _RowPadding;
|
public int RowPadding
|
{
|
get
|
{
|
return _RowPadding;
|
}
|
set
|
{
|
_RowPadding = value;
|
ReLocation();
|
}
|
}
|
|
|
/// <summary>
|
/// 是否显示下面一排的点
|
/// </summary>
|
/// <value><c>true</c> if is show point; otherwise, <c>false</c>.</value>
|
public bool IsShowPoint
|
{
|
get
|
{
|
return !iosUIPageControl.Hidden;
|
}
|
set
|
{
|
iosUIPageControl.Hidden = !value;
|
}
|
}
|
|
/// <summary>
|
/// 是否允许滑动
|
/// </summary>
|
/// <value><c>true</c> if scroll enabled; otherwise, <c>false</c>.</value>
|
public bool ScrollEnabled
|
{
|
get
|
{
|
return (iosUIScrolView as MyCoverFlowLayout).ScrollEnabled;
|
}
|
set
|
{
|
(iosUIScrolView as MyCoverFlowLayout).ScrollEnabled = value;
|
}
|
}
|
|
int pageIndex;
|
/// <summary>
|
/// 设置或者获取当前的界面索引
|
/// </summary>
|
/// <value>The index of the page.</value>
|
public int PageIndex
|
{
|
get
|
{
|
return pageIndex;
|
}
|
set
|
{
|
if (value < 0 || ChildrenCount <= value)
|
{
|
return;
|
}
|
int beforePageIndex = pageIndex;
|
pageIndex = value;
|
if (!IsCanRefresh)
|
{
|
return;
|
}
|
|
var viewSize = iosUIScrolView.Frame.Size;
|
|
|
var rect = new CGRect(pageIndex * viewSize.Width, 0, viewSize.Width, viewSize.Height);
|
if (pageIndex != 0) {
|
var frame = iosUIScrolView.Subviews[pageIndex].Frame;
|
var w = frame.X - _RowPadding;
|
|
rect = new CGRect(w, 0, viewSize.Width, viewSize.Height);
|
Shared.HDLUtils.WriteLine($"CGRect xx-------{w}");
|
}
|
|
Shared.HDLUtils.WriteLine($"pageIndex xx-------{pageIndex}");
|
iosUIScrolView.ScrollRectToVisible(rect, false);
|
|
if (beforePageIndex != pageIndex)
|
{
|
iosUIPageControl.CurrentPage = pageIndex;
|
PageChange?.Invoke(this, pageIndex);
|
}
|
}
|
}
|
/// <summary>
|
/// 增加子控件
|
/// </summary>
|
/// <param name="view">View.</param>
|
public override void AddChidren(View view)
|
{
|
//var v = Application.AverageScale;
|
//var vv = Application.CurrentHeight;
|
//var dddd = Application.DesignWidth;
|
base.AddChidren(view);
|
iosUIPageControl.Pages = ChildrenCount;
|
ReLocation();
|
}
|
|
/// <summary>
|
/// 重新排位置及设备内容大小
|
/// </summary>
|
public virtual void ReLocation()
|
{
|
if (iosUIScrolView.Subviews.Length == 0)
|
{
|
return;
|
}
|
|
var frame0 = iosUIScrolView.Subviews[0].Frame;
|
frame0.X = _RowPadding;
|
iosUIScrolView.Subviews[0].Frame = frame0;
|
|
|
|
for (int i = 1; i < iosUIScrolView.Subviews.Length; i++)
|
{
|
var frame = iosUIScrolView.Subviews[i].Frame;
|
frame.X = iosUIScrolView.Subviews[i - 1].Frame.Right+_PagePadding;
|
iosUIScrolView.Subviews[i].Frame = frame;
|
}
|
|
iosUIScrolView.ContentSize = new CoreGraphics.CGSize(iosUIScrolView.Subviews[iosUIScrolView.Subviews.Length - 1].Frame.Right+_RowPadding, iosUIScrolView.Frame.Height);
|
}
|
|
|
|
/// <summary>
|
/// 控件宽度
|
/// </summary>
|
/// <value>The width.</value>
|
public override int Width
|
{
|
get
|
{
|
return base.Width;
|
}
|
set
|
{
|
base.Width = value;
|
if (!IsCanRefresh)
|
return;
|
var frame = iosUIPageControl.Frame;
|
frame.Width = Width;
|
iosUIPageControl.Frame = frame;
|
}
|
}
|
|
/// <summary>
|
/// 控件的高度
|
/// </summary>
|
/// <value>The height.</value>
|
public override int Height
|
{
|
get
|
{
|
return base.Height;
|
}
|
set
|
{
|
base.Height = value;
|
if (!IsCanRefresh)
|
return;
|
var frame = iosUIPageControl.Frame;
|
frame.Y = Height - frame.Height;
|
iosUIPageControl.Frame = frame;
|
}
|
}
|
|
/// <summary>
|
/// 移除当前控件
|
/// </summary>
|
/// <param name="view">View.</param>
|
internal override void Remove(View view)
|
{
|
base.Remove(view);
|
ReLocation();
|
PageIndex = ChildrenCount - 1;
|
iosUIPageControl.Pages = ChildrenCount;
|
iosUIPageControl.CurrentPage = PageIndex;
|
}
|
|
/// <summary>
|
/// 移除所有的控件
|
/// </summary>
|
public override void RemoveAll()
|
{
|
base.RemoveAll();
|
ReLocation();
|
iosUIPageControl.Pages = 0;
|
PageIndex = 0;
|
iosUIPageControl.CurrentPage = 0;
|
}
|
|
/// <summary>
|
/// 根据索引移除控件
|
/// </summary>
|
/// <param name="index">Index.</param>
|
public override void RemoveAt(int index)
|
{
|
Remove(viewList[index]);
|
}
|
}
|
}
|