我正在创建一个简单的闪存卡应用程序,如下图所示:
我想要向后滑动,就像这样:
要做到这一点,onBack(index: Int)
我需要在刷卡时调用(为了更新显示的卡):
import UIKit class ViewController: UIViewController { @IBOutlet weak var flashCardLabel: UILabel! // Populate initial content let content = ["Lorem", "Ipsum", "Dolor", "Sit"] // Index of where we are in content var index = 0 override func viewDidLoad() { super.viewDidLoad() } // Label text based on index func setLabelToIndex() { flashCardLabel.text = content[index] } // Go back @IBAction func back(_ sender: Any) { if index > 0 { index = index - 1 setLabelToIndex() } } // Go forward @IBAction func next(_ sender: Any) { if index + 1 < content.count { index = index + 1 setLabelToIndex() } } // Desired function to be called // when swiping back in navigation stack func onBack(index: Int) { self.index = index setLabelToIndex() } }
ThunderStruc.. 5
如果我正确理解您的问题,您希望能够在单击"下一步"或"返回"时在问题之间滑动和/或具有滑动效果.如果是这样的话,我建议你嵌入您UILabel
的UIScrollView
.看一下这个:
class ViewController: UIViewController,UIScrollViewDelegate { let content = ["Lorem", "Ipsum", "Dolor", "Sit"] let scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: 320, height: 300)) var index = 0 override func viewDidLoad() { super.viewDidLoad() scrollView.delegate = self scrollView.contentSize = CGSize(width: self.view.frame.width * content.count, height: self.scrollView.frame.size.height) scrollView.isPagingEnabled = true // add labels to pages for i in 0 ..< content.count { let label = UILabel(frame: CGRect(x: self.view.center.x * (i + 1), y: self.view.center.y, width: 100, height: 50)) label.textAlignment = .center label.text = content[i] scrollView.addSubview(label) } self.view.addSubview(scrollView) } // Go back @IBAction func back(_ sender: Any) { if index > 0 { index = index - 1 // scroll to page let offset = CGPoint(x: CGFloat(index) * self.view.frame.width, y: 0) self.scrollView.setContentOffset(offset, animated: true) } } // Go forward @IBAction func next(_ sender: Any) { if index + 1 < content.count { index = index + 1 // scroll to page let offset = CGPoint(x: CGFloat(index) * self.view.frame.width, y: 0) self.scrollView.setContentOffset(offset, animated: true) } } func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { index = round(scrollView.contentOffset.x / scrollView.frame.size.width) } }
说明:
您基本上创建一个UIScrollView
处理分页效果并使用数组中的相应文本UILabel
向每个页面添加一个content
.每次用户滚动到不同的页面时,index
都会更新到当前页面的索引.最后,当用户单击"下一步"或"返回"时,您将滚动到下一页或上一页
如果我正确理解您的问题,您希望能够在单击"下一步"或"返回"时在问题之间滑动和/或具有滑动效果.如果是这样的话,我建议你嵌入您UILabel
的UIScrollView
.看一下这个:
class ViewController: UIViewController,UIScrollViewDelegate { let content = ["Lorem", "Ipsum", "Dolor", "Sit"] let scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: 320, height: 300)) var index = 0 override func viewDidLoad() { super.viewDidLoad() scrollView.delegate = self scrollView.contentSize = CGSize(width: self.view.frame.width * content.count, height: self.scrollView.frame.size.height) scrollView.isPagingEnabled = true // add labels to pages for i in 0 ..< content.count { let label = UILabel(frame: CGRect(x: self.view.center.x * (i + 1), y: self.view.center.y, width: 100, height: 50)) label.textAlignment = .center label.text = content[i] scrollView.addSubview(label) } self.view.addSubview(scrollView) } // Go back @IBAction func back(_ sender: Any) { if index > 0 { index = index - 1 // scroll to page let offset = CGPoint(x: CGFloat(index) * self.view.frame.width, y: 0) self.scrollView.setContentOffset(offset, animated: true) } } // Go forward @IBAction func next(_ sender: Any) { if index + 1 < content.count { index = index + 1 // scroll to page let offset = CGPoint(x: CGFloat(index) * self.view.frame.width, y: 0) self.scrollView.setContentOffset(offset, animated: true) } } func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { index = round(scrollView.contentOffset.x / scrollView.frame.size.width) } }
说明:
您基本上创建一个UIScrollView
处理分页效果并使用数组中的相应文本UILabel
向每个页面添加一个content
.每次用户滚动到不同的页面时,index
都会更新到当前页面的索引.最后,当用户单击"下一步"或"返回"时,您将滚动到下一页或上一页