NikoDiYana
NikoDiYana

Reputation: 31

Why are NSView frames ignored when set just before an animation block?

I have the following unclear behaviour in MacOS:

When setting multiple NSView frames, and animating them as a next step, the animation block ignores the last set frames.

The host view is a NSSCrollView.documentView.

Here the code:

override func resizeSubviews(withOldSize oldSize: NSSize) {
   customLayout()
}

func customLayout() {
  let offset = 200

  // Shift all views after the second one horizontally with offset:
  scrollView!.documentView!.subviews[2...].forEach {
      view in
      let newOrigin = CGPoint(x: view.frame.origin.x + offset, y: view.frame.origin.y)
      view.setFrameOrigin(newOrigin)
  }

  // Animate the views from the frames set in the above loop to the new origins and new size
  // newOrigins[CGPoint] and newSize:CGSize are generated from another function

  // Doesn't work - the animation block is ignoring the frames set in the above loop, so no offset is seen
  // If I comment out the animation block below, the frames with offset are set properly
  NSAnimationContext.runAnimationGroup {
      context in
      self.scrollView!.documentView!.subviews.forEach { (index, view) in
         view.frame = .init(origin: newOrigins[index], size: newSize)
      }
   }
}

I assume it is something with the NSView animation architecture - i.e. the frames does not 'latch' if they are immediately animated after.

Could somebody give me an advice how to solve this?

I tried different ways to loop the views, using implicit animations, etc, but no success.

Edit: The views involved are all layer backed.

Upvotes: 0

Views: 60

Answers (0)

Related Questions