0

How would I increment a variable when the user swipes to the right or left in my horizontal UIScrollView.

For example;

var pageNumber = 1

When the user swipes right it increments by 1 and when swipes left it decrements by 1 -

Here is my scrollView code that is initiated in the viewDidLoad function.

for (var i : Int = 0; i < numberOfQuestions ;i++)
    {

        //Construct the view by using the Template XIB file
        var array : NSArray = NSBundle.mainBundle().loadNibNamed("QuestionView", owner: self, options: nil);
        var view : QuestionView = array.objectAtIndex(0) as! QuestionView

        // Set the scroll view to global variable

        scrollViewQuiz = view

        questionLabel.text = questions[currentQuestionIndexView].question
        questionViews.addObject(view);
        view.setTranslatesAutoresizingMaskIntoConstraints(false);
        view.backgroundColor = UIColor.clearColor();


        //Add to the scrollView
        scrollView.addSubview(view);
        //Add the top Constraints, they need to fit the superview
        let views : NSDictionary = ["view" : view,"scrollView" : scrollView];
        let constraints : NSArray = NSLayoutConstraint.constraintsWithVisualFormat("V:|[view(==scrollView)]|", options: NSLayoutFormatOptions.allZeros, metrics: nil, views: views as [NSObject : AnyObject]);
        scrollView.addConstraints(constraints as! [AnyObject]);

        // Increments the question
        currentQuestionIndexView++
    }
    contentView.backgroundColor = UIColor.clearColor();


    var viewsDictionary : NSMutableDictionary = NSMutableDictionary(dictionary: ["scrollView" : scrollView]);
    var visualFormat : NSMutableString = ("H:|").mutableCopy() as! NSMutableString;



    //With all the views created, create the Layout Constraints for the horizontal logic
    for (var i : Int = 0; i < numberOfQuestions; i++)

    {
        viewsDictionary.setValue(self.questionViews[i], forKey: NSString(format: "view%d", i) as String);
        visualFormat.appendFormat("[view%d(==scrollView)]", i);
    }

    visualFormat.appendString("|");

    constraints = NSLayoutConstraint.constraintsWithVisualFormat(visualFormat as String, options: NSLayoutFormatOptions.allZeros, metrics: nil, views: viewsDictionary as [NSObject : AnyObject]);
    scrollView.addConstraints(constraints as! [AnyObject]);

}

View Did Load UPDATE

override func viewDidLoad() {

    // QUIZ LOGIC START

    shuffleQuestions()

    // UI CONSTRAINTS AND VIEW GENERATION

    //Example of using 3 questions
    var scrollView = self.scrollView;
    scrollView.setTranslatesAutoresizingMaskIntoConstraints(false);
    self.view.setTranslatesAutoresizingMaskIntoConstraints(false);
    self.contentView.setTranslatesAutoresizingMaskIntoConstraints(false);


    //Constraints
    var constraints : NSArray;

    for (var i : Int = 0; i < numberOfQuestions ;i++)
    {

        //Construct the view by using the Template XIB file
        var array : NSArray = NSBundle.mainBundle().loadNibNamed("QuestionView", owner: self, options: nil);
        var view : QuestionView = array.objectAtIndex(0) as! QuestionView

        // Set the scroll view to global variable

        scrollViewQuiz = view

        questionLabel.text = questions[currentQuestionIndexView].question
        questionViews.addObject(view);
        view.setTranslatesAutoresizingMaskIntoConstraints(false);
        view.backgroundColor = UIColor.clearColor();

        //Add to the scrollView
        scrollView.addSubview(view);
        //Add the top Constraints, they need to fit the superview
        let views : NSDictionary = ["view" : view,"scrollView" : scrollView];
        let constraints : NSArray = NSLayoutConstraint.constraintsWithVisualFormat("V:|[view(==scrollView)]|", options: NSLayoutFormatOptions.allZeros, metrics: nil, views: views as [NSObject : AnyObject]);
        scrollView.addConstraints(constraints as! [AnyObject]);

        let leftSwipeRecognizer = UISwipeGestureRecognizer(target: self, action: "handleLeftSwipe:")
        leftSwipeRecognizer.direction = .Left
        scrollViewQuiz!.addGestureRecognizer(leftSwipeRecognizer)

        let rightSwipeRecognizer = UISwipeGestureRecognizer(target: self, action: "handleRightSwipe:")
        rightSwipeRecognizer.direction = .Right
        scrollViewQuiz!.addGestureRecognizer(rightSwipeRecognizer)


        // Increments the question
        currentQuestionIndexView++
    }
    contentView.backgroundColor = UIColor.clearColor();

    var viewsDictionary : NSMutableDictionary = NSMutableDictionary(dictionary: ["scrollView" : scrollView]);
    var visualFormat : NSMutableString = ("H:|").mutableCopy() as! NSMutableString;


    //With all the views created, create the Layout Constraints for the horizontal logic
    for (var i : Int = 0; i < numberOfQuestions; i++)

    {
        viewsDictionary.setValue(self.questionViews[i], forKey: NSString(format: "view%d", i) as String);
        visualFormat.appendFormat("[view%d(==scrollView)]", i);
    }

    visualFormat.appendString("|");

    constraints = NSLayoutConstraint.constraintsWithVisualFormat(visualFormat as String, options: NSLayoutFormatOptions.allZeros, metrics: nil, views: viewsDictionary as [NSObject : AnyObject]);
    scrollView.addConstraints(constraints as! [AnyObject]);

}

Update 3 ##

//Add to the scrollView
        scrollView.addSubview(view);
        scrollView.addSubview(scrollViewQuiz!)

Update 4

simlimsd3
  • 599
  • 2
  • 6
  • 14

2 Answers2

1

I'm assuming you can't use the paging property of UIScrollView.

Contrary to the previous answer, you will actually need two different swipe recognizers. See https://stackoverflow.com/a/7760927/5007059

I understand that you want to detect swipes on the scroll view. To accomplish this, you could add two UISwipeGestureRecognizers to your scrollView, one for left swipes and one for right swipes like so:

// add to viewDidLoad
let leftSwipeRecognizer = UISwipeGestureRecognizer(target: self, action: "handleLeftSwipe:")
leftSwipeRecognizer.direction = .Left
scrollView.addGestureRecognizer(leftSwipeRecognizer)

let rightSwipeRecognizer = UISwipeGestureRecognizer(target: self, action: "handleRightSwipe:")
rightSwipeRecognizer.direction = .Right
scrollView.addGestureRecognizer(rightSwipeRecognizer)

...

// add to your view controller subclass
func handleLeftSwipe(sender: UISwipeGestureRecognizer) {
    self.pageNumber = min(PageCount, self.pageNumber + 1)
}


func handleRightSwipe(sender: UISwipeGestureRecognizer) {
    self.pageNumber = max(0, self.pageNumber - 1)
}
Community
  • 1
  • 1
ndmeiri
  • 4,979
  • 12
  • 37
  • 45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/81138/discussion-between-simlimsd3-and-ndmeiri). – simlimsd3 Jun 22 '15 at 00:34
0
  1. create swipe gesture
  2. add gesture to the view

var pageNumber = 1

override func viewDidLoad() {
    super.viewDidLoad()
    ///////
    let swipeGesture = UISwipeGestureRecognizer(target: self, action: "swipeHandler:")
    swipeGesture.direction = .Left | .Right
    scrollViewQuiz.addGestureRecognizer(swipeGesture)
}

func swipeHandler( recognizer:UISwipeGestureRecognizer){

    switch(recognizer.direction){
    case UISwipeGestureRecognizerDirection.Left:
        pageNumber--
    case UISwipeGestureRecognizerDirection.Right:
        pageNumber++
    default: break
    }

}
nRewik
  • 8,958
  • 4
  • 23
  • 30
  • Ive had the same prroblem with the other answer as well. Ive gone over the code - added it to test, but my pageNumber doesn`t increment. I have also tried to debug and just make it println("Test") but nothing works - it appears to not be recognising the function. – – simlimsd3 Jun 21 '15 at 18:14