• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 在 Swift 中使用 UIPageViewController 直接跳转到特定页面

[复制链接]
菜鸟教程小白 发表于 2022-12-11 17:07:35 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在使用 UIPageViewController 来滑动图像。 View Controller 中有一组小图像,然后我转到另一个仅显示较大图像的 View Controller 。我创建了一个带有 ScrollView 的页面 View Controller ,它工作正常,但它总是从第一张图像开始。假设第一个 Controller 有五个小图像,假设用户点击第三个图像以查看更大尺寸的图像,则第二个 Controller 显示更大图像的segue成功完成,当前页面也设置为3但图像是第一个图像。当我使用第二个 Controller 时,我传递包含图像 url 的数组,并根据在第一个 View 上单击的索引设置当前页面,但图像是数组中的第一个图像。

enter image description here

enter image description here

我该如何解决这个问题?下面是代码:

import UIKit

class ImageViewerViewController: UIViewController, UIScrollViewDelegate {

    @IBOutlet weak var scrollView: UIScrollView!

    @IBOutlet weak var pageControl: UIPageControl!

    var seguedAlbumImagePathArray=[]
    var seguedCurrentImageIndex: Int?


    var totalPages: Int = 0

    //let sampleBGColors: Array<UIColor> = [UIColor.redColor(), UIColor.yellowColor(), UIColor.greenColor(), UIColor.magentaColor(), UIColor.orangeColor()]

    override func shouldAutorotate() -> Bool {
        return false
    }

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.Portrait
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        totalPages = seguedAlbumImagePathArray.count
        view.bringSubviewToFront(pageControl)
    }


    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        configureScrollView()
        configurePageControl()
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        return UIStatusBarStyle.LightContent
    }


    // MARK: Custom method implementation

    func configureScrollView() {
        // Enable paging.
        scrollView.pagingEnabled = true

        // Set the following flag values.
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.scrollsToTop = false

        // Set the scrollview content size.
        scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * CGFloat(totalPages), scrollView.frame.size.height)

        // Set self as the delegate of the scrollview.
        scrollView.delegate = self

        // Load the ImageView view from the ImageView.xib file and configure it properly.
        for i in 0 ..< totalPages {
            // Load the ImageView view.
            let imageView = NSBundle.mainBundle().loadNibNamed("ImageView", owner: self, options: nil)[0] as! UIView

            imageView.backgroundColor = UIColor.blackColor()
            // Set its frame and the background color.
            imageView.frame = CGRectMake(CGFloat(i) * scrollView.frame.size.width, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height)
            //imageView.backgroundColor = sampleBGColors[i]

            // Set the proper message to the test view's label.
            let label = imageView.viewWithTag(1) as! UILabel
            label.text = "\(i + 1)/\(totalPages)"

            // Set the proper message to the test view's label.
            let imagev = imageView.viewWithTag(2) as! UIImageView
            view.sendSubviewToBack(imagev)

            if let imageData = NSUserDefaults.standardUserDefaults().objectForKey(seguedAlbumImagePathArray[i] as! String),
                let imageToDisplay = UIImage(data: imageData as! NSData) {
                imagev.image = imageToDisplay
                view.sendSubviewToBack(imagev)
            }
            else {
                ImageLoader.sharedLoader.imageForUrl(seguedAlbumImagePathArray[i] as! String, completionHandler:{(image: UIImage?, url: String) in
                    imagev.image = image
                })
            }

            // Add the test view as a subview to the scrollview.
            scrollView.addSubview(imageView)
        }
    }


    func configurePageControl() {
        // Set the total pages to the page control.
        pageControl.numberOfPages = totalPages

        // Set the initial page.
        pageControl.currentPage = seguedCurrentImageIndex!
    }


    // MARK: UIScrollViewDelegate method implementation

    func scrollViewDidScroll(scrollView: UIScrollView) {
        // Calculate the new page index depending on the content offset.
        let currentPage = floor(scrollView.contentOffset.x / UIScreen.mainScreen().bounds.size.width);

        // Set the new page index to the page control.
        pageControl.currentPage = Int(currentPage)
    }


    // MARK: IBAction method implementation

    @IBAction func changePage(sender: AnyObject) {
        // Calculate the frame that should scroll to based on the page control current page.
        var newFrame = scrollView.frame
        newFrame.origin.x = newFrame.size.width * CGFloat(pageControl.currentPage)
        scrollView.scrollRectToVisible(newFrame, animated: true)

    }
}



Best Answer-推荐答案


好的,我找到了出路,我可以使用滚动分页来达到预期的效果,这是我用来转到 ScrollView 的特定页面的代码:

var frame: CGRect = scrollView.frame
frame.origin.x = frame.size.width * CGFloat(seguedCurrentImageIndex!)
frame.origin.y = 0
scrollView.scrollRectToVisible(frame, animated: false)

关于ios - 在 Swift 中使用 UIPageViewController 直接跳转到特定页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38621665/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap