当用户在水平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]);
}
//Add to the scrollView
scrollView.addSubview(view);
scrollView.addSubview(scrollViewQuiz!)
答案 0 :(得分:1)
我假设您无法使用UIScrollView的paging
属性。
与之前的答案相反,您实际上需要两个不同的滑动识别器。见https://stackoverflow.com/a/7760927/5007059
我知道你想要在滚动视图上检测滑动。为此,您可以在scrollView中添加两个UISwipeGestureRecognizer,一个用于左滑动,另一个用于右滑动,如下所示:
// 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)
}
答案 1 :(得分:0)
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
}
}