lbrendanl / SwiftSwipeView

93 stars 19 forks source link

Start at BViewController? #7

Open thewthew opened 9 years ago

thewthew commented 9 years ago

Hi! Thanks for the project, it's very good :+1: I have a question : is it possible that when the app starts it shows first the BViewController ? So that when you open the app you can swipe left or right ?

Thanks for your work here :)

thewthew commented 9 years ago

just resolved this ^^ Just needed to scroll to some point in a scroll view

simply add this line : self.scrollView! .setContentOffset(CGPoint(x: self.view.frame.width, y: scrollHeight), animated: false) after this line : self.scrollView!.contentSize = CGSizeMake(scrollWidth, scrollHeight);

if you want to start by C View : self.scrollView! .setContentOffset(CGPoint(x: 2*self.view.frame.width, y: scrollHeight), animated: false) etc.

davalcato commented 9 years ago

Yes that shouldn't be a problem. What are you building?

Sent from my iPhone

On Jun 2, 2015, at 3:16 PM, Matthew Usdin notifications@github.com wrote:

Hi! Thanks for the project, it's very good
I have a question : is it possible that when the app starts it shows first the BViewController ? So that when you open the app you can swipe left or right ?

Thanks for your work here :)

― Reply to this email directly or view it on GitHub.

LuAndreCast commented 9 years ago

I tried that and i get a white screen but once i touch the screen, the contents of the view controller are displayed. is there another alternative ? I am building a music app.

thewthew commented 9 years ago

Hi!

I found your bug when you touch the screen : you have to set the scroll height to 0, like this :

var scrollWidth: CGFloat = 3 * self.view.frame.width var scrollHeight: CGFloat = 0 self.scrollView!.contentSize = CGSizeMake(scrollWidth, scrollHeight); self.scrollView! .setContentOffset(CGPoint(x: self.view.frame.width, y: scrollHeight), animated: false)

I have changed the scrollHeight because when you where scrolling up or down on a tab it was also detecting scrolling to left/right. For example if you had typically a tableView on your left tab and you'd scrolled it down the view would also scroll a little one the right, which is a bad UX I guess.

Tell me if it's all good :)

LuAndreCast commented 9 years ago

how can one programmatically move to a desired controller? (Example, you are in A controller and want to move up to B controller) Usually It is the below but with this scrollview, its different

var next = self.storyboard?.instantiateViewControllerWithIdentifier("MusicController") as! MusicViewController self.presentViewController(next, animated: true, completion: nil)

lbrendanl commented 9 years ago

@LuAndreCast

Give this a try:

    var xPos: CGFloat = self.view.frame.width * CGFloat(viewNum)
    self.scrollView.setContentOffset(CGPointMake(xPos,0), animated: true)

Where viewNum is the index of the view you would like to scroll to. (0 for A controller, 1 for B controller, etc.)

@thewthew

When I needed to embed a vertical scrolling view I ended up doing it all with a constraint based layout, but if just modifying the scrollHeight worked for you, I see no problem with it.

LuAndreCast commented 9 years ago

@lbrendanl tried what as suggested but it stating that "AViewController" does not have a member named 'scrollview'.

I think what was suggested will only work inside the ContainerViewController since that is the only place the scrollview exist... i know I am stating the obvious , sorry, just don't know going crazy a bit lol

lbrendanl commented 9 years ago

@LuAndreCast Yes, sorry, allow me to give a more complete answer. The code snippet above that I mentioned is something that goes in the ContainerViewController. If you wish to access this from one of the child view controllers, you should utilize a protocol/delegate pattern. This design pattern allows one object to perform an operation on behalf of another object.

In this case, the ContainerViewController is a delegate for the child view controller. When something happens in a child view controller that should cause the app to move to scroll to a different child view, the child view controller can call a protocol method of the its delegate (the ContainerViewController).

I'd recommend doing some background reading on the delegation pattern in iOS, but all the code you should need is the following:

In ContainerViewController.swift:

enum ChildViews {
    case A
    case B
    case C
}  

// Declare this protocol outside the class
protocol ContainerView {
    // This method allows a child to tell the parent view controller
    // to change to a different child view
    func moveToView(viewNum: ChildViews)
}

// Make the class conform to the protocol by add the protocol to the class declaration
class ContainerViewController: UIViewController, ContainerView {
   /// all the original class code

    override func viewDidLoad() {
        // All the original viewDidLoad code, but you need to set the delegate for each child view
        // controller after you create them, like this
        AVc.delegate = self
    }

   /// Make sure you add this method to conform to the protocol
   func moveToView(viewNum: ChildViews) {
        var viewNum = -1
        switch viewToShow {
        case ChildViews.Selection:
            viewNum = 0
        case ChildViews.Feed:
            viewNum = 1
        case ChildViews.Camera:
            viewNum = 2
        }

        // Determine the offset in the scroll view we need to move to
        var xPos: CGFloat = self.view.frame.width * CGFloat(viewNum)
        self.scrollView.setContentOffset(CGPointMake(xPos,0), animated: true)
   }
}

And then in each of the child view controllers where you might need the functionality of scrolling to another child, you can add the following:

// Declare this 
 var delegate: ContainerView!
// When the child view controller needs to call the protocol, do
self.delegate!.moveToView(ChildViews.A)

Hope that helps!

LuAndreCast commented 9 years ago

@lbrendanl thank you so much! i will definitely look into it!

LuAndreCast commented 9 years ago

@lbrendanl another note. will I have to do the same ^^^ to be able to move from a controller (A,B,C) to a controller outside this scrollview?

var next = self.storyboard?.instantiateViewControllerWithIdentifier("outsiderView") as! OutsiderViewController self.presentViewController(next, animated: true, completion: nil)

I place the above inside a button and it works but if i try to do it programmatically I get the below error:

2015-06-16 22:33:33.683 SwiftSwipeView[914:172341] Warning: Attempt to present <SwiftSwipeView.OutsiderViewController: 0x15dd0d6c0> on <SwiftSwipeView.FirstViewController: 0x15de11430> whose view is not in the window hierarchy!

LuAndreCast commented 9 years ago

@lbrendanl Please disregard it works outside the viewdidload method. (on the viewdidAppear).

modsoussi commented 9 years ago

@lbrendanl Hey man, thanks a lot for this, it's been of extreme help to me in my project! I have a question though, I noticed that when I comment out the self.addChildView and VC.didMoveToParentViewController, the screens still work as they're supposed to. So why are those lines necessary?

samyoungnyc commented 8 years ago

Thanks for this - however for some reason I get two black bands at the bottom and top of the screen. I am using Xcode 7 beta 6 if that makes any difference. Any idea what's going on ?

antoineplane commented 8 years ago

Hello everyone, First I would like to thank you for the repo, it is very useful ! But I would like now to have a buttons in my 3 navigationcontrollers which are in the scrollview (I changed the xib files to navigation controllers), to scroll programmaticaly from the different navigationcontrollers.

Like in the snapchat app for example, you can swipe but you can also clic on a button which make the scrolling. I've search a lot but I did not find a solution, I think that I have to do something with subviews or relation parent-child but I don't understand exactly what is means.

Thanks

t2ac32 commented 8 years ago

@antoineplane To Add 3 buttons: i added my 3 buttons in a separate bar to recicle the view, but you can add them directly into you container view.

1) In story board select your buttons and add a different tag to each button.

link the buttons to this action you can create an iBAction by ctrl+draggin to the view controller or you can create a function and lin them by adding a target in view did load and creating an iboutlet for your button.

myBtn.addTarget(self , action: #selector(HomeViewController.loadSection(_:)), forControlEvents: UIControlEvents.TouchUpInside)

 func loadSection(sender: UIButton) {
            //What we are doing here is get the total width of your scroll view and multiply it by the tag you tags should start from 0.

            self.scrollView.setContentOffset(CGPointMake(self.scrollView.frame.size.width * CGFloat(sender.tag), 0), animated: true)

    }

try an tell how it went. i'm doing this and it is working but may fail in other cases. :)