如何创建弧形ios swift

时间:2016-01-26 10:30:08

标签: ios swift uibutton

我有一些带有按钮的应用程序。我想创建一个弧形按钮,如下图enter image description here

我该怎么办?我使用以下代码来实现此目的。

<?php
@include_once ("pdocon.php");

$url = 'cost.json';
$api = file_get_contents($url);
$json = json_decode($api, true);


foreach($json['prices'] as $item) {
    $itemname = $item['market_hash_name'];
    $itemcost = $item['price'];

    $stmt = $dbh->prepare("SELECT * FROM items WHERE name=?");
    $stmt->execute(array($itemname));
    $rs = $stmt->fetch(PDO::FETCH_ASSOC);
    if(!empty($rs)) {
        if(time()-$rs["lastupdate"] < 604800) die($rs["cost"]);
    }

    $stmt = $dbh->prepare("UPDATE items SET `cost` = ?,`lastupdate` = ? WHERE `name` = ?");
    $stmt->execute(array($itemcost, time(), $itemname));

    $stmt = $dbh->prepare("INSERT INTO items (`name`,`cost`,`lastupdate`) VALUES (?, ?, ?)");
    $stmt->execute(array($itemname, $itemcost, time()));


}

?>

button.layer.cornerRadius =  button.bounds.size.height

我也尝试使用宽度

button.layer.cornerRadius =  0.5*button.bounds.size.height

button.layer.cornerRadius =  button.bounds.size.width

我该怎么办?请有人帮我解决这个问题。

3 个答案:

答案 0 :(得分:2)

要创建弧形,您需要使用 UIBezierPath 请阅读apple的UIBezierPath_class文档。

他们提到了创建弧形的方法。

Creates and returns a new UIBezierPath object initialized with an arc of a circle.

enter image description here

构建路径

Appends an arc to the receiver’s path.

enter image description here

您还可以参考Core Graphics Tutorial Part 1使用示例并在浏览器中使用 arc 关键字进行搜索,您将在页面上重定向到弧形。

答案 1 :(得分:1)

今天遇到了类似的任务,我使用CGMutableShape代替UIBezierPath,因为我需要动画形状。

无论如何,为了理解形状我将形状分为6个部分(线条和曲线):

enter image description here

基础UIView在

中绘制形状
let shapeView = UIView(frame: CGRect(x: 16, y: 16, width: 100, height: 44))
shapeView.backgroundColor = UIColor.lightGray
self.view.addSubview(shapeView)

实际形状及其路径

let path = CGMutablePath()
path.move(to: CGPoint(x: 10, y: 0)) // Move path to beginning of part-1

// Draw above divided parts
path.addLine(to: CGPoint(x: 90, y: 0)) // Part-1
path.addCurve(to: CGPoint(x: 95, y: 22), control1: CGPoint(x: 93, y: 6.6), control2: CGPoint(x: 95, y: 14)) // Part-2
path.addCurve(to: CGPoint(x: 90, y: 44), control1: CGPoint(x: 95, y: 30), control2: CGPoint(x: 93, y: 37)) // Part-3
path.addLine(to: CGPoint(x: 10, y: 44))  // Part-4
path.addCurve(to: CGPoint(x: 5, y: 22), control1: CGPoint(x: 7, y: 37), control2: CGPoint(x: 5, y: 30)) // Part-5
path.addCurve(to: CGPoint(x: 10, y: 0), control1: CGPoint(x: 5, y: 14), control2: CGPoint(x: 6, y: 6.6)) // Part-6

let arcLayer = CAShapeLayer()
arcLayer.path = path
arcLayer.fillColor = UIColor.gray.cgColor

shapeView.layer.insertSublayer(arcLayer, at: 0)

答案 2 :(得分:1)

@Jack在上面的评论中发布的另一张图片并询问了帮助,这是图片:

enter image description here

我无法回答他的评论,所以请在这里粘贴我的游乐场。

let shapeView = UIView(frame: CGRect(x: 0, y: 0, width: 360, height: 60))
let shapeSize = shapeView.frame.size
shapeView.backgroundColor = UIColor.white

let path = CGMutablePath()
path.move(to: CGPoint.zero)

let curveWidthOneFourth = shapeSize.width / 4
let curveHeight = shapeSize.height * 0.2
path.addCurve(to: CGPoint(x: curveWidthOneFourth * 2, y: curveHeight), control1: .zero, control2: CGPoint(x: curveWidthOneFourth, y: curveHeight))
path.addCurve(to: CGPoint(x: shapeSize.width, y: 0), control1: CGPoint(x: curveWidthOneFourth * 3, y: curveHeight), control2: CGPoint(x: shapeSize.width, y: 0))
path.addLine(to: CGPoint(x: shapeSize.width, y: shapeSize.height))
path.addLine(to: CGPoint(x: 0, y: shapeSize.height))
path.addLine(to: CGPoint.zero)

并将我们的路径添加到视图层:

let layer = CAShapeLayer()
layer.path = path
layer.fillColor = UIColor.red.cgColor
shapeView.layer.addSublayer(layer)