UIDynamicAnimator in swift ios 7

936 views Asked by At

My iPad application have one page needed to develop with UIDynamicAnimator. I added some piece of code below shown. Its perfectly work in iOS8 as per my need, but fails on iOS7. I am not getting repeated call to delegate when forcefully changing the frame to bouncing.

private var bounceCount:Int = 0
private var animator:UIDynamicAnimator!
private var animateWithBounce:Bool = Yes

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

// Do any additional setup.
if(self.animateWithBounce==Yes) {

self.hideAnimationViews()

var rect:CGRect = self.bounceView.frame
rect.origin.y = 319
self.bounceView.frame = rect
self.bounceCount = 0

var animator:UIDynamicAnimator = UIDynamicAnimator(referenceView: self.bounceContainerView)

var gravityBehavior:UIGravityBehavior = UIGravityBehavior(items: [self.bounceView])
gravityBehavior.gravityDirection = CGVector(dx: 0.0, dy: -1.0)
gravityBehavior.magnitude = 1.0
animator.addBehavior(gravityBehavior)

var collisionBehavior:UICollisionBehavior = UICollisionBehavior(items: [self.bounceView])
collisionBehavior.translatesReferenceBoundsIntoBoundary = Yes;
collisionBehavior.collisionDelegate = self;
animator.addBehavior(collisionBehavior)

var elasticityBehavior:UIDynamicItemBehavior = UIDynamicItemBehavior(items: [self.bounceView])
elasticityBehavior.elasticity = 0.8;
animator.addBehavior(elasticityBehavior)

self.animator = animator 
  }
}

// MARK: - UICollisionBehaviorDelegate methods

func collisionBehavior(behavior: UICollisionBehavior, endedContactForItem item: UIDynamicItem, withBoundaryIdentifier identifier: NSCopying) {
    var rect:CGRect = self.bounceView.frame

    if(self.bounceCount==0) {
        rect.origin.y = 79.25
    }
    else if(self.bounceCount==1)    {
        rect.origin.y = 19.25
    }
    else if(self.bounceCount==2)    {
        rect.origin.y = 09.25
    }
    else if(self.bounceCount==3)   {
        self.startAnimationOnViews()
    }

    self.bounceView.frame = rect

    bounceCount = bounceCount+1
}

// MARK: -

private func hideAnimationViews()
{
//some views hided here
}

private func startAnimationOnViews()
{
UIView.animateKeyframesWithDuration(duration, delay: delay, options: options, animations: {

UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: time1, animations: {
//some views showing on first time slot
})

UIView.addKeyframeWithRelativeStartTime(time1, relativeDuration: time2, animations: {
//some views showing on second time slot
})

}, completion: {finished in
//recursive call
})
}

I am using XCode 6.1.0, app compatibility from iOS7. Base SDK iOS 8.1

var Yes:Bool { return true }
var No:Bool { return false }

These are globally shared in app like macro or something for better readability

Thanks for support in advance.

1

There are 1 answers

3
matt On BEST ANSWER

The problem is this line:

self.bounceView.frame = rect

You are changing the frame of a view under the control of the dynamic animator, behind the dynamic animator's back. That is illegal. I'm surprised your code ever worked. The whole point of the dynamic animator is that it is the one setting the frame of the views that it controls.

To do this in a way that doesn't escape the notice of the dynamic animator, simply call updateItemUsingCurrentState. That's what it's for.