在 UIView 及其子类上使用不同的方法相同 属性
Same property with diferent methods on UIView and it's subclasses
所以要求在所有 UIView 子类上都有一个 属性,例如 myView.property
。对于许多子类来说,会有一些特定的功能,比如 UILabel
并且它的所有子类都只有标签特定的东西。其他元素也一样...
所以我真的需要能够做到以下几点:
MyView.property.methodSharedByAllUIViewSubclasses
MyImageView.property.someImageViewSpecificMethod
MyLabel.property.onlyLabelSpecificMethod
任何有关如何针对 Swift 中的这种情况进行设计的帮助将不胜感激。
编辑:
我想使用协议和协议扩展来实现这个...
在 swift 3 中,您可以创建如下代码片段所示的扩展,并向标准 类 添加一些额外的属性,并根据需要在多个地方使用它。
extension UIView {
@IBInspectable var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
}
}
}
上面提到的扩展将允许您将 cornerRadius 添加到任何 UIView 或其子类 继承 UIView 像 UIImageView。
以下是上述代码的截取结果:
我希望这能帮助你实现你想要的。
您可以覆盖每个子类中的现有 variable/function
class MyView: UIView {
var property: Bool {
get {
return false
} set {
}
}
func propertyFunction () {
//methodSharedByAllUIViewSubclasses
}
}
class MyImageView: MyView {
override var property: Bool {
get {
return false
} set {
}
}
override func propertyFunction () {
//someImageViewSpecificMethod
}
}
class MyLabel: MyView {
override var property: Bool {
get {
return false
} set {
}
}
override func propertyFunction () {
//onlyLabelSpecificMethod
}
}
struct Property<T> {
let property: T
init(_ obj: T) {
property = obj
}
}
protocol PropertyDSL {
associatedtype DSL
var property: Property<DSL> { get set }
}
extension PropertyDSL {
var property: Property<Self> {
get {
return Property(self)
}
set { }
}
}
extension UIView: PropertyDSL {}
extension Property where T: UIView {
func methodSharedByAllUIViewSubclasses() {
}
}
extension Property where T: UIImageView {
func someImageViewSpecificMethod() {
}
}
extension Property where T: UILabel {
func onlyLabelSpecificMethod() {
}
}
所以要求在所有 UIView 子类上都有一个 属性,例如 myView.property
。对于许多子类来说,会有一些特定的功能,比如 UILabel
并且它的所有子类都只有标签特定的东西。其他元素也一样...
所以我真的需要能够做到以下几点:
MyView.property.methodSharedByAllUIViewSubclasses
MyImageView.property.someImageViewSpecificMethod
MyLabel.property.onlyLabelSpecificMethod
任何有关如何针对 Swift 中的这种情况进行设计的帮助将不胜感激。
编辑: 我想使用协议和协议扩展来实现这个...
在 swift 3 中,您可以创建如下代码片段所示的扩展,并向标准 类 添加一些额外的属性,并根据需要在多个地方使用它。
extension UIView {
@IBInspectable var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
}
}
}
上面提到的扩展将允许您将 cornerRadius 添加到任何 UIView 或其子类 继承 UIView 像 UIImageView。
以下是上述代码的截取结果:
我希望这能帮助你实现你想要的。
您可以覆盖每个子类中的现有 variable/function
class MyView: UIView {
var property: Bool {
get {
return false
} set {
}
}
func propertyFunction () {
//methodSharedByAllUIViewSubclasses
}
}
class MyImageView: MyView {
override var property: Bool {
get {
return false
} set {
}
}
override func propertyFunction () {
//someImageViewSpecificMethod
}
}
class MyLabel: MyView {
override var property: Bool {
get {
return false
} set {
}
}
override func propertyFunction () {
//onlyLabelSpecificMethod
}
}
struct Property<T> {
let property: T
init(_ obj: T) {
property = obj
}
}
protocol PropertyDSL {
associatedtype DSL
var property: Property<DSL> { get set }
}
extension PropertyDSL {
var property: Property<Self> {
get {
return Property(self)
}
set { }
}
}
extension UIView: PropertyDSL {}
extension Property where T: UIView {
func methodSharedByAllUIViewSubclasses() {
}
}
extension Property where T: UIImageView {
func someImageViewSpecificMethod() {
}
}
extension Property where T: UILabel {
func onlyLabelSpecificMethod() {
}
}