是否可以根据条件决定要在对象中定义哪个属性?例如:
props="{
'prop1': {label: 'Prop1'},
hasProp2 ? '(prop2': {label: 'Prop2'}) : ('prop3': {label: 'Prop3'}),
'prop4': {label: 'Prop4'}
}"
假设hasProp2
是computed property
,其函数返回true
或false
。如何在对象内部使用ternary operator
或基本if statement
,甚至有可能?
答案 0 :(得分:1)
您可以非常简单地对其进行测试
class DrawingImageView: UIImageView {
var points: [CGPoint]?
var path: UIBezierPath?
var pathLayer: CAShapeLayer!
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
pathLayer = CAShapeLayer()
pathLayer.fillColor = UIColor.clear.cgColor
pathLayer.strokeColor = UIColor.clear.withAlphaComponent(0.5).cgColor
pathLayer.lineWidth = 15
pathLayer.lineJoin = .round
pathLayer.lineCap = .round
pathLayer.allowsEdgeAntialiasing = true
pathLayer.allowsGroupOpacity = true
pathLayer.shadowColor = UIColor.black.cgColor
pathLayer.shadowOpacity = 1
pathLayer.shadowOffset = CGSize.zero
pathLayer.shadowRadius = 5
self.layer.addSublayer(pathLayer)
if let touch = touches.first {
points = [touch.location(in: self)]
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
if let coalescedTouches = event?.coalescedTouches(for: touch) {
points? += coalescedTouches.map { $0.location(in: self) }
} else {
points?.append(touch.location(in: self))
}
if let predictedTouches = event?.predictedTouches(for: touch) {
let predictedPoints = predictedTouches.map { $0.location(in: self) }
pathLayer.path = UIBezierPath.interpolateHermiteFor(points: points! + predictedPoints, closed: false).cgPath
} else {
pathLayer.path = UIBezierPath.interpolateHermiteFor(points: points!, closed: false).cgPath
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
pathLayer.path = UIBezierPath.interpolateHermiteFor(points: points!, closed: false).cgPath
points?.removeAll()
}
}
如您所见,这是有可能的。 Vue是反应性的,也将起作用。