using System;
using UIKit;
using Shared.IO;
using CoreGraphics;
using Foundation;
using CoreAnimation;
namespace Shared
{
///
/// 位置布局
///
public class HorizontalPages : ViewGroup
{
readonly MyHorizontalPages iosUIScrolView;
//readonly UIPageControl iosUIPageControl;
///
/// 页面变化事件
///
public Action PageChange;
///
/// 垂直方向滚动视图
///
class MyHorizontalPages : UIScrollView
{
nfloat beforeXScrollX;
nfloat moveX;
[Weak] HorizontalPages HorizontalPages;
//nfloat mLastX, mLastY, deltaX;
public MyHorizontalPages(HorizontalPages HorizontalPages)
{
this.HorizontalPages = HorizontalPages;
//this.ScrollEnabled = false;
Scrolled += (s, e) =>
{
moveX = this.ContentOffset.X - beforeXScrollX;
};
DraggingStarted += (sender, e) =>
{
beforeXScrollX = this.ContentOffset.X;
};
DecelerationStarted += (s, e) =>
{
//var tempUIScrolView = s as MyHorizontalPages;
////获取当前界面的索引
////System.Console.WriteLine($"DecelerationStarted-------{tempUIScrolView.ContentOffset.X}");
//var tempPageIndex = Convert.ToInt32(tempUIScrolView.ContentOffset.X / (this.HorizontalPages.mScrollWidth));
//this.HorizontalPages.PageIndex = tempPageIndex;
GetMovePageIndex();
};
//DecelerationEnded += (s, e) =>
//{
// Console.WriteLine("HorizontalPages DecelerationEnded");
//};
DraggingEnded += (s, e) =>
{
var tempUIScrolView = s as MyHorizontalPages;
if (!e.Decelerate)
{
GetMovePageIndex();
////System.Console.WriteLine($"DraggingEnded-------{tempUIScrolView.ContentOffset.X}");
//var tempPageIndex = Convert.ToInt32(tempUIScrolView.ContentOffset.X / (this.HorizontalPages.mScrollWidth));
//this.HorizontalPages.PageIndex = tempPageIndex;
}
};
}
private void GetMovePageIndex() {
if (mTouchSlop < Math.Abs(moveX))
{
if (moveX < 0)
{
this.HorizontalPages.PageIndex -= 1;
}
else if (moveX > 0)
{
this.HorizontalPages.PageIndex += 1;
}
}
else
{
this.HorizontalPages.PageIndex = this.HorizontalPages.PageIndex;
}
}
///
/// 点击开始
///
/// 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);
_TCBJ = Application.GetRealWidth(50);
}
// //两个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();
// }
//}
///
/// 突出宽度
///
//public int JMBJ
//两个Page界面之间的距离
int _PagePadding;
///
/// 界面之间的边距
///
public int JMBJ
{
get
{
return _PagePadding;
}
set
{
_PagePadding = value;
_RowPadding = _TCBJ + _PagePadding;
RefreshScrollWidth();
ReLocation();
}
}
//page的与手机屏幕的边距 = 突出宽度 + 两个Page之间的距离
int _RowPadding;
///
/// 突出宽度
///
int _TCBJ;
public int TCBJ
{
get
{
return _TCBJ;
}
set
{
_TCBJ = value;
_RowPadding = _TCBJ + _PagePadding;
RefreshScrollWidth();
ReLocation();
}
}
int mScrollWidth;
int mPageWidth;
private void RefreshScrollWidth()
{
mScrollWidth = (int)this.RealView.Frame.Width - 2 * _RowPadding + _PagePadding;
mPageWidth = (int)this.RealView.Frame.Width - 2 * _RowPadding;
}
private void RefreshPageView() {
//RefreshView();
RefreshViewWithTransform3D();
}
//********************Transform3D 旋转效果方法**********************
//默认25度
nfloat transformAngle = (nfloat)(25 * Math.PI / 180.0);
//旋转角度
public int TransformAngle
{
set
{
transformAngle = (nfloat)(value * Math.PI / 180.0);
}
}
//
public void RefreshViewWithTransform3D()
{
if (iosUIScrolView.Subviews.Length <= 0) return;
if (PageIndex > iosUIScrolView.Subviews.Length - 1)
{
PageIndex = iosUIScrolView.Subviews.Length - 1;
}
// - 单位矩阵
CATransform3D transform = CATransform3D.Identity;
// - 增加透视效果
transform.m34 = (nfloat)(-1.0 / 500);
var mRotate = transform.Rotate(0, 0f, 1f, 0f);
iosUIScrolView.Subviews[PageIndex].Layer.Transform = mRotate;
//判断PageIndex 前后是否有view,并旋转
if (PageIndex - 1 >= 0)
{
var mRotate1 = transform.Rotate(transformAngle, 0f, 1f, 0f);
iosUIScrolView.Subviews[PageIndex - 1].Layer.Transform = mRotate1;
}
if (PageIndex <= iosUIScrolView.Subviews.Length - 2)
{
var mRotate2 = transform.Rotate(-1 * transformAngle, 0f, 1f, 0f);
iosUIScrolView.Subviews[PageIndex + 1].Layer.Transform = mRotate2;
}
}
int PaddingHeight = 20;
//********************普通高度改变方法**********************
private void RefreshView()
{
if (iosUIScrolView.Subviews.Length <= 0) return;
if (PageIndex > iosUIScrolView.Subviews.Length - 1)
{
PageIndex = iosUIScrolView.Subviews.Length - 1;
}
//System.Console.WriteLine($" tempPageIndex:{PageIndex}");
if (PageIndex == 0)
{
var frame0 = iosUIScrolView.Subviews[0].Frame;
frame0.Height = iosUIScrolView.Frame.Height;
frame0.Y = 0;
iosUIScrolView.Subviews[0].Frame = frame0;
if (iosUIScrolView.Subviews.Length > 1)
{
var frame1 = iosUIScrolView.Subviews[1].Frame;
frame1.Height = iosUIScrolView.Frame.Height - PaddingHeight;
frame1.Y = PaddingHeight/2;
iosUIScrolView.Subviews[1].Frame = frame1;
}
}
else
{
var frame0 = iosUIScrolView.Subviews[PageIndex].Frame;
frame0.Height = iosUIScrolView.Frame.Height;
frame0.Y = 0;
iosUIScrolView.Subviews[PageIndex].Frame = frame0;
var frame = iosUIScrolView.Subviews[PageIndex - 1].Frame;
frame.Height = iosUIScrolView.Frame.Height - PaddingHeight;
frame.Y = PaddingHeight/2;
iosUIScrolView.Subviews[PageIndex - 1].Frame = frame;
if (PageIndex <= iosUIScrolView.Subviews.Length - 2)
{
var frame1 = iosUIScrolView.Subviews[PageIndex + 1].Frame;
frame1.Height = iosUIScrolView.Frame.Height - PaddingHeight;
frame1.Y = PaddingHeight/2;
iosUIScrolView.Subviews[PageIndex + 1].Frame = frame1;
}
}
}
///
/// 是否允许滑动
///
/// 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;
RefreshPageView();
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}");
//Console.WriteLine("HorizontalPages ScrollRectToVisible");
iosUIScrolView.ScrollRectToVisible(rect, true);
if (beforePageIndex != pageIndex)
{
//Console.WriteLine("HorizontalPages DelayPageChange");
DelayPageChange();
//PageChange?.Invoke(this, pageIndex);
}
}
}
public void DelayPageChange()
{
new System.Threading.Thread(async () =>
{
System.Threading.Thread.Sleep(500);
Application.RunOnMainThread(() =>
{
PageChange?.Invoke(this, pageIndex);
});
})
{ IsBackground = true }.Start();
}
///
/// 增加子控件
///
/// 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;
frame0.Y = 0;
frame0.Width = mPageWidth;
frame0.Height = iosUIScrolView.Frame.Height;
iosUIScrolView.Subviews[0].Frame = frame0;
for (int i = 1; i < iosUIScrolView.Subviews.Length; i++)
{
var frame = iosUIScrolView.Subviews[i].Frame;
frame.Width = mPageWidth;
frame.Height = iosUIScrolView.Frame.Height;
frame.Y = 0;
//frame.X = iosUIScrolView.Subviews[i - 1].Frame.Right + _PagePadding;
frame.X = _RowPadding + (mPageWidth + _PagePadding)*i;
iosUIScrolView.Subviews[i].Frame = frame;
}
iosUIScrolView.ContentSize = new CoreGraphics.CGSize(iosUIScrolView.Subviews[iosUIScrolView.Subviews.Length - 1].Frame.Right+_RowPadding, iosUIScrolView.Frame.Height);
RefreshPageView();
}
///
/// 控件宽度
///
/// 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]);
}
}
}