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);
//}
///
/// 刷新布局
///
private void RefreshFrame()
{
(uiView as GradientButton).InitWithFrameArc(new CoreGraphics.CGRect(base.X, base.Y, base.Width, base.Height));
}
///
/// 控件宽度
///
public override int Width
{
get
{
return base.Width;
}
set
{
base.Width = value;
RefreshFrame();
}
}
///
/// 控件的高度
///
public override int Height
{
get
{
return base.Height;
}
set
{
base.Height = value;
RefreshFrame();
}
}
///
/// 控件的X坐标
///
public override int X
{
get
{
return base.X;
}
set
{
base.X = value;
RefreshFrame();
}
}
///
/// 控件的X坐标
///
public override int Y
{
get
{
return base.Y;
}
set
{
base.Y = value;
RefreshFrame();
}
}
public bool IsShow
{
get
{
return mGradientButton.IsShow;
}
set
{
mGradientButton.IsShow = value;
}
}
///
/// 0%,25%,50%,80%
///
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
{
///
/// 刷新布局
///
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();
}
}
///
/// 绘制渐变效果
///
///
///
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();
}
}
///
/// 点击开始
///
/// 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));
}
}
}
}