using System;
using UIKit;
using System.IO;
using Foundation;
using CoreGraphics;
using Shared.IO;
namespace Shared
{
///
/// 位置布局
///
public class HorizontalSeekBar : View
{
MySlide currentRealView;
///
/// 构造函数
///
public HorizontalSeekBar()
{
currentRealView = new MySlide(this) { };
uiView = currentRealView;
}
///
/// 进度变化事件
///
public Action ProgressChanged;
int max = 100;
///
/// 最大值
///
/// The max.
public int Max
{
get
{
return max;
}
set
{
max = value;
if (!IsCanRefresh)
{
return;
}
currentRealView.MaxValue = value;
}
}
bool isCanScrolled = true;
///
/// 是否允许滑动滑块
///
/// true if is can scrolled; otherwise, false.
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;
///
/// 当前进度
///
/// The progress.
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;
///
/// 进度颜色
///
/// The color of the progress.
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;
///
/// 底部颜色
///
/// The color of the background.
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;
///
/// 滑块颜色
///
/// The color of the background.
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);
}
}
///
/// 是否可以滑动
///
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);
}
}
///
/// 刷新当前视图
///
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;
}
///
/// 点击开始
///
/// 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));
}
}
}
}