画一个半圆形按钮iOS

时间:2015-10-23 18:19:07

标签: ios swift

我正在尝试绘制一个半圆形按钮。我在绘制半圆并将其附加到我在xcode中制作的按钮时遇到了麻烦。 xcode中的按钮具有约束,将其固定到屏幕底部并使其居中。我在视图控制器中引用该按钮,然后尝试将其覆盖为半圆,如下所示。我得到一个空白按钮。我还在按钮所在的故事板的坐标中进行了硬编码。有没有更好的方法呢?

let circlePath = UIBezierPath.init(arcCenter: CGPoint(x: 113.0, y: 434.0),
radius: 10.0, startAngle: 0.0, endAngle: CGFloat(M_PI), clockwise: true)

let circleShape = CAShapeLayer()
circleShape.path = circlePath.CGPath

sunButton.layer.mask = circleShape

3 个答案:

答案 0 :(得分:9)

问题是你的形状图层位于按钮边界之外。如果您只是将形状图层添加为按钮图层的子视图,您将看到问题:

button.layer.addSublayer(circleShape)

enter image description here

您必须调整圆弧中心点,以便圆弧位于按钮的边界内:

let circlePath = UIBezierPath.init(arcCenter: CGPointMake(button.bounds.size.width / 2, 0), radius: button.bounds.size.height, startAngle: 0.0, endAngle: CGFloat(M_PI), clockwise: true)
let circleShape = CAShapeLayer()
circleShape.path = circlePath.CGPath
button.layer.mask = circleShape

你会得到这个:

enter image description here

答案 1 :(得分:1)

快捷键5:

    // semiCircleDown
  let circlePath =  UIBezierPath(arcCenter: CGPoint(x: shapeView.bounds.size.width/2, y: 0), radius: shapeView.bounds.size.height, startAngle: 0, endAngle: .pi, clockwise: true).cgPath

        //semiCircleUp
  let circlePath =  UIBezierPath(arcCenter: CGPoint(x: shapeView.bounds.size.width/2, y: 0), radius: shapeView.bounds.size.height, startAngle: 2 * .pi, endAngle: .pi, clockwise: true).cgPath

        //quarterCircleRightDown
  let circlePath =  UIBezierPath(arcCenter: CGPoint(x: 0, y: 0), radius: shapeView.bounds.size.width, startAngle: 2 * .pi, endAngle: .pi * 3/2, clockwise: true).cgPath

        //quarterCircleLeftDown 
  let circlePath =  UIBezierPath(arcCenter: CGPoint(x: shapeView.bounds.size.width, y: 0), radius: shapeView.bounds.size.width, startAngle: .pi * 3/2, endAngle: .pi, clockwise: true).cgPath

        quarterCircleRightUp
  let circlePath = UIBezierPath(arcCenter: CGPoint(x: 0, y: shapeView.bounds.size.height), radius: shapeView.bounds.size.width, startAngle: 0, endAngle: .pi/2, clockwise: false).cgPath

        //quarterCircleLeftUp
  let circlePath = UIBezierPath(arcCenter: CGPoint(x: shapeView.bounds.size.width, y: shapeView.bounds.size.height), radius: shapeView.bounds.size.width, startAngle: .pi/2, endAngle: .pi, clockwise: false).cgPath

let shape = CAShapeLayer()
circleShape.path = circlePath.cgPath
shapeView.layer.mask = shape

答案 2 :(得分:0)

迅速5更新:

//Prepare Datatable and Add All Columns Here
            DataTable dt = new DataTable();
            DataRow row;
            DataColumn dc = new DataColumn();
            dc.DataType = System.Type.GetType("System.String");
            dc.ColumnName = "title";
            dc.ReadOnly = false;
            dc.Unique = true;
            dc.AutoIncrement = false;

            dc.DataType = System.Type.GetType("System.String");
            dc.ColumnName = "link";
            dc.ReadOnly = false;
            dc.Unique = true;
            dc.AutoIncrement = false;

            foreach (XmlNode rssNode in rssNodes)
            {
                XmlNode rssSubNode = rssNode.SelectSingleNode("title");
                string title = rssSubNode != null ? rssSubNode.InnerText : "";

                rssSubNode = rssNode.SelectSingleNode("link");
                string link = rssSubNode != null ? rssSubNode.InnerText : "";

                //Add new row and assign values to columns, no need to add columns again and again in loop which will throw exception
                row = dt.NewRow();
                //Map all the values in the columns
                row["title"] = title;
                row["link"] = link;


                //At the end just add that row in datatable
                dt.Rows.Add(row);

            }