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

时间:2018-09-17 00:07:23

标签: ios swift uitableview uiimageview

我的应用程序显示一个表格视图,该表格视图将打开并在新的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

1 个答案:

答案 0 :(得分:1)

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

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

  1. 在switch语句后调用self.navigationController?.pushViewController(Vc, animated: true),以防止代码重复
  2. 对图像使用数组,然后可以在indexPath.row上选择图像
  3. 一旦有了该数组,您就可以将整个数组和indexPath传递给第二个vc