Cannot switch to another child view controller in container view - ios

I have a main view controller, a container view and 2 child view controllers and i would like to be able to switch between the children (for example: when the application loads for the first time, i would like that the controller containing the MapView to be loaded and when i press the Search Bar found in the main view, the controller with the table to be loaded).
Here is my storyboard: https://i.stack.imgur.com/rDPMe.png
MainScreen.swift
class MainScreen: UIViewController {
#IBOutlet private weak var searchBar: UISearchBar!
#IBOutlet private weak var ContainerView: UIView!
//private var openSearchBar: Bool?
private var openMapView: Bool = true
private var openPlacesList: Bool = false
private var containerView: ContainerViewController!
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//let containerView = segue.destination as? ContainerViewController
if containerView == nil{
containerView = segue.destination as? ContainerViewController
}
if openMapView == true{
containerView!.moveToMapView()
}
else if openPlacesList == true{
containerView!.MoveToOpenPlaces()
}
}
}
//search bar delegate functions
extension MainScreen: UISearchBarDelegate{
//detects when text is entered
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
openPlacesList = true
openMapView = false
containerView!.MoveToOpenPlaces()
}
}
ContainerViewController.swift:
class ContainerViewController: UIViewController {
private var childViewController: UIViewController!
private var first: UIViewController?
private var sec: UIViewController?
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "MainToMap"{
first = segue.destination as! MapViewController
self.addChild(first!)
self.view.addSubview(first!.view)
self.didMove(toParent: self)
}else{
sec = segue.destination as! PlacesListController
}
if(first != nil && sec != nil){
interchange(first!,sec!)
}
}
func interchange(_ oldVc: UIViewController,_ newVc: UIViewController ){
oldVc.willMove(toParent: nil)
self.addChild(newVc)
self.view.addSubview(newVc.view)
self.transition(from: oldVc, to: newVc, duration: 2, options: UIView.AnimationOptions.transitionCrossDissolve, animations: {
newVc.view.alpha = 1
oldVc.view.alpha = 0
}, completion: { (complete) in
oldVc.view.removeFromSuperview()
oldVc.removeFromParent()
newVc.willMove(toParent: self)
})
}
func moveToMapView(){
performSegue(withIdentifier: "MainToMap", sender: nil)
}
func MoveToOpenPlaces(){
performSegue(withIdentifier: "MainToSearches", sender: nil)
}
}
The problem is that when I press the search bar, it calls the method interchange and then it just gives a SIGABRT 1 error. I tried this tutorial: https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html#//apple_ref/doc/uid/TP40007457-CH11-SW1 and many more but so far no luck. I am stucked here and don't know how i can solve this problem.
Stack: https://i.stack.imgur.com/Zqpm1.png
SIGABR 1 Error: https://i.stack.imgur.com/NBgEN.png

You appear to be trying to manually transition between child view controllers, but at the same time using segues (which do their own transitioning for you). Eliminate the segues (other than the initial embed segue, if you're using a storyboard with a "container view"), and just manually instantiate the child view controllers using their storyboard IDs. But don't use segues and then try to replace the child view controllers in prepare(for:sender:).
Also, when you use transition(from:to:duration:options:animations:completion:), you should not add the views the the view hierarchy yourself. That method does that for you (unless you use the showHideTransitionViews option, which tells the method that you're taking this over, something we don't need to do here). Likewise, when you use the transitionCrossDissolve option, you don't need to mess with alphas, either.
Thus, using the code snippet from that article you reference, you can do:
class FirstViewController: UIViewController {
#IBOutlet weak var containerView: UIView! // the view for the storyboard's "container view"
#IBOutlet weak var redButton: UIButton! // a button to transition to the "red" child view controller
#IBOutlet weak var blueButton: UIButton! // a button to transition to the "blue" child view controller
// tapped on "transition to red child view controller" button
#IBAction func didTapRedButton(_ sender: UIButton) {
redButton.isEnabled = false
blueButton.isEnabled = true
let oldVC = children.first!
let newVC = storyboard!.instantiateViewController(withIdentifier: "RedStoryboardID")
cycle(from: oldVC, to: newVC)
}
// tapped on "transition to blue child view controller" button
#IBAction func didTapBlueButton(_ sender: UIButton) {
blueButton.isEnabled = false
redButton.isEnabled = true
let oldVC = children.first!
let newVC = storyboard!.instantiateViewController(withIdentifier: "BlueStoryboardID")
cycle(from: oldVC, to: newVC)
}
func cycle(from oldVC: UIViewController, to newVC: UIViewController) {
// Prepare the two view controllers for the change.
oldVC.willMove(toParent: nil)
addChild(newVC)
// Get the final frame of the new view controller.
newVC.view.frame = containerView.bounds
// Queue up the transition animation.
transition(from: oldVC, to: newVC, duration: 0.25, options: .transitionCrossDissolve, animations: {
// this is intentionally blank; transitionCrossDissolve will do the work for us
}, completion: { finished in
oldVC.removeFromParent()
newVC.didMove(toParent: self)
})
}
func display(_ child: UIViewController) {
addChild(child)
child.view.frame = containerView.bounds
containerView.addSubview(child.view)
child.didMove(toParent: self)
}
func hide(_ child: UIViewController) {
child.willMove(toParent: nil)
child.view.removeFromSuperview()
child.removeFromParent()
}
}
That yields:

Related

Container view function not calling in parent view

I'm trying to run a function in a container view that I added in the storyboard editor but when I call it in the parent view controller, nothing happens. I want to be able to call a function and change a property of the child view controller triggered from the parent. Am I doing something wrong or should I just be doing this another way?
TLDR: Trigger a function in a child view controller from parent
Parent View Controller:
// ViewController.swift
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var routeConfirmationView: UIView! //This is the container view that I'm trying to work with
var RouteSelectionViewController: RouteSelectionViewController?
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(routeConfirmationView)
self?.RouteSelectionViewController?.getRidOfLoadingCover(isHidden: true) //The code that isn't doing anything
}
}
Container View Controller:
import UIKit
class RouteSelectionViewController: UIViewController {
#IBOutlet weak var loadingCoverView: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad(
}
//The function that I want to trigger from the other view controller:
func getRidOfLoadingCover (isHidden: Bool){
if (isHidden == true) {
loadingCoverView.alpha = 0
}
else if (isHidden == false) {
loadingCoverView.alpha = 100
}
}
}
Storyboard:
To call functions, or access properties, in a View Controller embedded in a Container View, you need to get and keep a reference to that controller.
When the Container View loads the embedded VC, it calls Prepare For Segue. Grab your reference there:
class WithContainerViewController: UIViewController {
var routeSelectionVC: RouteSelectionViewController?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? RouteSelectionViewController {
// save reference to VC embedded in Container View
self.routeSelectionVC = vc
}
}
#IBAction func didTap(_ sender: Any) {
if let vc = routeSelectionVC {
vc.getRidOfLoadingCover(isHidden: true)
}
}
}
class RouteSelectionViewController: UIViewController {
#IBOutlet weak var loadingCoverView: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
}
//The function that I want to trigger from the other view controller:
func getRidOfLoadingCover (isHidden: Bool){
if (isHidden == true) {
loadingCoverView.alpha = 0
}
else if (isHidden == false) {
loadingCoverView.alpha = 100
}
}
}
You will likely next ask about calling a function in the "parent" VC from the embedded VC. This can be done with protocol / delegate pattern, or with closures. Again, you can set that up in prepare for segue:
class WithContainerViewController: UIViewController {
var routeSelectionVC: RouteSelectionViewController?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? RouteSelectionViewController {
// set the closure in the VC embedded in Container View
vc.callbackClosure = {
self.routeSelectionButtonTapped()
}
// save reference to VC embedded in Container View
self.routeSelectionVC = vc
}
}
#IBAction func didTap(_ sender: Any) {
if let vc = routeSelectionVC {
vc.getRidOfLoadingCover(isHidden: true)
}
}
func routeSelectionButtonTapped() -> Void {
print("Button in RouteSelectionViewController in Container View was tapped!")
}
}
class RouteSelectionViewController: UIViewController {
#IBOutlet weak var loadingCoverView: UIActivityIndicatorView!
var callbackClosure: (() -> ())?
override func viewDidLoad() {
super.viewDidLoad()
}
//The function that I want to trigger from the other view controller:
func getRidOfLoadingCover (isHidden: Bool){
if (isHidden == true) {
loadingCoverView.alpha = 0
}
else if (isHidden == false) {
loadingCoverView.alpha = 100
}
}
#IBAction func didTap(_ sender: Any) {
callbackClosure?()
}
}
I guess you did not set the RouteSelectionViewController variable (btw., variable names should start with a small letter), so it is nil and nothing happens.
If you want you embed a view controller into another, you need to implement the view controller containment requirements (see documentation).

I am trying to using a custom delegate in swift but when I want to call its methods, it did not get called

I have been searching for how the delegate works and I tried to do it in my project. Unfortunately, the delegate method I implement does not get called ever. I am trying to do a slide-out navigation panel. so what I did is that I put two uicontainerviews, one is for slide-out navigation panel and the other for main view controller
enter image description here
The code is that
For main view controller
protocol MainViewControllerDelegate {
func toggleSideMenu()
}
class MainViewController: UIViewController {
var delegate: MainViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - Slide Action
#IBAction func slideMenuTapped(_ sender: UIBarButtonItem){
delegate?.toggleSideMenu()
print("Slide Menu has been tapped")
}
}
For container view controller
class ContainerVC: UIViewController {
#IBOutlet weak var SideMenuConstraint: NSLayoutConstraint!
#IBOutlet weak var slideMenuContainer: UIView!
#IBOutlet weak var mainViewContainer: UIView!
var mainViewController: MainViewController?
var isSideMenuOpened = false
override func viewDidLoad() {
super.viewDidLoad()
mainViewController = UIStoryboard.mainViewController()
mainViewController?.delegate = self
}
}
extension ContainerVC: MainViewControllerDelegate{
func toggleSideMenu() {
print("It works")
if isSideMenuOpened{
isSideMenuOpened = false
SideMenuConstraint.constant = -260
mainViewContainer.layer.shadowOpacity = 0
} else {
isSideMenuOpened = true
SideMenuConstraint.constant = 0
mainViewContainer.layer.shadowOpacity = 0.59
}
UIView.animate(withDuration: 0.3) {
self.view.layoutIfNeeded()
}
}
}
extension UIStoryboard{
static func mainStoryboard() -> UIStoryboard { return UIStoryboard(name: "Main", bundle: Bundle.main) }
static func mainViewController() -> MainViewController? {
return mainStoryboard().instantiateViewController(withIdentifier: "MainViewController") as? MainViewController
}
}
Please let know what's wrong
I think the reason is that you embed your main view controller in navigation controller :
let navigationController = self.childViewControllers.last as! UINavigationController
let mainViewController = navigationController.topViewController as! MainViewController
mainViewController?.delegate = self
Here is where you got wrong:
mainViewController = UIStoryboard.mainViewController()
mainViewController?.delegate = self
this mainViewController is not the same as the child of the container view controller, so setting its delegate doesn't really do anything.
You need to first get the VC that is the child of the container view controller:
mainViewController = self.childViewControllers.last as! MainViewController
mainViewController.delegate = self

How to call a function in the first controller after dismissing the second controller

I have two UIViewController, when I click a button, it goes from the first view controller to the second one. And before that, I animated a UIView to move to another place. After dismissing the second View Controller, I want to move the UIView in the first view controller back to where it originally was. However, when I call a function from the second View Controller to animate the UIview in the first view controller after dismissing the second one, It could not get the UIView's properties, and cannot do anything with it. I think because the first UIViewController is not loaded yet. Is that the problem? And How should I solve this?
There are two solutions you can either use swift closures
class FirstViewController: UIViewController {
#IBAction func start(_ sender: Any) {
guard let secondController = self.storyboard?.instantiateViewController(withIdentifier: "SecondController") as? SecondController else { return }
secondController.callbackClosure = { [weak self] in
print("Do your stuff")
}
self.navigationController?.pushViewController(secondController, animated: true)
}
}
//----------------------------
class SecondController: UIViewController {
var callbackClosure: ((Void) -> Void)?
override func viewWillDisappear(_ animated: Bool) {
callbackClosure?()
}
}
or you can use protocols
class FirstViewController: UIViewController {
#IBAction func start(_ sender: Any) {
guard let secondController = self.storyboard?.instantiateViewController(withIdentifier: "SecondController") as? SecondController else { return }
secondController.delegate = self
self.navigationController?.pushViewController(secondController, animated: true)
}
}
extension ViewController : ViewControllerSecDelegate {
func didBackButtonPressed(){
print("Do your stuff")
}
}
//--------------------------
protocol SecondControllerDelegate : NSObjectProtocol {
func didBackButtonPressed()
}
class SecondController: UIViewController {
weak var delegate: SecondControllerDelegate?
override func viewWillDisappear(_ animated: Bool) {
delegate?.didBackButtonPressed()
}
}
You can try to use a closure. Something like this:
class FirstViewController: UIViewController {
#IBOutlet weak var nextControllerButton: UIButton!
private let animatableView: UIView = UIView()
private func methodsForSomeAnimation() {
/*
perform some animation with 'animatableView'
*/
}
#IBAction func nextControllerButtonAction() {
// you can choose any other way to initialize controller :)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
guard let secondController = storyboard.instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController else { return }
secondController.callbackClosure = { [weak self] in
self?.methodsForSomeAnimation()
}
present(secondController, animated: true, completion: nil)
}
}
class SecondViewController: UIViewController {
#IBOutlet weak var dismissButton: UIButton!
var callbackClosure: ((Void) -> Void)?
#IBAction func dismissButtonAction() {
callbackClosure?()
dismiss(animated: true, completion: nil)
/*
or you call 'callbackClosure' in dismiss completion
dismiss(animated: true) { [weak self] in
self?.callbackClosure?()
}
*/
}
}
When you present your second view controller you can pass an instance of the first view controller.
The second VC could hold an instance of the first VC like such:
weak var firstViewController: NameOfController?
then when your presenting the second VC make sure you set the value so it's not nil like so:
firstViewController = self
After you've done this you'll be able to access that viewControllers functions.
iOS 11.x Swift 4.0
In calling VC you put this code ...
private struct Constants {
static let ScannerViewController = "Scan VC"
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == Constants.ScannerViewController {
let svc = destination as? ScannerViewController
svc?.firstViewController = self
}
}
Where you have named the segue in my case "Scan VC", this is what it looks like in Xcode panel.
Now in scan VC we got this just under the class declaration
weak var firstViewController: HiddingViewController?
Now later in your code, when your ready to return I simply set my concerned variables in my firstViewController like this ...
self.firstViewController?.globalUUID = code
Which I have setup in the HiddingViewController like this ...
var globalUUID: String? {
didSet {
startScanning()
}
}
So basically when I close the scanning VC I set the variable globalUUID which in term starts the scanning method here.
When you are saying it could not get the UIView's properties it's because you put it as private ? Why you don't replace your UIView in the first controller when it disappears before to go to your secondViewController. I think it's a case where you have to clean up your view controller state before to go further to your second view controller.
Check IOS lifecycle methods : viewWillDisappear or viewDidDisappear through Apple documentation and just do your animation in one of these methods.
Very simple solution actually... Just put your animation in the viewDidAppear method. This method is called every time the view loads.
class firstViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// insert animation here to run when FirstViewController appears...
}
}

Calling child view controller using segmented control action?

I have two child view controllers in my parent view controller, I want to call them upon a value change in the segmented control, and want to set the value of the parent imageView through child view controllers.
protocol UserEdittedPhoto {
func UserIsDone(image:UIImage)
}
class ControllerFinal:UIViewController, UserEdittedPhoto{
func UserIsDone(imageEditted: UIImage){
self.usedImage=imageEditted
self.imageView.image=self.usedImage
}
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func segmentAction(sender:UISegmentedControl){
if (segmentedControl.selectedSegmentIndex==0){
performSegueWithIdentifier("EditIAm", sender: nil)
}
else if (segmentedControl.selectedSegmentIndex==1){
performSegueWithIdentifier("EditIAm", sender: nil)
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "EditIAm"{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("ControllerEdit")
self.presentViewController(controller, animated: true, completion: nil)
let nextView = segue.destinationViewController as! ControllerEdit
nextView.originalImage=self.imageView.image!
nextView.delegate=self
}
else if segue.identifier == "FilterIAm"{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("ControllerFilters")
self.presentViewController(controller, animated: true, completion: nil)
let nextView = segue.destinationViewController as! ControllerFilters
nextView.toBeFilter=self.imageView.image!
}
}
class ControllerEdit:UIViewController{
var delegate: UserEdittedPhoto? = nil
if (delegate != nil){
self.originalImage = UIImage(CIImage: CIImage(image: self.originalImage)!.exposureAdjustFilter(sliderValue.value)!)
self.delegate!.UserIsDone(self.originalImage)
print("I am Called!")
}
}
class ControllerFilters:UIViewController{
override func viewDidLoad() {
print("SAHASHhqdwiuhiuhsaiuhsaiudhiuash")
controllerFinal?.imageView.image=toBeFilter
print("SAHASHhqdwiuhiuhsaiuhsaiudhiuash")
super.viewDidLoad()
}
}
UPDATE
To reflect our discussion in the comments below, I don't think you really need Containers and View Controllers to manage your custom controls (Edit / Filters). It's overkill.
Instead, I think you should be creating custom Views, and then adding them to your main Storyboard.
Then you could simply hide/show your custom Views when users tap on the Segmented Control as well as passing values to them, for example:
CustomEditView.valueY = newValueY
CustomFiltersView.valueX = newValueX
Regarding:
I need to call it forcefully through segmentedControl action, so that
my values in the childView be updated
Then you need to map the target View Controllers to local variables and use them to update the target View Controller variables when users presses the segments.
I've update the code and "demo" in my answer to reflect that.
(Notice that I'm just putting random Strings in the labels to make a point.)
Now to the complete answer...
In the setup you described in your other question, which is based on containers, the View Controllers are already there, in the Storyboard. You absolutely don't need to present them again (you can remove performSegueWithIdentifier calls).
If I understood correctly, you just want to show different "controllers" to the user based on what they choose via a Segmented Control.
There are some ways for doing that, but the easiest one would be to hide and to show the containers of the ControllerEdit / ControllerFilters View Controllers -- by changing the containers isHidden variable state.
Like this:
Storyboard setup:
Code (based on my other answer):
import UIKit
protocol UpdateImageProtocol {
func userIsDone(image: UIImage)
}
class ViewController: UIViewController, UpdateImageProtocol {
#IBOutlet weak var imageView: UIImageView!
#IBOutlet weak var changeImageContainer: UIView!
var controllerEdit: ControllerEdit?
#IBOutlet weak var applyFilterContainer: UIView!
var controllerFilters: ControllerFilters?
var image = UIImage(named: "hello")
override func viewDidLoad() {
super.viewDidLoad()
userIsDone(image: image!)
}
func userIsDone(image: UIImage) {
imageView.image = image
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "controllerEdit" {
let nextView = segue.destination as! ControllerEdit
nextView.delegate = self
controllerEdit = nextView
} else if segue.identifier == "controllerFilters" {
let nextView = segue.destination as! ControllerFilters
controllerFilters = nextView
}
}
#IBAction func segmentAction(_ sender: UISegmentedControl) {
if sender.selectedSegmentIndex == 0 {
changeImageContainer.isHidden = false
applyFilterContainer.isHidden = true
controllerEdit?.customLabel.text = String(arc4random_uniform(999))
} else {
changeImageContainer.isHidden = true
applyFilterContainer.isHidden = false
controllerFilters?.customLabel.text = String(arc4random_uniform(999))
}
}
}
class ControllerEdit: UIViewController {
#IBOutlet weak var customLabel: UILabel!
var image = UIImage(named: "newHello")
var delegate: UpdateImageProtocol?
#IBAction func changeImage(sender: UIButton) {
delegate?.userIsDone(image: image!)
}
}
class ControllerFilters: UIViewController {
#IBOutlet weak var customLabel: UILabel!
// TODO
}
Put a breakpoint in this function:
#IBAction func segmentAction(sender:UISegmentedControl){
if (segmentedControl.selectedSegmentIndex==0){
performSegueWithIdentifier("EditIAm", sender: nil)
}
else if (segmentedControl.selectedSegmentIndex==1){
performSegueWithIdentifier("EditIAm", sender: nil)
}
}
If it's not getting called, then you probably didn't connect it to an action in IB (is the circle to the left of the the #IBAction filled in?)
If it is getting called, then make sure the segue names are right -- also, fix the one in the else if, because it looks like you want "FilterIAm" there.
Then, put a breakpoint in prepareForSegue:... -- is that getting called? If not, recheck the names are the same as in IB.
EDIT: based on comment
Your prepareForSegue is not supposed to create the ViewController. The destination view controller is created as a consequence of performing the segue and passed to this function.
if segue.identifier == "EditIAm"{
let nextView = segue.destinationViewController as! ControllerEdit
nextView.originalImage=self.imageView.image!
nextView.delegate=self
}
You don't need to present anything -- the destinationViewController is going to be presented. You can set any of its variables (as you have) - that is what is meant by preparing for the segue.

How to animate a transition between 3 or more view controllers with buttons in SWIFT 3?

I'm relatively new in programming and currently try to build an app with three (if possible more) view controllers. I did a tutorial (https://www.youtube.com/watch?v=B9sH_VxPPo4&t=505s) in which I learned to animate between two view controllers after pressing a custom button. This all worked perfectly.
But now I try to implement another view controller (ThirdViewController) and a second button (showThirdVCButton).
The transitioning works perfectly for both buttons but the animation is set back to the standard animation.
This is my code in the initial 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()
}
}
Thank you for your help!!
huberdo - I followed the same tutorial you were looking at, and I was able to replicate the error you're getting, when I copied the second button rather than creating one from scratch. Have a look and check that you only have one segue for each button.
I think you can simplify the code. I would always prefer to set up an action for the buttons, so that you can see in your code where things are being called - what you have just now is a combination of Storyboard setup and explicit code.
If you use actions for the buttons, and set a flag to keep track of which button is pressed, it all becomes a lot simpler
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)
}
Simplified animation functions
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
}

Resources