using System;
using UIKit;
using Shared.IO;
using CoreGraphics;
using Foundation;
namespace Shared
{
///
/// 位置布局
///
public class HorizontalPages : ViewGroup
{
readonly MyHorizontalPages iosUIScrolView;
//readonly UIPageControl iosUIPageControl;
///
/// 页面变化事件
///
public Action PageChange;
///
/// 垂直方向滚动视图
///
class MyHorizontalPages : UIScrollView
{
[Weak] HorizontalPages HorizontalPages;
//nfloat mLastX, mLastY, deltaX;
public MyHorizontalPages(HorizontalPages HorizontalPages)
{
this.HorizontalPages = HorizontalPages;
//this.ScrollEnabled = false;
DecelerationStarted += (s, e) =>
{
var tempUIScrolView = s as MyHorizontalPages;
//获取当前界面的索引
//System.Console.WriteLine($"DecelerationStarted-------{tempUIScrolView.ContentOffset.X}");
var tempPageIndex = Convert.ToInt32(tempUIScrolView.ContentOffset.X / (tempUIScrolView.Frame.Width - 2 * this.HorizontalPages._RowPadding + this.HorizontalPages._PagePadding));
this.HorizontalPages.PageIndex = tempPageIndex;
};
DraggingEnded += (s, e) =>
{
var tempUIScrolView = s as MyHorizontalPages;
if (!e.Decelerate)
{
//System.Console.WriteLine($"DraggingEnded-------{tempUIScrolView.ContentOffset.X}");
var tempPageIndex = Convert.ToInt32(tempUIScrolView.ContentOffset.X / (tempUIScrolView.Frame.Width - 2 * this.HorizontalPages._RowPadding + this.HorizontalPages._PagePadding));
this.HorizontalPages.PageIndex = tempPageIndex;
}
};
}
///
/// 点击开始
///
/// Touches.
/// Evt.
public override void TouchesBegan(NSSet touches, UIEvent evt)
{
HorizontalPages?.TouchEvent(EventActions.Down, (touches.AnyObject as UITouch).LocationInView(this));
//CGPoint point = (touches.AnyObject as UITouch).LocationInView(this);
////记录点击的最新X坐标
//mLastX = point.X;
//bMove = false;
//System.Console.WriteLine($"TouchesBegan-------{mLastX}");
}
int mTouchSlop = 20;
bool bMove;
///
/// 移动
///
/// Touches.
/// Evt.
public override void TouchesMoved(NSSet touches, UIEvent evt)
{
HorizontalPages?.TouchEvent(EventActions.Move, (touches.AnyObject as UITouch).LocationInView(this));
//CGPoint point = (touches.AnyObject as UITouch).LocationInView(this);
//deltaX = point.X - mLastX;
//if (Math.Abs(deltaX) >= mTouchSlop) {
// bMove = true;
//}
//System.Console.WriteLine($"deltaX-------{deltaX}");
}
///
/// 点击弹起
///
/// Touches.
/// Evt.
public override void TouchesEnded(NSSet touches, UIEvent evt)
{
HorizontalPages?.TouchEvent(EventActions.Up, (touches.AnyObject as UITouch).LocationInView(this));
//if (bMove)
//{
// if (deltaX < 0)//左滑
// {
// this.HorizontalPages.PageIndex--;
// }
// else
// {
// this.HorizontalPages.PageIndex++;
// }
//}
}
public override void TouchesCancelled(NSSet touches, UIEvent evt)
{
HorizontalPages?.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 HorizontalPages()
{
viewGroup = new UIView();
iosUIScrolView = new MyHorizontalPages(this) { };
realViewGroup = iosUIScrolView;
//启动翻页功能
//iosUIScrolView.PagingEnabled = true;
iosUIScrolView.ShowsHorizontalScrollIndicator = false;
viewGroup.AddSubview(iosUIScrolView);
_PagePadding = Application.GetRealWidth(50);
_RowPadding = Application.GetRealWidth(100);
}
//两个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 scroll enabled; otherwise, false.
public bool ScrollEnabled
{
get
{
return (iosUIScrolView as MyHorizontalPages).ScrollEnabled;
}
set
{
(iosUIScrolView as MyHorizontalPages).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)
{
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);
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;
}
}
///
/// 控件的高度
///
/// The height.
public override int Height
{
get
{
return base.Height;
}
set
{
base.Height = value;
if (!IsCanRefresh)
return;
}
}
///
/// 移除当前控件
///
/// View.
internal override void Remove(View view)
{
base.Remove(view);
ReLocation();
if (PageIndex > ChildrenCount - 1)
{
PageIndex = ChildrenCount - 1;
}
}
///
/// 移除所有的控件
///
public override void RemoveAll()
{
base.RemoveAll();
ReLocation();
PageIndex = 0;
}
///
/// 根据索引移除控件
///
/// Index.
public override void RemoveAt(int index)
{
if (viewList.Count - 1 < index || index < 0)
{
return;
}
Remove(viewList[index]);
}
}
}