フレームサイズに合わせて拡大縮小したいボタンがあります。ただし、基になる CALayer パスを変更しても更新されません。関数では、「updateLayerProperites
関数を使用してパスを変更します。また、プロパティを直接設定しようとしましたが、それも機能しません。図面を拡大縮小するにはどうすればよいですか?CALayer
setValue
path
kiteShape.path = kitePath.CGPath
import UIKit
@IBDesignable
class ElementButton: UIButton {
var leftKiteShape: CAShapeLayer!
var kiteShape: CAShapeLayer!
var rightKiteShape: CAShapeLayer!
override init(frame: CGRect) {
super.init(frame: frame)
createLayers()
updateLayerProperties()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
createLayers()
updateLayerProperties()
}
@IBInspectable
var lineWidth: CGFloat = 1 {
didSet {
updateLayerProperties()
}
}
private func createLayers() {
if kiteShape == nil {
//// Kite Drawing
let kitePath = UIBezierPath()
kitePath.moveToPoint(CGPointMake(frame.minX + 48, frame.minY + 5))
kitePath.addLineToPoint(CGPointMake(frame.minX + 28, frame.minY + 68))
kitePath.addLineToPoint(CGPointMake(frame.minX + 48, frame.minY + 93))
kitePath.addLineToPoint(CGPointMake(frame.minX + 68, frame.minY + 68))
kitePath.addLineToPoint(CGPointMake(frame.minX + 48, frame.minY + 5))
kitePath.closePath()
kiteShape = CAShapeLayer()
kiteShape.path = kitePath.CGPath
kiteShape.strokeColor = UIColor.blackColor().CGColor
kiteShape.lineWidth = lineWidth
kiteShape.opacity = 0.5
self.layer.addSublayer(kiteShape)
}
if leftKiteShape == nil {
//// LeftKite Drawing
let leftKitePath = UIBezierPath()
leftKitePath.moveToPoint(CGPointMake(frame.minX + 46, frame.minY + 4.25))
leftKitePath.addLineToPoint(CGPointMake(frame.minX + 24, frame.minY + 68))
leftKitePath.addLineToPoint(CGPointMake(frame.minX + 45, frame.minY + 92.75))
leftKitePath.addLineToPoint(CGPointMake(frame.minX + 6, frame.minY + 68))
leftKitePath.addLineToPoint(CGPointMake(frame.minX + 46, frame.minY + 4.25))
leftKitePath.closePath()
leftKiteShape = CAShapeLayer()
leftKiteShape.path = leftKitePath.CGPath
leftKiteShape.strokeColor = UIColor.blackColor().CGColor
leftKiteShape.lineWidth = lineWidth
leftKiteShape.opacity = 0.5
self.layer.addSublayer(leftKiteShape)
}
if rightKiteShape == nil {
//// RightKite Drawing
let rightKitePath = UIBezierPath()
rightKitePath.moveToPoint(CGPointMake(frame.minX + 50, frame.minY + 4.25))
rightKitePath.addLineToPoint(CGPointMake(frame.minX + 72, frame.minY + 68))
rightKitePath.addLineToPoint(CGPointMake(frame.minX + 51, frame.minY + 92.75))
rightKitePath.addLineToPoint(CGPointMake(frame.minX + 90, frame.minY + 68))
rightKitePath.addLineToPoint(CGPointMake(frame.minX + 50, frame.minY + 4.25))
rightKitePath.closePath()
rightKiteShape = CAShapeLayer()
rightKiteShape.path = rightKitePath.CGPath
rightKiteShape.strokeColor = UIColor.blackColor().CGColor
rightKiteShape.lineWidth = lineWidth
rightKiteShape.opacity = 0.5
self.layer.addSublayer(rightKiteShape)
}
}
private func updateLayerProperties() {
let kitePath = UIBezierPath()
kitePath.moveToPoint(CGPointMake(frame.minX + 48, frame.minY + 5))
kitePath.addLineToPoint(CGPointMake(frame.minX + 28, frame.minY + 68))
kitePath.addLineToPoint(CGPointMake(frame.minX + 48, frame.minY + 93))
kitePath.addLineToPoint(CGPointMake(frame.minX + 68, frame.minY + 68))
kitePath.addLineToPoint(CGPointMake(frame.minX + 48, frame.minY + 5))
kitePath.closePath()
kiteShape.setValue(kitePath.CGPath, forKey: "path")
kiteShape.setValue(lineWidth, forKey: "lineWidth")
let leftKitePath = UIBezierPath()
leftKitePath.moveToPoint(CGPointMake(frame.minX + 46, frame.minY + 4.25))
leftKitePath.addLineToPoint(CGPointMake(frame.minX + 24, frame.minY + 68))
leftKitePath.addLineToPoint(CGPointMake(frame.minX + 45, frame.minY + 92.75))
leftKitePath.addLineToPoint(CGPointMake(frame.minX + 6, frame.minY + 68))
leftKitePath.addLineToPoint(CGPointMake(frame.minX + 46, frame.minY + 4.25))
leftKitePath.closePath()
leftKiteShape.setValue(leftKitePath.CGPath, forKey: "path")
leftKiteShape.setValue(lineWidth, forKey: "lineWidth")
let rightKitePath = UIBezierPath()
rightKitePath.moveToPoint(CGPointMake(frame.minX + 50, frame.minY + 4.25))
rightKitePath.addLineToPoint(CGPointMake(frame.minX + 72, frame.minY + 68))
rightKitePath.addLineToPoint(CGPointMake(frame.minX + 51, frame.minY + 92.75))
rightKitePath.addLineToPoint(CGPointMake(frame.minX + 90, frame.minY + 68))
rightKitePath.addLineToPoint(CGPointMake(frame.minX + 50, frame.minY + 4.25))
rightKitePath.closePath()
rightKiteShape.setValue(rightKitePath.CGPath, forKey: "path")
rightKiteShape.setValue(lineWidth, forKey: "lineWidth")
}
private func disableTouch() {
self.userInteractionEnabled = false
}
private func enableTouch() {
self.userInteractionEnabled = true
}
}