swift - 如何切掉 UIButton 的角,而不是圆角?
问题描述
我想迅速切掉 UIButton 的角落。
我知道如何设置拐角半径,但我想减少拐角。
// what I DON'T want, rounded corners
myButton.layer.cornerRadius = myButton.frame.height / 2
切角适合我:
解决方案
像这样创建具有所需路径的 CAShapeLayer
let button = UIButton()
button.backgroundColor = .red
button.frame = CGRect(x: 100, y: 100, width: 200, height: 40)
view.addSubview(button)
let layer = CAShapeLayer()
layer.fillColor = UIColor.yellow.cgColor
let cutSize:CGFloat = 20
let path = UIBezierPath()
path.move(to: CGPoint(x: 0, y: button.bounds.midY))
path.addLine(to: CGPoint(x: cutSize, y: 0))
path.addLine(to: CGPoint(x: button.bounds.maxX-cutSize, y: 0))
path.addLine(to: CGPoint(x: button.bounds.maxX, y: button.bounds.midY))
path.addLine(to: CGPoint(x: button.bounds.maxX-cutSize, y: button.bounds.maxY))
path.addLine(to: CGPoint(x: cutSize, y: button.bounds.maxY))
path.addLine(to: CGPoint(x: 0, y: button.bounds.midY))
layer.path = path.cgPath
button.layer.insertSublayer(layer, at: 0)
推荐阅读
- spring-mvc - 如何在配置类中使用 RedisMessageListenerContainer
- css - 我可以对所有元素或父元素使用'/deep/'吗
- android - 删除单个 SQlite 行 RecyclerView onSwiped
- javascript - 如何检测 Web Share Target API 的可用性?
- node.js - Bcrypt密码比较不显示结果
- docker - 需要在 docker 容器中使用脚本运行服务
- llvm - 如何关闭llvm中的常量折叠优化
- mysql - 如何在某个时间获取 mysql 日志?
- arrays - 如何将包含游戏板的数组传递给在单独的 .cpp 文件中定义的类函数?
- php - 没有帖子时生成空的li元素