我想要向后滑动,就像这样:
要做到这一点,onBack(index:Int)是我需要在刷卡时调用的(为了更新显示的卡):
import UIKitclass 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() }}解决方法 如果我正确理解您的问题,您希望能够在单击“下一步”或“返回”时在问题之间滑动和/或具有滑动效果.如果是这种情况,我建议您将UILabel嵌入到UIScrollVIEw中.看一下这个:
class VIEwController: UIVIEwController,uiscrollviewdelegate { let content = ["Lorem","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,animated: true) } } func scrollVIEwDIDEndDecelerating(_ scrollVIEw: UIScrollVIEw) { index = round(scrollVIEw.contentOffset.x / scrollVIEw.frame.size.wIDth) }}
说明:
您基本上创建一个UIScrollVIEw来处理分页效果,并使用内容数组中的相应文本向每个页面添加UILabel.每次用户滚动到不同的页面时,索引都会更新为当前页面的索引.最后,当用户单击“下一步”或“返回”时,您将滚动到下一页或上一页
总结以上是内存溢出为你收集整理的ios – 在UINavigationController中控制向后滑动 *** 作全部内容,希望文章能够帮你解决ios – 在UINavigationController中控制向后滑动 *** 作所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)