using System;
using UIKit;
using Shared.IO;
using CoreGraphics;
using Foundation;
namespace Shared
{
///
/// 位置布局
///
public class CoverFlowLayout : ViewGroup
{
readonly MyCoverFlowLayout iosUIScrolView;
readonly UIPageControl iosUIPageControl;
///
/// 页面变化事件
///
public Action PageChange;
///
/// 垂直方向滚动视图
///
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;
System.Console.WriteLine($"pageIndex-------{tempPageIndex}");
this.CoverFlowLayout.iosUIPageControl.CurrentPage = tempPageIndex;
this.CoverFlowLayout.PageChange?.Invoke(this.CoverFlowLayout, this.CoverFlowLayout.pageIndex);
}
};
}
///
/// 点击开始
///
/// Touches.
/// Evt.
public override void TouchesBegan(NSSet touches, UIEvent evt)
{
CoverFlowLayout?.TouchEvent(EventActions.Down, (touches.AnyObject as UITouch).LocationInView(this));
}
///
/// 移动
///
/// Touches.
/// Evt.
public override void TouchesMoved(NSSet touches, UIEvent evt)
{
CoverFlowLayout?.TouchEvent(EventActions.Move, (touches.AnyObject as UITouch).LocationInView(this));
}
///
/// 点击弹起
///
/// Touches.
/// Evt.
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));
}
///
/// 框架默认加了两个UIImageView ,这样可以去掉默认添加
///
/// View.
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);
}
}
///
/// 构造函数
///
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();
}
}
///
/// 是否显示下面一排的点
///
/// true if is show point; otherwise, false.
public bool IsShowPoint
{
get
{
return !iosUIPageControl.Hidden;
}
set
{
iosUIPageControl.Hidden = !value;
}
}
///
/// 是否允许滑动
///
/// true if scroll enabled; otherwise, false.
public bool ScrollEnabled
{
get
{
return (iosUIScrolView as MyCoverFlowLayout).ScrollEnabled;
}
set
{
(iosUIScrolView as MyCoverFlowLayout).ScrollEnabled = value;
}
}
int pageIndex;
///
/// 设置或者获取当前的界面索引
///
/// The index of the page.
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);
System.Console.WriteLine($"CGRect xx-------{w}");
}
System.Console.WriteLine($"pageIndex xx-------{pageIndex}");
iosUIScrolView.ScrollRectToVisible(rect, false);
if (beforePageIndex != pageIndex)
{
iosUIPageControl.CurrentPage = pageIndex;
PageChange?.Invoke(this, pageIndex);
}
}
}
///
/// 增加子控件
///
/// View.
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();
}
///
/// 重新排位置及设备内容大小
///
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);
}
///
/// 控件宽度
///
/// The width.
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;
}
}
///
/// 控件的高度
///
/// The height.
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;
}
}
///
/// 移除当前控件
///
/// View.
internal override void Remove(View view)
{
base.Remove(view);
ReLocation();
PageIndex = ChildrenCount - 1;
iosUIPageControl.Pages = ChildrenCount;
iosUIPageControl.CurrentPage = PageIndex;
}
///
/// 移除所有的控件
///
public override void RemoveAll()
{
base.RemoveAll();
ReLocation();
iosUIPageControl.Pages = 0;
PageIndex = 0;
iosUIPageControl.CurrentPage = 0;
}
///
/// 根据索引移除控件
///
/// Index.
public override void RemoveAt(int index)
{
Remove(viewList[index]);
}
}
}