![](/img/trans.png)
[英]UIView.animateWithDuration doesn't animate on separate view controller
[英]My subviews doesn't animate with the view controller
我有一個簡單的示例項目,其中我正在播放一些視圖控制器自定義動畫,以集成到更大的項目中,但我發現它們存在一些問題。
我有 2 個簡單的視圖控制器,第一個帶有按鈕來呈現第二個,帶有自定義動畫; 第二個有一些標簽和一個關閉按鈕,可以將視圖控制器設置為“脫離場景”
當我呈現視圖控制器時,在單擊呈現按鈕后,呈現的視圖控制器的子視圖在視圖控制器動畫之前出現在屏幕上,但是當我關閉 VC 時,所有子視圖都與解除視圖控制器。
在我的視圖控制器 2(呈現)上,我有默認自動布局的視圖(重置為建議的約束)
我找不到為什么子視圖沒有按預期在視圖控制器內部設置動畫的理由。
下面是一個 gif 顯示正在發生的事情,以及源代碼:
代碼:視圖控制器 1(呈現 VC)
import UIKit
class ViewController: UIViewController, UIViewControllerTransitioningDelegate {
var animator = Animator()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func presentButton(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil);
let vc = storyboard.instantiateViewController(withIdentifier: "vc2") as! ViewController2
vc.transitioningDelegate = self
vc.modalPresentationStyle = .custom // chama as funções à parte
present(vc, animated: true, completion: nil)
}
// REMARK: UIViewControllerTransitioningDelegate
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
animator.transitioningMode = .Present // sabe que está em presenting mode
return animator
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
animator.transitioningMode = .Dismiss // Sabe que está em dismissing mode
return animator
}
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
return CustomPresentationController(presentedViewController: presented, presenting: presenting)
}
}
代碼:視圖控制器 2(呈現 VC)
import UIKit
class ViewController2: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func dismissButton(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
}
代碼:CustomPresentationController
import UIKit
import Foundation
class CustomPresentationController: UIPresentationController {
override init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController!) {
super.init(presentedViewController: presentedViewController, presenting: presentingViewController)
}
override var frameOfPresentedViewInContainerView: CGRect {
// arranca a 0
var presentedViewFrame = CGRect.zero
// Calcula os bounds do container
let containerBounds = self.containerView?.bounds
// Recalcula o size
presentedViewFrame.size = CGSize(width: (containerBounds?.size.width)! , height: ((containerBounds?.size.height)! * 0.90))
presentedViewFrame.origin.x = 0
presentedViewFrame.origin.y = (containerBounds?.size.height)! * 0.1
return presentedViewFrame
}
}
CODE:動畫師類
import Foundation
import UIKit
class Animator: NSObject, UIViewControllerAnimatedTransitioning {
enum Status {
case Present
case Dismiss
}
var transitioningMode: Status = .Present
var presentDuration = 1.0
var dismissDuration = 0.3
// Tempo da animação
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
if (transitioningMode == .Present) {
return presentDuration
} else {
return dismissDuration
}
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
// Get the set of relevant objects.
let containerView = transitionContext.containerView
guard
let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from),
let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)
else {
print("Returning animateTransition VC")
return
}
let toView = transitionContext.view(forKey: UITransitionContextViewKey.to)
let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from)
// Set up some variables for the animation.
let containerFrame: CGRect = containerView.frame
var toViewStartFrame: CGRect = transitionContext.initialFrame(for: toVC)
let toViewFinalFrame: CGRect = transitionContext.finalFrame(for: toVC)
var fromViewFinalFrame: CGRect = transitionContext.finalFrame(for: fromVC)
// Set up animation parameters.
if (transitioningMode == .Present) {
// Modify the frame of the presented view so that it starts
// offscreen at the lower-right corner of the container.
toViewStartFrame.origin.x = 0//containerFrame.size.width
toViewStartFrame.origin.y = containerFrame.size.height * 0.1
} else {
// Modify the frame of the dismissed view so it ends in
// the lower-right corner of the container view.
fromViewFinalFrame = CGRect(x: containerFrame.size.width,
y: containerFrame.size.height,
width: (toVC.view.frame.size.width),
height: (toVC.view.frame.size.height))
}
if (transitioningMode == .Present) {
// Always add the "to" view to the container.
// And it doesn't hurt to set its start frame.
containerView.addSubview(toView!)
toView?.frame = toViewStartFrame
}
// Animate using the animator's own duration value.
UIView.animate(withDuration: presentDuration, animations: {
if (self.transitioningMode == .Present) {
// Move the presented view into position.
toView?.frame = toViewFinalFrame
}
else {
// Move the dismissed view offscreen.
fromView?.frame = fromViewFinalFrame
}
}) { (finished) in
let success = !(transitionContext.transitionWasCancelled)
// After a failed presentation or successful dismissal, remove the view.
if ((self.transitioningMode == .Present && !success) || (self.transitioningMode == .Dismiss && success)) {
toView?.removeFromSuperview()
}
// Notify UIKit that the transition has finished
transitionContext.completeTransition(success)
}
}
}
好的,我會盡力幫助你。 首先,它不起作用的原因是自動布局已定位您的視圖,但基本上您的框架大小為零。 為了向您展示這一點,請轉到有問題的控制器並讓它剪輯其子視圖,並且它們不會在過渡期間出現。 現在我可能不會更改框架以將其移入,因為您只想將其滑入並且您可能可以擺脫一些代碼。 這就是我的樣子。
import UIKit
class ViewController: UIViewController,UIViewControllerTransitioningDelegate {
var animator = Animator()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func presentModally(_ sender: Any) {
self.performSegue(withIdentifier: "modal", sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "modal"{
let dvc = segue.destination
dvc.transitioningDelegate = self
dvc.modalPresentationStyle = .overCurrentContext
}
}
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
animator.transitioningMode = .Present // sabe que está em presenting mode
return animator
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
animator.transitioningMode = .Dismiss // Sabe que está em dismissing mode
return animator
}
}
刪除自定義展示控制器。 老實說,我認為沒有必要。
現在是動畫師。
import UIKit
class Animator: NSObject,UIViewControllerAnimatedTransitioning {
enum Status {
case Present
case Dismiss
}
var transitioningMode: Status = .Present
var presentDuration = 1.0
var dismissDuration = 0.3
// Tempo da animação
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
if (transitioningMode == .Present) {
return presentDuration
} else {
return dismissDuration
}
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
// Get the set of relevant objects.
let containerView = transitionContext.containerView
guard
let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from),
let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)
else {
print("Returning animateTransition VC")
return
}
let toView = transitionContext.view(forKey: UITransitionContextViewKey.to)
let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from)
// Set up some variables for the animation.
let containerFrame: CGRect = containerView.frame
let toViewFinalFrame: CGRect = transitionContext.finalFrame(for: toVC)
var fromViewFinalFrame: CGRect = transitionContext.finalFrame(for: fromVC)
// Set up animation parameters.
if (transitioningMode == .Present) {
let anchor = toViewFinalFrame.origin
toView?.layer.anchorPoint = anchor
toView?.layer.position = anchor
toView?.transform = CGAffineTransform(scaleX: 0, y: 1)
//another posibility
//toView?.transform = CGAffineTransform(translationX: -containerView.bounds.width, y: -containerView.bounds.height)
} else {
// Modify the frame of the dismissed view so it ends in
// the lower-right corner of the container view.
fromViewFinalFrame = CGRect(x: containerFrame.size.width,
y: containerFrame.size.height,
width: (toVC.view.frame.size.width),
height: (toVC.view.frame.size.height))
}
if (transitioningMode == .Present) {
// Always add the "to" view to the container.
// And it doesn't hurt to set its start frame.
containerView.addSubview(toView!)
// toView?.frame = toViewStartFrame
}
// Animate using the animator's own duration value.
UIView.animate(withDuration: presentDuration, animations: {
if (self.transitioningMode == .Present) {
// Move the presented view into position.
toView?.transform = .identity
}
else {
// Move the dismissed view offscreen.
fromView?.frame = fromViewFinalFrame
}
}) { (finished) in
let success = !(transitionContext.transitionWasCancelled)
// After a failed presentation or successful dismissal, remove the view.
if ((self.transitioningMode == .Present && !success) || (self.transitioningMode == .Dismiss && success)) {
toView?.removeFromSuperview()
}
// Notify UIKit that the transition has finished
transitionContext.completeTransition(success)
}
}
}
這就是你所需要的。 干杯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.