0
我有NSCollectionView裏面的項目。 我有xib NSCollectionViewItem,綁定到representObject.imageNSCollectionView,imageview旋轉問題
我的問題是以下。有時我的項目應該顯示「空白圖像」,直到我通過我的控制選擇圖像。當我選擇圖像 - 我將它存儲在representObject的.image屬性中,綁定更新視圖,一切都很酷。
我可以在集合中旋轉我的imageview。所有的工作都很酷,除了上面的情況,當我選擇圖像爲空時,視圖正在更新,綁定顯示正確的圖像,但如果我嘗試旋轉視圖後 - 它不能正常工作。它只在旋轉的地方顯示出來,但如果我旋轉到不同的角度 - 它會消失。 KVO正在觀察旋轉。
這是代碼我有:
class CollectionViewItem: NSCollectionViewItem {
//Init
override init?(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
self.addObserver(self, forKeyPath: "modelRepresentation.Rotation", options: [.new, .old], context: nil)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
//Properties
@objc dynamic var modelRepresentation : Artwork?
override var highlightState: NSCollectionViewItemHighlightState {
get {
return super.highlightState
}
set(newHighlightState) {
super.highlightState = newHighlightState
// Relay the newHighlightState to our AAPLSlideCarrierView.
guard let carrierView = self.view as? LibraryViewItemCarrierView else {return}
carrierView.highlightState = newHighlightState
}
}
override var isSelected: Bool {
get {
return super.isSelected
}
set {
super.isSelected = newValue
guard let carrierView = self.view as? LibraryViewItemCarrierView else {return}
carrierView.selected = newValue
}
}
override var representedObject: Any? {
get {
return super.representedObject as AnyObject?
}
set(newRepresentedObject) {
super.representedObject = newRepresentedObject
if let model = newRepresentedObject as? Artwork {
modelRepresentation = model
imageView?.rotate(byDegrees: CGFloat((model.Rotation)))
}
}
}
// 2
override func viewDidLoad() {
super.viewDidLoad()
view.wantsLayer = true
view.layer?.backgroundColor = NSColor(red: 15/255, green: 34/255, blue: 42/255, alpha: 1).cgColor
view.layer?.borderWidth = 0.0
view.layer?.borderColor = NSColor.lightGray.cgColor
}
override func prepareForReuse() {
super.prepareForReuse()
imageView?.boundsRotation = 0
}
func setHighlight(_ selected: Bool) {
view.layer?.borderWidth = selected ? 3.0 : 0.0
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
let kind = change![NSKeyValueChangeKey.kindKey]! as! UInt
if(keyPath == "modelRepresentation.Rotation")
{
if kind == NSKeyValueChange.setting.rawValue {
guard let newVal = change![.newKey]! as? Int,
let oldVal = change![.oldKey]! as? Int else {return}
let delta = newVal - oldVal
imageView?.rotate(byDegrees: CGFloat(delta))
}
}
}
}