我有一堆不规则形状的键盘,我想把它们放在一起。每个键都是一个单独的UIView,它们都包含在包装器键盘视图中。它们看起来像这样:
理想情况下,边框应该看起来像左下方的红色边框(在预览中模拟),并在必要时有圆角,但我认为只是让正确的形状开始。
最好的方法是什么?形状和面具?一条复杂的道路?看起来我需要CALayers,不过我对这些都很新。
非常感谢任何建议。非常感谢!
杰克
答案 0 :(得分:0)
我明白了!我最终使用CAShapeLayers
和UIBezierPaths
在键盘周围绘制边框;我用弧线来模拟圆角:
func createBezier(key1Num: Int, key2Num: Int, key3Num: Int, key4Num: Int) {
borderPath = UIBezierPath()
let key1 = self.keys[key1Num].frame
let key2 = self.keys[key2Num].frame
let key3 = self.keys[key3Num].frame
let key4 = self.keys[key4Num].frame
let arcRadius = key1.height * 1/32
let start = CGPoint(x: key1.origin.x, y: key1.origin.y)
func bothEdgeNotesBlack() {
borderPath.move(to: start)
borderPath.addLine(to: CGPoint(x: start.x, y: key1.height * 31/32))
borderPath.addArc(withCenter: CGPoint(x: start.x + arcRadius, y: key1.height * 31/32), radius: arcRadius, startAngle: leftAng, endAngle: bottomAng, clockwise: false)
borderPath.addLine(to: CGPoint(x: key2.origin.x, y: key1.height))
borderPath.addLine(to: CGPoint(x: key2.origin.x, y: key2.height - arcRadius))
borderPath.addArc(withCenter: CGPoint(x: key2.origin.x + arcRadius, y: key2.height - arcRadius), radius: arcRadius, startAngle: leftAng, endAngle: bottomAng, clockwise: false)
borderPath.addLine(to: CGPoint(x: key3.origin.x + key3.width - arcRadius, y: key2.height))
borderPath.addArc(withCenter: CGPoint(x: key3.origin.x + key3.width - arcRadius, y: key2.height - arcRadius), radius: arcRadius, startAngle: bottomAng, endAngle: rightAng, clockwise: false)
borderPath.addLine(to: CGPoint(x: key3.origin.x + key3.width, y: key4.height))
borderPath.addLine(to: CGPoint(x: key4.origin.x + key4.width - arcRadius, y: key4.height))
borderPath.addArc(withCenter: CGPoint(x: key4.origin.x + key4.width - arcRadius, y: key4.height - arcRadius), radius: arcRadius, startAngle: bottomAng, endAngle: rightAng, clockwise: false)
borderPath.addLine(to: CGPoint(x: key4.origin.x + key4.width, y: key4.origin.y))
borderPath.close()
}
func leftBlackrightWhite() {
borderPath.move(to: start)
borderPath.addLine(to: CGPoint(x: start.x, y: key1.height * 31/32))
borderPath.addArc(withCenter: CGPoint(x: start.x + arcRadius, y: key1.height * 31/32), radius: arcRadius, startAngle: leftAng, endAngle: bottomAng, clockwise: false)
borderPath.addLine(to: CGPoint(x: key2.origin.x, y: key1.height))
borderPath.addLine(to: CGPoint(x: key2.origin.x, y: key2.height - arcRadius))
borderPath.addArc(withCenter: CGPoint(x: key2.origin.x + arcRadius, y: key2.height - arcRadius), radius: arcRadius, startAngle: leftAng, endAngle: bottomAng, clockwise: false)
borderPath.addLine(to: CGPoint(x: key4.origin.x + key4.width - arcRadius, y: key4.height))
borderPath.addArc(withCenter: CGPoint(x: key4.origin.x + key4.width - arcRadius, y: key4.height - arcRadius), radius: arcRadius, startAngle: bottomAng, endAngle: rightAng, clockwise: false)
borderPath.addLine(to: CGPoint(x: key4.origin.x + key4.width, y: key4.origin.y))
borderPath.close()
}
func leftWhiteRightBlack() {
borderPath.move(to: start)
borderPath.addLine(to: CGPoint(x: start.x, y: key1.height * 31/32))
borderPath.addArc(withCenter: CGPoint(x: start.x + arcRadius, y: key1.height * 31/32), radius: arcRadius, startAngle: leftAng, endAngle: bottomAng, clockwise: false)
borderPath.addLine(to: CGPoint(x: key3.origin.x + key3.width - arcRadius, y: key3.height))
borderPath.addArc(withCenter: CGPoint(x: key3.origin.x + key3.width - arcRadius, y: key3.height - arcRadius), radius: arcRadius, startAngle: bottomAng, endAngle: rightAng, clockwise: false)
borderPath.addLine(to: CGPoint(x: key3.origin.x + key3.width, y: key4.height))
borderPath.addLine(to: CGPoint(x: key4.origin.x + key4.width - arcRadius, y: key4.height))
borderPath.addArc(withCenter: CGPoint(x: key4.origin.x + key4.width - arcRadius, y: key4.height - arcRadius), radius: arcRadius, startAngle: bottomAng, endAngle: rightAng, clockwise: false)
borderPath.addLine(to: CGPoint(x: key4.origin.x + key4.width, y: key4.origin.y))
borderPath.close()
}
func bothEdgeNotesWhite() {
borderPath.move(to: start)
borderPath.addLine(to: CGPoint(x: start.x, y: key1.height * 31/32))
borderPath.addArc(withCenter: CGPoint(x: start.x + arcRadius, y: key1.height * 31/32), radius: arcRadius, startAngle: leftAng, endAngle: bottomAng, clockwise: false)
borderPath.addLine(to: CGPoint(x: key4.origin.x + key4.width - arcRadius, y: key4.height))
borderPath.addArc(withCenter: CGPoint(x: key4.origin.x + key4.width - arcRadius, y: key4.height - arcRadius), radius: arcRadius, startAngle: bottomAng, endAngle: rightAng, clockwise: false)
borderPath.addLine(to: CGPoint(x: key4.origin.x + key4.width, y: key4.origin.y))
borderPath.close()
}
switch self.keys[key1Num].keyType {
case 2, 5, 7, 10, 12: // 1st key is black
switch self.keys[key4Num].keyType {
case 2, 5, 7, 10, 12: // last key is black
bothEdgeNotesBlack()
case 1, 3, 4, 6, 8, 9, 11:
leftBlackrightWhite() // last key is white
default:
()
}
case 1, 3, 4, 6, 8, 9, 11: // 1st key is white
switch self.keys[key4Num].keyType {
case 2, 5, 7, 10, 12: // last key is black
leftWhiteRightBlack()
case 1, 3, 4, 6, 8, 9, 11: // last key is white
bothEdgeNotesWhite()
default:
()
}
default:
()
}
}
func borderBezier(key1Num: Int, key2Num: Int, key3Num: Int, key4Num: Int) {
self.createBezier(key1Num: key1Num, key2Num: key2Num, key3Num: key3Num, key4Num: key4Num)
let borderLayer = CAShapeLayer()
borderLayer.zPosition = 4
borderLayer.path = self.borderPath.cgPath
borderLayer.fillColor = UIColor.clear.cgColor
borderLayer.strokeColor = UIColor.red.cgColor
borderLayer.lineWidth = 3.0
self.borderLayer = borderLayer
self.layer.addSublayer(self.borderLayer)
}