0

I have a CorePlot CPTXYGraph in a CPTGraphHostingView which is constrained by AutoLayout. I animate a constraint change (shrinking an adjacent view) that also causes the CPTGraphHostingView to expand and would like the graph to animate its resizing along with the hosting view.

The constraint animation code is fairly boilerplate (see e.g. this SO question) and I'm using Swift:

    self.view.layoutIfNeeded()
    shrinkableViewHeightConstraint.constant = 100.0

    UIView.animateWithDuration(
        1.0,
        animations: { () -> Void in
            self.view.layoutIfNeeded()
    })

What I actually see is the graph jumping to the new final size of the container view immediately while the animation proceeds normally. The end result is correct - the graph and container are the correct size - but I'd like a smoother transition with the graph updating its size (and maintaining its plotSpace) throughout the animation. Is this possible?

Note: I've tried removing the AutoLayout and manually animating the frame changes (I'd really prefer to use AutoLayout - there are a lot of other pieces to position) but the result is the same which makes me think it's related to CorePlot architecture (or the underlying plot CALayer)

Community
  • 1
  • 1
Robin Macharg
  • 1,468
  • 14
  • 22
  • What is `self.view`? As noted in the answers to the linked question, you need to animate `layoutIfNeeded()` on the parent (or higher ancestor) view of the one with the animated constraint. – Eric Skroch Apr 25 '15 at 23:58
  • I've tried various values for the constraint (a height constraint), and appropriate view.layoutIfNeeded() - the graphHostingView, the adjacent view. I've also tried putting the graphHostingView inside another container view and calling layoutIfNeeded() on that and it's parent. All to no avail. The target bounds seem to be set before the animation takes place, causing the graph to redraw immediately. I'm wondering if a secondary CPTAnimation of the graphPadding might work if CorePlot's graph frame sizes aren't smoothly animatable. – Robin Macharg Apr 27 '15 at 10:34
  • The graph layout should follow its hosting view. Can you test with a dummy view temporarily in place of the hosting view? Give it a border and/or background that stands out from its neighbors so you can see if it animates correctly. If that works, put the hosting view back and we'll keep troubleshooting. If it doesn't, it means you have a problem in the constraint or the way you're doing the animation. – Eric Skroch Apr 30 '15 at 00:51
  • Hi Eric. I tried what you suggested and while the views animate smoothly as I'd expect, and the border changes as expected, the graph still has an abrupt size change at the start of the animation. I've put a simple mock-up that shows the effect [here on github](https://github.com/robinmacharg/corePlotConstraintAnimationTesting) if you want to have a look. It uses CocoaPods to get CorePlot. – Robin Macharg May 01 '15 at 14:36
  • Core Plot is based on Core Animation layers. `UIView` animations only affect the view backing layers. Since Core Plot layers are hosted layers, they don't animate with the view. See [this question](http://stackoverflow.com/questions/24670269) for a possible solution. – Eric Skroch May 02 '15 at 17:05
  • Ah ha. Thanks for the pointer! Will report back. – Robin Macharg May 02 '15 at 19:01

0 Answers0