首页 > 技术文章 > xamarin.ios 半圆角按钮Readerer

zuimengaitianya 2017-07-12 16:25 原文

      xamarin.from上可以使用本身的button实现圆角带图标的按钮,但是没有半圆角的按钮实现,需要自己使用Renderer重新写过来重写一个button。

    下面是一个重写的带边框的方式,代码如下:

 1 using UIKit;
 2 using Xamarin.Forms.Platform.iOS;
 3 using Xamarin.Forms;
 4 using CoreAnimation;
 5 using System.ComponentModel;
 6 
 7 [assembly: ExportRenderer(typeof(Test.Renderers.MyRadiusButton), typeof(Test.iOS.Renderers.MyRadiusButton))]
 8 namespace Test.iOS.Renderers
 9 {
10     public class MyRadiusButton : ButtonRenderer
11     {
12         bool btnStatus = true;
13         bool radiusLeft = true;
14         protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
15         {
16             base.OnElementChanged(e);
17             Test.Renderers.MyRadiusButton view = Element as Test.Renderers.MyRadiusButton;
18             radiusLeft = view.RadiusLeft;
19             if (e.OldElement == null)
20             {
21                 UIBezierPath maskPath;
22 
23 
24                 double widthLength = 600 / 2 - 10;
25                 maskPath = UIBezierPath.FromRoundedRect(new CoreGraphics.CGRect(0, 0, widthLength, 35), UIRectCorner.TopLeft | UIRectCorner.BottomLeft, new CoreGraphics.CGSize(18, 18));
26 
27                 CAShapeLayer maskLayer = new CAShapeLayer();
28                 //maskLayer.Frame = new CoreGraphics.CGRect(0, 0, widthLength, 35);
29                 maskLayer.Path = maskPath.CGPath;
30                 maskLayer.StrokeColor = new CoreGraphics.CGColor(0, 0, 0);//边框颜色
31 
32                 CAShapeLayer borderLayer = new CAShapeLayer();
33                 borderLayer.Path = maskPath.CGPath;
34                 borderLayer.Frame = new CoreGraphics.CGRect(0, 0, widthLength, 35);
35                 borderLayer.FillColor = new CoreGraphics.CGColor(1, 1, 1, 0f);
36                 borderLayer.StrokeColor = new CoreGraphics.CGColor(0.23f, 0.72f, 0.47f, 1.0f);
37                 borderLayer.LineWidth = 1;
38 
39                 Control.Layer.Mask = maskLayer;
40                 Control.Layer.AddSublayer(borderLayer);
41                 Control.Layer.MasksToBounds = true;
42                 Control.SetTitleColor(UIColor.FromRGB(255, 255, 255), UIControlState.Normal);
43                 Control.BackgroundColor = UIColor.FromRGB(59, 183, 120);
44 
45                 view.Clicked += (sender, even) =>
46                 {
47                     if (!view.BtnSelected)
48                     {
49                         if (btnStatus)
50                         {
51                             Control.SetTitleColor(UIColor.FromRGB(59, 183, 120), UIControlState.Normal);
52                             Control.BackgroundColor = UIColor.FromRGB(222, 222, 222);
53                             btnStatus = !btnStatus;
54                         }
55                         else
56                         {
57                             Control.SetTitleColor(UIColor.FromRGB(255, 255, 255), UIControlState.Normal);
58                             Control.BackgroundColor = UIColor.FromRGB(59, 183, 120);
59                             btnStatus = !btnStatus;
60                         }
61                         view.BtnClickAction?.Invoke();
62                     }
63                 };
64                 
65             }
66         }
67     }
68 }

 

推荐阅读