using System;
using CoreGraphics;
using Foundation;
using UIKit;
namespace Shared
{
public class MusicVerticalScrolViewLayout:ViewGroup
{
///
/// 构造函数
///
public MusicVerticalScrolViewLayout()
{
viewGroup = new MyMusicVerticalScrolViewLayout(this) { };
viewGroup.AddGestureRecognizer(new MyUILongPressGestureRecognizer(sender =>
{
(sender as MyUILongPressGestureRecognizer).musicVerticalScrolViewLayout?.uiViewLongPressed(sender);
})
{ MinimumPressDuration = 1.0f,musicVerticalScrolViewLayout=this });
realViewGroup = viewGroup;
}
class MyUILongPressGestureRecognizer : UILongPressGestureRecognizer
{
[Weak] internal MusicVerticalScrolViewLayout musicVerticalScrolViewLayout;
public MyUILongPressGestureRecognizer(Action action) : base(action)
{
}
}
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 MyMusicVerticalScrolViewLayout).ContentSize = new CGSize(Width, maxHeight < realViewGroup.Frame.Height ? (realViewGroup.Frame.Height + 1) : maxHeight);
}
///
/// 根据点击位置找出点击到的视图
///
/// The view of point.
/// Point.
/// Uiview.
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;
}
///
///
///
public Action ReplaceChanged;
///
/// 长按事件
///
public Action LongPressAction;
///
/// 根据点击位置找出点击到的视图
///
/// 返回点击到的视图
/// 当前点击位置
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;
UIView selectedUIView,replaceUIView;
bool isFirst;
void uiViewLongPressed(UILongPressGestureRecognizer sender)
{
//长按开始
if (sender.State == UIGestureRecognizerState.Began)
{
//找出当前点击位置的视图
selectedUIView = selectedUIViewOfPoint(sender.LocationInView(sender.View));
if (selectedUIView == null)
{
return;
}
viewGroup.BringSubviewToFront(selectedUIView);
originPoint = selectedUIView.Center;
isFirst = false;
UIView.Animate(1.0f, () =>
{
selectedUIView.Transform = CGAffineTransform.MakeScale(0.8f, 0.8f);
selectedUIView.Alpha = 0.7f;
});
}
else if (sender.State == UIGestureRecognizerState.Changed)
{
if (selectedUIView == null)
{
return;
}
//移动当前视图
selectedUIView.Center = sender.LocationInView(sender.View);
//获取当前视图进入了哪个视图的区域
var temReplaceUIView = replaceUIViewOfPoint(selectedUIView.Center, selectedUIView);
replaceUIView = temReplaceUIView;
if (temReplaceUIView != null)
{
if (isFirst)
{
return;
}
isFirst = true;
////进入区域后切换位置
UIView.Animate(1.0f, () =>
{
temReplaceUIView.Transform = CGAffineTransform.MakeScale(1.1f, 1.1f);
}, () =>
{
temReplaceUIView.Transform = CGAffineTransform.MakeScale(1.0f, 1.0f);
});
}
}
else if (sender.State == UIGestureRecognizerState.Ended)
{
if (selectedUIView == null)
{
return;
}
View selectedView = null;
foreach (View view in viewList)
{
if (view.RealView == selectedUIView)
{
selectedView = view;
break;
}
}
if(selectedUIView!=null&&selectedUIView.Frame.Contains(originPoint)){
UIView.Animate(1.0f, () =>
{
selectedUIView.Transform = CGAffineTransform.MakeIdentity();
selectedUIView.Alpha = 1.0f;
selectedUIView.Center = originPoint;
});
return;
}
UIView.Animate(1.0f, () =>
{
selectedUIView.Transform = CGAffineTransform.MakeIdentity();
selectedUIView.Alpha = 1.0f;
selectedUIView.Center = originPoint;
});
if (replaceUIView == null)
{
if (LongPressAction != null)
{
LongPressAction(selectedView);
}
return;
}
View replaceView = null;
foreach (View view in viewList)
{
if (view.RealView == replaceUIView)
{
replaceView = view;
break;
}
}
if (ReplaceChanged != null)
{
ReplaceChanged(replaceView, selectedView);
}
}
}
///
/// 垂直方向滚动视图
///
class MyMusicVerticalScrolViewLayout : UIScrollView
{
[Weak] View view;
public MyMusicVerticalScrolViewLayout(View view)
{
this.view = view;
}
///
/// 点击开始
///
/// Touches.
/// Evt.
public override void TouchesBegan(NSSet touches, UIEvent evt)
{
view?.TouchEvent(EventActions.Down, (touches.AnyObject as UITouch).LocationInView(this));
}
///
/// 移动
///
/// Touches.
/// Evt.
public override void TouchesMoved(NSSet touches, UIEvent evt)
{
view?.TouchEvent(EventActions.Move, (touches.AnyObject as UITouch).LocationInView(this));
}
///
/// 点击弹起
///
/// Touches.
/// Evt.
public override void TouchesEnded(NSSet touches, UIEvent evt)
{
view?.TouchEvent(EventActions.Up, (touches.AnyObject as UITouch).LocationInView(this));
}
public override void TouchesCancelled(NSSet touches, UIEvent evt)
{
view?.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;
}
base.AddSubview(view);
}
}
}
}