using System;
|
using UIKit;
|
using System.IO;
|
using Foundation;
|
using CoreGraphics;
|
using Shared.IO;
|
|
namespace Shared
|
{
|
/// <summary>
|
/// 位置布局
|
/// </summary>
|
public class HorizontalSeekBar : View
|
{
|
MySlide currentRealView;
|
/// <summary>
|
/// 构造函数
|
/// </summary>
|
public HorizontalSeekBar()
|
{
|
currentRealView = new MySlide(this) { };
|
|
uiView = currentRealView;
|
}
|
|
/// <summary>
|
/// 进度变化事件
|
/// </summary>
|
public Action<View, int> ProgressChanged;
|
|
|
int max = 100;
|
/// <summary>
|
/// 最大值
|
/// </summary>
|
/// <value>The max.</value>
|
public int Max
|
{
|
get
|
{
|
return max;
|
}
|
set
|
{
|
max = value;
|
if (!IsCanRefresh)
|
{
|
return;
|
}
|
currentRealView.MaxValue = value;
|
}
|
}
|
bool isCanScrolled = true;
|
/// <summary>
|
/// 是否允许滑动滑块
|
/// </summary>
|
/// <value><c>true</c> if is can scrolled; otherwise, <c>false</c>.</value>
|
public bool IsCanScrolled
|
{
|
get
|
{
|
return isCanScrolled;
|
}
|
set
|
{
|
isCanScrolled = value;
|
if (!IsCanRefresh)
|
{
|
return;
|
}
|
currentRealView.UserInteractionEnabled = value;
|
if (value)
|
{
|
//currentRealView.ThumbTintColor = UIColor.White;
|
}
|
else
|
{
|
//currentRealView.ThumbTintColor = UIColor.Clear;
|
}
|
}
|
}
|
|
//2019-08-29 改100毫秒
|
public int DelayTime = 100;
|
//防止跳动,延时显示状态
|
DateTime delayDateTime = DateTime.MinValue;
|
int progress;
|
/// <summary>
|
/// 当前进度
|
/// </summary>
|
/// <value>The progress.</value>
|
public int Progress
|
{
|
get
|
{
|
return (int)currentRealView.Value;
|
}
|
set
|
{
|
progress = value;
|
if (!IsCanRefresh)
|
{
|
return;
|
}
|
//发送命令后3000毫秒内不更新状态,防止跳动
|
if ((DateTime.Now - delayDateTime).TotalMilliseconds < DelayTime)
|
{
|
return;
|
}
|
currentRealView.Value = value;
|
}
|
}
|
|
public int SleepTime = 0;
|
uint progressColor = 0xFFEB642C;
|
/// <summary>
|
/// 进度颜色
|
/// </summary>
|
/// <value>The color of the progress.</value>
|
public uint ProgressColor
|
{
|
get
|
{
|
return progressColor;
|
}
|
set
|
{
|
progressColor = value;
|
int r = (byte)(progressColor / 256 / 256 % 256);
|
int g = (byte)(progressColor / 256 % 256);
|
int b = (byte)(progressColor % 256);
|
int a = (byte)(progressColor / 256 / 256 / 256 % 256);
|
currentRealView.MinimumTrackTintColor = UIKit.UIColor.FromRGBA(r, g, b, a);
|
}
|
}
|
|
uint backgroundColor = 0xFFFFFFFF;
|
/// <summary>
|
/// 底部颜色
|
/// </summary>
|
/// <value>The color of the background.</value>
|
public new uint BackgroundColor
|
{
|
get
|
{
|
return backgroundColor;
|
}
|
set
|
{
|
backgroundColor = value;
|
int r = (byte)(backgroundColor / 256 / 256 % 256);
|
int g = (byte)(backgroundColor / 256 % 256);
|
int b = (byte)(backgroundColor % 256);
|
int a = (byte)(backgroundColor / 256 / 256 / 256 % 256);
|
currentRealView.MaximumTrackTintColor = UIKit.UIColor.FromRGBA(r, g, b, a);
|
}
|
}
|
|
uint thumbColor = 0xFFFFFFFF;
|
/// <summary>
|
/// 滑块颜色
|
/// </summary>
|
/// <value>The color of the background.</value>
|
public uint ThumbColor
|
{
|
get
|
{
|
return thumbColor;
|
}
|
set
|
{
|
thumbColor = value;
|
int r = (byte)(thumbColor / 256 / 256 % 256);
|
int g = (byte)(thumbColor / 256 % 256);
|
int b = (byte)(thumbColor % 256);
|
int a = (byte)(thumbColor / 256 / 256 / 256 % 256);
|
currentRealView.SetThumbImage(createImageWithColor(UIColor.FromRGBA(r, g, b, a)), UIControlState.Normal);
|
}
|
}
|
|
|
/// <summary>
|
/// 是否可以滑动
|
/// </summary>
|
public bool IsCanMove = true;
|
public DateTime dateTime = DateTime.MinValue;
|
internal override bool TouchEvent(EventActions eventAction, CGPoint point)
|
{
|
if (!IsCanMove)
|
{
|
return true;
|
}
|
delayDateTime = DateTime.Now;
|
switch (eventAction)
|
{
|
case EventActions.Down:
|
dateTime = DateTime.MinValue;
|
setValue((float)point.X);
|
break;
|
case EventActions.Move:
|
if (dateTime == DateTime.MinValue)
|
{
|
dateTime = DateTime.Now;
|
}
|
setValue((float)point.X);
|
break;
|
case EventActions.Up:
|
dateTime = DateTime.MinValue;
|
setValue((float)point.X);
|
break;
|
case EventActions.Cancel:
|
dateTime = DateTime.MinValue;
|
setValue((float)point.X);
|
break;
|
}
|
return base.TouchEvent(eventAction, point);
|
}
|
float beforeValue = -1;
|
void setValue(float x)
|
{
|
if (x < 0)
|
{
|
x = 0;
|
}
|
if (Width - ThumbRadius < x)
|
{
|
x = Width - ThumbRadius;
|
}
|
|
currentRealView.Value = (x / (Width - ThumbRadius) * max);
|
|
if (beforeValue != currentRealView.Value)
|
{
|
//点击下或者弹起或者滑动时间500毫秒的时候,就触发事件
|
if (ProgressChanged != null
|
&& SleepTime < (DateTime.Now - dateTime).TotalMilliseconds)
|
{
|
beforeValue = currentRealView.Value;
|
dateTime = DateTime.MinValue;
|
delayDateTime = DateTime.Now;
|
ProgressChanged(this, (int)currentRealView.Value);
|
}
|
}
|
}
|
|
int thumbRadius = 9;// 12;
|
public int ThumbRadius
|
{
|
get
|
{
|
return thumbRadius;
|
}
|
set
|
{
|
thumbRadius = value;
|
int r = (byte)(thumbColor / 256 / 256 % 256);
|
int g = (byte)(thumbColor / 256 % 256);
|
int b = (byte)(thumbColor % 256);
|
int a = (byte)(thumbColor / 256 / 256 / 256 % 256);
|
currentRealView.SetThumbImage(createImageWithColor(UIColor.FromRGBA(r, g, b, a)), UIControlState.Normal);
|
}
|
}
|
|
/// <summary>
|
/// 刷新当前视图
|
/// </summary>
|
public override void Refresh()
|
{
|
base.Refresh();
|
Max = max;
|
ProgressColor = progressColor;
|
Progress = progress;
|
IsCanScrolled = isCanScrolled;
|
ThumbRadius = thumbRadius;
|
}
|
|
UIImage createImageWithColor(UIColor color)
|
{
|
//设置长宽
|
CGRect rect = new CGRect(0.0f, 0.0f, ThumbRadius * 2, ThumbRadius * 2);
|
UIGraphics.BeginImageContext(rect.Size);
|
var context = UIGraphics.GetCurrentContext();
|
context.SetFillColor(color.CGColor);
|
context.FillRect(rect);
|
UIImage resultImage = UIGraphics.GetImageFromCurrentImageContext();
|
UIGraphics.EndImageContext();
|
// 开始一个Image的上下文
|
UIGraphics.BeginImageContextWithOptions(resultImage.Size, false, 0.0f);
|
// 添加圆角
|
UIBezierPath.FromRoundedRect(rect, ThumbRadius).AddClip();
|
// 绘制图片
|
resultImage.Draw(rect);
|
// 接受绘制成功的图片
|
resultImage = UIGraphics.GetImageFromCurrentImageContext();
|
UIGraphics.EndImageContext();
|
return resultImage;
|
}
|
|
class MySlide : UISlider
|
{
|
[Weak] View view;
|
public MySlide(View view) {
|
this.view = view;
|
}
|
/// <summary>
|
/// 点击开始
|
/// </summary>
|
/// <param name="touches">Touches.</param>
|
/// <param name="evt">Evt.</param>
|
public override void TouchesBegan(NSSet touches, UIEvent evt)
|
{
|
view?.TouchEvent(EventActions.Down, (touches.AnyObject as UITouch).LocationInView(this));
|
}
|
/// <summary>
|
/// 移动
|
/// </summary>
|
/// <param name="touches">Touches.</param>
|
/// <param name="evt">Evt.</param>
|
public override void TouchesMoved(NSSet touches, UIEvent evt)
|
{
|
view?.TouchEvent(EventActions.Move, (touches.AnyObject as UITouch).LocationInView(this));
|
}
|
|
/// <summary>
|
/// 点击弹起
|
/// </summary>
|
/// <param name="touches">Touches.</param>
|
/// <param name="evt">Evt.</param>
|
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));
|
}
|
}
|
}
|
}
|