我的 iPad 应用程序有一个页面需要使用 UIDynamicAnimator 进行开发。我在下面添加了一些代码。根据我的需要,它在 iOS8 中完美运行,但在 iOS7 上失败。当强行将框架更改为弹跳时,我不会重复调用委托。
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
})
}
我正在使用 XCode 6.1.0,iOS7 的应用程序兼容性。基础 SDK iOS 8.1
var Yes:Bool { return true }
var No:Bool { return false }
这些在应用程序中全局共享,例如宏或其他东西,以提高可读性
提前感谢您的支持。