NSCollectionView,imageview轮换问题

时间:2017-08-25 10:11:26

标签: swift nscollectionview nscollectionviewitem

我有NSCollectionView里面的项目。 我有NSCollectionViewItem的xib,绑定到representObject.image

我的问题如下。有时我的项目应显示“空图像”,直到我通过我的控件选择图像。当我选择图像时 - 我将它存储在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))
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

哎呀,发现我的bug,在CollectionView和CvItem上都可以。我只是在另一个地方设置了两次图像,这是一个问题。 感谢所有观看过该主题的人!