滑动以在新的viewcontroller中显示第二张图片

问题描述 投票:0回答:1

我的应用程序显示一个表视图,该视图将打开并在新的ViewController上显示图片。我能够将一组图片发送到新的控制器,但我无法想象如何滑动以显示第二张图片,该图片是发送的阵列的一部分。我的代码如下:

var firstchoice: [UIImage] = [
    UIImage(named: "Appa1")!,
    UIImage(named: "Appa2")!
]




func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
    ///Right way here
    ///You can easily manage using this
    let Vc = self.storyboard?.instantiateViewController(withIdentifier: "imageViewController") as! imageViewController



    ///Here you have written four Animal names in Array
    ///So There is going to four case 0,1,2,3 and a default case
    switch indexPath.row
    {
    case 0:
        Vc.passedArray = firstchoice
        self.navigationController?.pushViewController(Vc, animated: true)
        break;

imageViewController代码:

class imageViewController: UIViewController,GADBannerViewDelegate, UIGestureRecognizerDelegate, UIScrollViewDelegate {

    var bannerView: GADBannerView!



    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var myImageView: UIImageView!


    @IBAction func pictureSwipe(sender: UISwipeGestureRecognizer) {

        let pictureString:String = self.passedArray[index]
        self.myImageView.image = passedArray.first

        index = (index < passedArray.count-1) ? index+1 : 0
    }

    var passedImage : UIImage! = nil
    var passedArray : [UIImage]!

    var index = 0

    override func viewDidLoad(){
        super.viewDidLoad()
        self.myImageView.image = passedArray.first
        self.navigationController?.navigationBar.isHidden = false

        scrollView.minimumZoomScale = 1.0
        scrollView.maximumZoomScale = 5.0
ios swift uitableview uiimageview
1个回答
1
投票

您可以将第二个视图控制器设置为内置swiping功能的UIPageController。然后,您只需为所选图像提供图像数组和currentIndex。然后,用户可以滑过其他相关图像。如果愿意,您还可以添加UIPageControl,以向用户显示有更多可用图像。

在代码中的didSelect方法中,我建议进行一些改进:

  1. 在switch语句之后调用self.navigationController?.pushViewController(Vc, animated: true)以防止代码重复
  2. 使用数组作为图像,然后您只需在indexPath.row中选择图像
  3. 一旦有了这个数组,你就可以将整个数组和indexPath传递给你的第二个vc
© www.soinside.com 2019 - 2024. All rights reserved.