2017-08-25 61 views
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)) 
     } 
    } 
} 
} 

回答

0

哎呀,发现我的错误,上的CollectionView和CvItem一切正常。我只是在另一个地方设置了两次图像,这是一个问题。 感谢所有浏览过该主题的人!