gpt4 book ai didi

ios - 如何在 SWIFT 3 中使用按钮为 3 个或更多 View Controller 之间的转换设置动画?

转载 作者:行者123 更新时间:2023-11-28 06:26:05 25 4
gpt4 key购买 nike

我在编程方面相对较新,目前正在尝试使用三个(如果可能的话更多) View Controller 构建一个应用程序。我做了一个教程(https://www.youtube.com/watch?v=B9sH_VxPPo4&t=505s),其中我学会了在按下自定义按钮后在两个 View Controller 之间制作动画。这一切都很完美。

但现在我尝试实现另一个 View Controller (ThirdViewController) 和第二个按钮 (showThirdVCButton)。

两个按钮的过渡效果都很好,但动画设置回标准动画。

这是我在初始 View Controller 中的代码:

import UIKit

class ViewController: UIViewController, UIViewControllerTransitioningDelegate {



@IBOutlet weak var showSecondVCButton: UIButton!
@IBOutlet weak var showThirdVCButton: UIButton!


let transition = CircularTransition()

override func viewDidLoad() {
super.viewDidLoad()


//I customise my buttons here
showSecondVCButton.layer.cornerRadius = showSecondVCButton.frame.size.width / 2
showThirdVCButton.layer.cornerRadius = showSecondVCButton.frame.size.width / 2
}

//The destination for each button is declared here and the animation style
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "secondVCSegue" {
let secondVC = segue.destination as! SecondViewController
secondVC.transitioningDelegate = self
secondVC.modalPresentationStyle = .custom
}

if segue.identifier == "thirdVCSegue" {
let thirdVC = segue.destination as! ThirdViewController
thirdVC.transitioningDelegate = self
thirdVC.modalPresentationStyle = .custom
}

}


func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {

transition.transitionMode = .present

if showSecondVCButton.isTouchInside == true {
transition.startingPoint = showSecondVCButton.center
transition.circleColor = showSecondVCButton.backgroundColor!
}

if showThirdVCButton.isTouchInside == true {

transition.startingPoint = showThirdVCButton.center
transition.circleColor = showThirdVCButton.backgroundColor!
}

return transition
}


func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
transition.transitionMode = .dismiss

if showSecondVCButton.isTouchInside == true {
transition.startingPoint = showSecondVCButton.center
transition.circleColor = showSecondVCButton.backgroundColor!
}

if showThirdVCButton.isTouchInside == true {

transition.startingPoint = showThirdVCButton.center
transition.circleColor = showThirdVCButton.backgroundColor!
}

return transition
}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

谢谢你的帮助!!

最佳答案

huberdo - 我按照您正在查看的相同教程进行操作,当我复制第二个按钮而不是从头开始创建按钮时,我能够复制您遇到的错误。查看并检查每个按钮是否只有一个 segue。

我认为你可以简化代码。我总是更喜欢为按钮设置一个 Action ,这样您就可以在代码中看到调用的地方 - 您刚才拥有的是 Storyboard 设置和显式代码的组合。

如果你对按钮使用 Action ,并设置一个标志来跟踪哪个按钮被按下,一切都会变得简单得多

var transition = CircularTransition()
var startingView = UIView() // this will define the starting point for all transitions

//
//
// explicit actions for the buttons
@IBAction func cmdOneAction(_ sender: Any)
{
startingView = sender as! UIView
self.performSegue(withIdentifier: "segueShow1", sender: self)
}

@IBAction func cmdTwoAction(_ sender: Any)
{
startingView = sender as! UIView
self.performSegue(withIdentifier: "segueShow2", sender: self)
}

简化的动画功能

func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {

transition.transitionMode = .present
transition.startingPoint = startingView.center
transition.circleColor = startingView.backgroundColor!

return transition
}


func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {

transition.transitionMode = .dismiss
transition.startingPoint = startingView.center
transition.circleColor = startingView.backgroundColor!

return transition
}

关于ios - 如何在 SWIFT 3 中使用按钮为 3 个或更多 View Controller 之间的转换设置动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41854906/

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