gpt4 book ai didi

ios - 如何使自定义过渡表现得像在重力下坠落?

转载 作者:可可西里 更新时间:2023-11-01 00:53:50 25 4
gpt4 key购买 nike

效果可以在animateTransition方法中实现如下代码:

UIView.animateWithDuration(duration, 
delay: 0,
usingSpringWithDamping: 0.3,
initialSpringVelocity: 0.0,
options: .CurveLinear,
animations: {
fromVC.view.alpha = 0.5
toVC.view.frame = finalFrame
},
completion: {_ -> () in
fromVC.view.alpha = 1.0
transitionContext.completeTransition(true)
})

但是我如何使用重力和碰撞行为(UIGravityBehaviorUICollisionBehavior)来实现它呢?

一个更普遍的问题可能是“如何使用 UIDynamicAnimator 自定义 UIViewControllers 之间的转换?”

最佳答案

您可以在帖子 Custom view controller transitions with UIDynamic behaviors 下找到解决方案 dasdom .

还有 Swift 代码:

func transitionDuration(transitionContext: UIViewControllerContextTransitioning!) -> NSTimeInterval {
return 1.0
}

func animateTransition(transitionContext: UIViewControllerContextTransitioning!) {

// 1. Prepare for the required components
let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
let finalFrame = transitionContext.finalFrameForViewController(toVC)
let containerView = transitionContext.containerView()
let screenBounds = UIScreen.mainScreen().bounds

// 2. Make toVC at the top of the screen
toVC.view.frame = CGRectOffset(finalFrame, 0, -1.0 * CGRectGetHeight(screenBounds))
containerView.addSubview(toVC.view)

// 3. Set the dynamic animators used by the view controller presentation
var animator: UIDynamicAnimator? = UIDynamicAnimator(referenceView: containerView)
let gravity = UIGravityBehavior(items: [toVC.view])
gravity.magnitude = 10

let collision = UICollisionBehavior(items: [toVC.view])
collision.addBoundaryWithIdentifier("GravityBoundary",
fromPoint: CGPoint(x: 0, y: screenBounds.height),
toPoint: CGPoint(x: screenBounds.width, y: screenBounds.height))

let animatorItem = UIDynamicItemBehavior(items: [toVC.view])
animatorItem.elasticity = 0.5

animator!.addBehavior(gravity)
animator!.addBehavior(collision)
animator!.addBehavior(animatorItem)

// 4. Complete the transition after the time of the duration
let nsecs = transitionDuration(transitionContext) * Double(NSEC_PER_SEC)
let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(nsecs))

dispatch_after(delay, dispatch_get_main_queue()) {
animator = nil
transitionContext.completeTransition(true)
}
}

比使用 animateWithDuration:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion: 方法稍微复杂一些。

编辑:修复了“transitionDuration”≤1 时的错误

关于ios - 如何使自定义过渡表现得像在重力下坠落?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24708035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com