using System;
|
using CoreGraphics;
|
using UIKit;
|
|
namespace Shared
|
{
|
public class LongPressMoveVerticalScrolViewLayout:ViewGroup
|
{
|
/// <summary>
|
/// 构造函数
|
/// </summary>
|
public LongPressMoveVerticalScrolViewLayout()
|
{
|
viewGroup = new MyUIScrolView(this);
|
viewGroup.AddGestureRecognizer(new UILongPressGestureRecognizer(sender =>
|
{
|
uiViewLongPressed(sender);
|
}));
|
realViewGroup = viewGroup;
|
(viewGroup as MyUIScrolView).Scrolled += (sender, e) =>
|
{
|
if (ScrolledEventHandler != null)
|
{
|
ScrolledEventHandler(this, new MouseEventArgs { X = (float)(sender as MyUIScrolView).ContentOffset.X, Y = (float)(sender as MyUIScrolView).ContentOffset.Y });
|
}
|
};
|
}
|
|
public override void AddChidren(View view)
|
{
|
base.AddChidren(view);
|
int maxHeight = 0;
|
foreach (View tempView in viewList)
|
{
|
if (maxHeight < tempView.Bottom)
|
{
|
maxHeight = tempView.Bottom;
|
}
|
}
|
(realViewGroup as MyUIScrolView).ContentSize = new CGSize(Width, maxHeight);
|
}
|
|
/// <summary>
|
/// 根据点击位置找出点击到的视图
|
/// </summary>
|
/// <returns>The view of point.</returns>
|
/// <param name="point">Point.</param>
|
/// <param name="uiview">Uiview.</param>
|
UIView replaceUIViewOfPoint(CGPoint point, UIView uiview)
|
{
|
//第一个是背景图
|
for (int i = 1; i < realViewGroup.Subviews.Length; i++)
|
{
|
var tempUIView = realViewGroup.Subviews[i];
|
if (tempUIView != uiview)
|
{
|
if (tempUIView.Frame.Contains(point))
|
{
|
return tempUIView;
|
}
|
}
|
}
|
return null;
|
}
|
|
/// <summary>
|
/// 对换位置的两个视图
|
/// </summary>
|
public Action<View,View> ReplaceChanged;
|
|
/// <summary>
|
/// 根据点击位置找出点击到的视图
|
/// </summary>
|
/// <returns>返回点击到的视图</returns>
|
/// <param name="point">当前点击位置</param>
|
UIView selectedUIViewOfPoint(CGPoint point)
|
{
|
foreach (UIView uiView in realViewGroup.Subviews)
|
{
|
if (uiView.GetType() != typeof(UIImageView) && uiView.Frame.Contains(point))
|
{
|
return uiView;
|
}
|
}
|
return null;
|
}
|
CGPoint originPoint;
|
bool contain;
|
UIView selectedUIView;
|
void uiViewLongPressed(UILongPressGestureRecognizer sender)
|
{
|
//长按开始
|
if (sender.State == UIGestureRecognizerState.Began)
|
{
|
//找出当前点击位置的视图
|
selectedUIView = selectedUIViewOfPoint(sender.LocationInView(sender.View));
|
if (selectedUIView == null)
|
{
|
return;
|
}
|
originPoint = selectedUIView.Center;
|
UIView.Animate(1.0f, () =>
|
{
|
selectedUIView.Transform = CGAffineTransform.MakeScale(1.1f, 1.1f);
|
selectedUIView.Alpha = 0.7f;
|
});
|
}
|
else if (sender.State == UIGestureRecognizerState.Changed)
|
{
|
if (selectedUIView == null)
|
{
|
return;
|
}
|
//移动当前视图
|
selectedUIView.Center = sender.LocationInView(sender.View);
|
//获取当前视图进入了哪个视图的区域
|
var replaceUIView = replaceUIViewOfPoint(selectedUIView.Center, selectedUIView);
|
if (replaceUIView ==null)
|
{
|
contain = false;
|
}
|
else
|
{
|
//进入区域后切换位置
|
UIView.Animate(1.0f, () =>
|
{
|
var temp = replaceUIView.Center;
|
replaceUIView.Center = originPoint;
|
selectedUIView.Center = temp;
|
originPoint = selectedUIView.Center;
|
contain = true;
|
}, () =>
|
{
|
View selectedView = null;
|
foreach (View view in viewList)
|
{
|
if (view.RealView == selectedUIView)
|
{
|
selectedView = view;
|
break;
|
}
|
}
|
View replaceView = null;
|
foreach (View view in viewList)
|
{
|
if (view.RealView == replaceUIView)
|
{
|
replaceView = view;
|
break;
|
}
|
}
|
if(ReplaceChanged!=null){
|
ReplaceChanged(selectedView, replaceView);
|
}
|
|
});
|
}
|
}
|
else if (sender.State == UIGestureRecognizerState.Ended)
|
{
|
if (selectedUIView == null)
|
{
|
return;
|
}
|
UIView.Animate(1.0f, () =>
|
{
|
selectedUIView.Transform = CGAffineTransform.MakeIdentity();
|
selectedUIView.Alpha = 1.0f;
|
//如果不需要移动位置
|
if (!contain)
|
{
|
selectedUIView.Center = originPoint;
|
}
|
});
|
}
|
}
|
|
/// <summary>
|
/// 是否允许滑动
|
/// </summary>
|
/// <value><c>true</c> if scroll enabled; otherwise, <c>false</c>.</value>
|
public bool ScrollEnabled
|
{
|
get
|
{
|
return (viewGroup as MyUIScrolView).ScrollEnabled;
|
}
|
set
|
{
|
(viewGroup as MyUIScrolView).ScrollEnabled = value;
|
}
|
}
|
|
/// <summary>
|
/// 是否正在滑动
|
/// </summary>
|
/// <value><c>true</c> if decelerating; otherwise, <c>false</c>.</value>
|
public bool Decelerating
|
{
|
get
|
{
|
return (viewGroup as MyUIScrolView).Decelerating;
|
}
|
}
|
|
/// <summary>
|
/// 滑动事件
|
/// </summary>
|
public event EventHandler<MouseEventArgs> ScrolledEventHandler;
|
}
|
}
|