gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:14:33 26 4
gpt4 key购买 nike

当用户在水平 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

最佳答案

我假设您无法使用 UIScrollView 的 paging 属性。

与之前的答案相反,您实际上需要两个不同的滑动识别器。请参阅https://stackoverflow.com/a/7760927/5007059

我知道您想要检测 ScrollView 上的滑动。为了实现这一点,你可以向你的scrollView添加两个UISwipeGestureRecognizers,一个用于向左滑动,一个用于向右滑动,如下所示:

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

关于swift - 水平 UIScrollView 幻灯片上的增量变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30966141/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com