tor*_*ama 11 objective-c ios uicollectionview uicollectionviewlayout swift
我正在尝试子类化,UICollectionViewLayoutAttributes
以便我可以添加额外的属性点(数组CGPoints
).
子类被设计为仅用作装饰视图的属性,因此我需要将成员设置representedElementCategory
为.Decoration
.但是representedElementCategory
readonly并且设置它的唯一方法是通过便利初始化器:
convenience init(forDecorationViewOfKind decorationViewKind: String, withIndexPath indexPath: NSIndexPath)
Run Code Online (Sandbox Code Playgroud)
看看Objective-C头文件,我看到这个便利初始化器实际上被定义为工厂方法:
+ (instancetype)layoutAttributesForDecorationViewOfKind:(NSString *)decorationViewKind withIndexPath:(NSIndexPath*)indexPath;
Run Code Online (Sandbox Code Playgroud)
我希望我的子类'initialiser看起来像:
CustomLayoutAttributes(points: [CGPoint], representedElementKind: String, withIndexPath: NSIndexPath)
Run Code Online (Sandbox Code Playgroud)
但由于子类无法调用其父母的便利初始化器,我无法看到这是如何实现的.
我是否认为将子类化的唯一方法是:
class CustomLayoutAttributes: UICollectionViewLayoutAttributes {
var points = [CGPoint]()
}
let attribs = UICollectionViewLayoutAttributes(
forDecorationViewOfKind: "x",
withIndexPath: NSIndexPath(forItem: 0, inSection: 0)
) as! CustomLayoutAttributes
attribs.points = [CGPoint(x: 1.0, y: 2.0), CGPoint(x: 4.0, y: 5.0)]
Run Code Online (Sandbox Code Playgroud)
实际上这不会起作用,因为'as!' 演员会失败....
tor*_*ama 10
事实证明,子类可以调用超类的便利初始化器,而后者又可以调用子类的初始化器.例如:
CustomLayoutAttributes(forDecorationViewOfKind: "decoration1", withIndexPath: indexPath)
这里:
init(forDecorationViewOfKind: String, withIndexPath: NSIndexPath)
没有在CustomLayoutAttributes
类中定义,但仍可用于构造子类的实例.
实施示例:
// CustomLayoutAttributes.swift
class CustomLayoutAttributes: UICollectionViewLayoutAttributes {
// Additional attribute to test our custom layout
var points = [CGPoint]()
// MARK: NSCopying
override func copyWithZone(zone: NSZone) -> AnyObject {
let copy = super.copyWithZone(zone) as! CustomLayoutAttributes
copy.points = self.points
return copy
}
override func isEqual(object: AnyObject?) -> Bool {
if let rhs = object as? CustomLayoutAttributes {
if points != rhs.points {
return false
}
return super.isEqual(object)
} else {
return false
}
}
}
// CustomLayout.swift
override func layoutAttributesForItemAtIndexPath(path: NSIndexPath) -> UICollectionViewLayoutAttributes? {
let attributes = CustomLayoutAttributes(forDecorationViewOfKind: "decoration1", withIndexPath: indexPath)
attributes.points = [...]
return attributes
}
Run Code Online (Sandbox Code Playgroud)
UICollectionViewLayoutAttributes 子类化需要一些特殊的东西。您是否在https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionViewLayoutAttributes_class/ 上检查子类化
在大多数情况下,您可以按原样使用此类。如果您想用自定义布局属性补充基本布局属性,您可以子类化并定义要存储附加布局数据的任何属性。因为布局属性对象可能会被集合视图复制,通过实现任何适合将自定义属性复制到子类的新实例的方法,确保您的子类符合 NSCopying 协议。除了定义您的子类之外,您的 UICollectionReusableView 对象还需要实现 applyLayoutAttributes: 方法,以便它们可以在布局时应用任何自定义属性。
如果您子类化并实现任何自定义布局属性,您还必须覆盖继承的 isEqual: 方法来比较您的属性值。在 iOS 7 及更高版本中,如果这些属性未更改,则集合视图不会应用布局属性。它通过使用 isEqual: 方法比较新旧属性对象来确定属性是否已更改。由于此方法的默认实现仅检查此类的现有属性,因此您必须实现自己的方法版本以比较任何其他属性。如果您的自定义属性都相等,则调用 super 并在您的实现结束时返回结果值。
归档时间: |
|
查看次数: |
5253 次 |
最近记录: |