using System;
|
using CoreGraphics;
|
using Foundation;
|
using UIKit;
|
|
namespace Shared
|
{
|
public class GradientMaskView : View
|
{
|
|
GradientButton mGradientButton
|
{
|
get
|
{
|
return uiView as GradientButton;
|
}
|
set
|
{
|
uiView = value;
|
}
|
}
|
|
|
|
public GradientMaskView()
|
{
|
mGradientButton = new GradientButton(this) { };
|
SetGradientColors(mGradientColors);
|
}
|
|
//CAGradientLayer gradient = new CAGradientLayer();
|
//void SetCAGradientLayer()
|
//{
|
|
// gradient.Colors = new CoreGraphics.CGColor[] { startUIColor.CGColor, endUIColor.CGColor };
|
// gradient.StartPoint = new CoreGraphics.CGPoint(0, 0);
|
// gradient.EndPoint = new CoreGraphics.CGPoint(0.0, 1.0);
|
// //gradient.Locations = new Foundation.NSNumber[] { 0.0, 1.0};
|
// gradient.Frame = iosButton.Bounds;
|
// iosButton.Layer.AddSublayer(gradient);
|
//}
|
|
/// <summary>
|
/// 刷新布局
|
/// </summary>
|
private void RefreshFrame()
|
{
|
(uiView as GradientButton).InitWithFrameArc(new CoreGraphics.CGRect(base.X, base.Y, base.Width, base.Height));
|
}
|
|
/// <summary>
|
/// 控件宽度
|
/// </summary>
|
public override int Width
|
{
|
get
|
{
|
return base.Width;
|
}
|
set
|
{
|
base.Width = value;
|
RefreshFrame();
|
}
|
}
|
|
|
/// <summary>
|
/// 控件的高度
|
/// </summary>
|
public override int Height
|
{
|
get
|
{
|
return base.Height;
|
}
|
set
|
{
|
base.Height = value;
|
RefreshFrame();
|
}
|
}
|
|
/// <summary>
|
/// 控件的X坐标
|
/// </summary>
|
public override int X
|
{
|
get
|
{
|
return base.X;
|
}
|
set
|
{
|
base.X = value;
|
RefreshFrame();
|
}
|
}
|
|
|
/// <summary>
|
/// 控件的X坐标
|
/// </summary>
|
public override int Y
|
{
|
get
|
{
|
return base.Y;
|
}
|
set
|
{
|
base.Y = value;
|
RefreshFrame();
|
}
|
}
|
|
|
|
|
public bool IsShow
|
{
|
get
|
{
|
return mGradientButton.IsShow;
|
|
}
|
set
|
{
|
mGradientButton.IsShow = value;
|
|
}
|
|
}
|
|
|
/// <summary>
|
/// 0%,25%,50%,80%
|
/// </summary>
|
uint[] mGradientColors = { 0x00000000, 0x40000000, 0x80000000, 0xCC000000 };
|
public uint[] GradientColors {
|
get
|
{
|
return mGradientColors;
|
|
}
|
set
|
{
|
mGradientColors = value;
|
SetGradientColors(mGradientColors);
|
|
}
|
}
|
|
void SetGradientColors(uint[] colors) {
|
if (colors == null || colors.Length == 0) return;
|
|
CGColor[] mCGColors = new CGColor[colors.Length];
|
|
for (int i = 0; i < colors.Length; i++) {
|
mCGColors[i] = HDLUtils.GetUIColorWithUint(colors[i]).CGColor;
|
}
|
mGradientButton.GradientColors = mCGColors;
|
}
|
|
public class GradientButton : UIKit.UIView
|
{
|
/// <summary>
|
/// 刷新布局
|
/// </summary>
|
public void InitWithFrameArc(CGRect mCGRect)
|
{
|
this.Frame = mCGRect;
|
}
|
|
[Weak] View view;
|
public GradientButton(View view)
|
{
|
this.view = view;
|
}
|
|
|
|
bool isShow = true;
|
public bool IsShow
|
{
|
get
|
{
|
return isShow;
|
}
|
set
|
{
|
isShow = value;
|
SetNeedsDisplay();
|
}
|
}
|
|
public override void Draw(CGRect rect)
|
{
|
base.Draw(rect);
|
|
if (isShow)
|
{
|
CGContext ctx = UIGraphics.GetCurrentContext();
|
if (ctx == null) return;
|
ctx.SaveState();
|
DrawWithGradient(ctx, rect);
|
ctx.RestoreState();
|
}
|
|
}
|
|
|
/// <summary>
|
/// 绘制渐变效果
|
/// </summary>
|
/// <param name="ctx"></param>
|
/// <param name="rect"></param>
|
void DrawWithGradient(CGContext ctx, CGRect rect)
|
{
|
// 创建一个渐变色
|
// 创建RGB色彩空间,创建这个以后,context里面用的颜色都是用RGB表示
|
CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB();
|
CGGradient gradient = new CGGradient(colorSpace, mGradientColors);
|
// 释放色彩空间
|
colorSpace.Dispose();
|
colorSpace = null;
|
// 剪裁路径
|
ctx.Clip();
|
// 用渐变色填充
|
ctx.DrawLinearGradient(gradient, new CGPoint(rect.X, rect.Top), new CGPoint(rect.X, rect.Bottom), CGGradientDrawingOptions.None);
|
// 释放渐变色
|
gradient.Dispose();
|
gradient = null;
|
|
ctx.RestoreState();
|
ctx.SaveState();
|
}
|
|
public CGColor[] mGradientColors = { UIColor.Clear.CGColor};
|
|
public CGColor[] GradientColors {
|
get
|
{
|
return mGradientColors;
|
}
|
set
|
{
|
mGradientColors = value;
|
SetNeedsDisplay();
|
}
|
}
|
|
|
|
/// <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));
|
}
|
|
|
}
|
}
|
}
|