水平 UIScrollView 幻灯片上的增量变量

Increment Variable on horizontal UIScrollView slide

当用户在我的水平 UIScrollView 中向右或向左滑动时,我如何增加一个变量。

例如;

var pageNumber = 1

当用户向右滑动时它递增 1,当用户向左滑动时它递减 1 -

这是我在 viewDidLoad 函数中启动的 scrollView 代码。

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]);

}

查看已加载更新

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]);

}

更新 3 ##

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

更新 4

  1. 创建滑动手势
  2. 向视图添加手势

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
    }

}

我假设你不能使用 UIScrollView 的 paging 属性。

与之前的答案相反,您实际上需要两个不同的滑动识别器。参见

我了解到您想检测滚动视图上的滑动。为此,您可以将两个 UISwipeGestureRecognizers 添加到您的 scrollView,一个用于向左滑动,一个用于向右滑动,如下所示:

// 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)
}