-1

I added an UIDynamicAnimator onto my UIView which I created programmatically. Now I'd like to create the UIView in the Storyboard because I'd like to use autolayout. Does someone knows what I have to change for using the UIView from Storyboard?

My code:

func showSideBar(shouldOpen:Bool){

    let barWidth:CGFloat = CGFloat(self.view.bounds.size.width)

    animator.removeAllBehaviors()
    isSideBarOpen = shouldOpen

    let gravityX:CGFloat = (shouldOpen) ? 1.5 : -1.5
    let magnitude:CGFloat = (shouldOpen) ? 50 : -50
    let boundaryX:CGFloat = (shouldOpen) ? barWidth : -barWidth

    let gravityBehavior:UIGravityBehavior = UIGravityBehavior(items: [sideBarContainerView])
    gravityBehavior.gravityDirection = CGVectorMake(gravityX, 0)
    animator.addBehavior(gravityBehavior)

    let collisionBehavior:UICollisionBehavior = UICollisionBehavior(items: [sideBarContainerView])
    collisionBehavior.addBoundaryWithIdentifier("sideBarBoundary", fromPoint: CGPointMake(boundaryX, 20), toPoint: CGPointMake(boundaryX, view.frame.size.height))
    animator.addBehavior(collisionBehavior)

    let pushBehavior:UIPushBehavior = UIPushBehavior(items: [sideBarContainerView], mode: UIPushBehaviorMode.Instantaneous)
    pushBehavior.magnitude = magnitude
    animator.addBehavior(pushBehavior)

    let sideBarBehavior:UIDynamicItemBehavior = UIDynamicItemBehavior(items: [sideBarContainerView])
    sideBarBehavior.elasticity = 0.2
    animator.addBehavior(sideBarBehavior)

}
paro
  • 217
  • 3
  • 10

1 Answers1

1

You don't have to change anything about the code you've shown; it remains identical. The animator continues to live wherever it lives now.

The sideBarContainerView might need to become an outlet so you can get a reference to that view if it is now being created in the storyboard instead of in code, but that's hard to know from here, because you didn't show that part of your code and you didn't specify in your question what you mean by "my UIView" — I am assuming that this refers to sideBarContainerView.

Note, finally, that UIKit Dynamics and autolayout are enemies of one another. You cannot animate a view using UIKit Dynamics if it is under the influence of autolayout, because UIKitDynamics tries to change the frame of the view while autolayout tries to position the view through constraints.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thanks for your answer! Yes `sideBarContainerView` is the UIView. But if I run my app, rotate the screen and let the animation code from above run, the view falls out of the screen. Because of this I'd like to use autolayout. Do you have an Idea what I can do instead of using autolayout? – paro Aug 23 '15 at 16:21
  • You can use autolayout, but you'll have to think carefully about what should happen when the view comes under the influence of UIKit Dynamics — you might want to take it _out_ of the influence of autolayout temporarily. If UIKitDynamics moves the view, you might need to remove its constraints beforehand and then add new constraints afterwards that correspond to its new position. – matt Aug 23 '15 at 16:29