簡體   English   中英

在segue中如何訪問子視圖

[英]How do I access subviews during a segue

我是iOS編程的新手,我想做一個簡單的選擇,在其中將源視圖和目標視圖的子視圖動畫化。

我無法在segue類中訪問源視圖中的子視圖以設置它們的動畫。 在segue期間訪問源視圖的子視圖並為其設置動畫的最簡單方法是什么?

這是我的代碼的簡化版本。

// Source view controller
class ViewController: UIViewController {
    @IBOutlet weak var myButton: UIButton!
    // other normal view controller code
}

class mySegue: UIStoryboardSegue {
    override func perform() {
        UIView.animate(withDuration: 1.0, animations: {
            // how do I access and animate myButton?
            // source.myButton
        })
    }
}

您可以使用以下代碼:

class MySegue: UIStoryboardSegue {
    override func perform() {
        UIView.animate(withDuration: 1.0, animations: {
            let sourceController = self.source as! ViewController
            sourceController.myButton
        })
    }
}

另一種選擇:如果要在離開場景過渡時為某物設置動畫,則可以在過渡旁邊設置動畫:

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    transitionCoordinator?.animate(alongsideTransition: { context in
        self.myButton.backgroundColor = .red            // animate change of background color during transition
    }, completion: nil)
}

此外,作為繼承segue的另一種選擇,您可以執行自定義轉換。 請參閱WWDC 2013 使用視圖控制器的自定義過渡 這取決於過渡和相關動畫的性質。 但這是進行強大而豐富的過渡的好方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM