当前位置:  开发笔记 > 编程语言 > 正文

在UINavigationController中控制向后滑动操作

如何解决《在UINavigationController中控制向后滑动操作》经验,为你挑选了1个好方法。

我正在创建一个简单的闪存卡应用程序,如下图所示:

闪存卡没有动画

我想要向后滑动,就像这样:

闪存卡动画

要做到这一点,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

如果我正确理解您的问题,您希望能够在单击"下一步"或"返回"时在问题之间滑动和/或具有滑动效果.如果是这样的话,我建议你嵌入您UILabelUIScrollView.看一下这个:

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都会更新到当前页面的索引.最后,当用户单击"下一步"或"返回"时,您将滚动到下一页或上一页



1> ThunderStruc..:

如果我正确理解您的问题,您希望能够在单击"下一步"或"返回"时在问题之间滑动和/或具有滑动效果.如果是这样的话,我建议你嵌入您UILabelUIScrollView.看一下这个:

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都会更新到当前页面的索引.最后,当用户单击"下一步"或"返回"时,您将滚动到下一页或上一页

推荐阅读
农大军乐团_697
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有