我的应用程序显示一个 TableView ,该 View 将在新的 ViewController 上打开并显示图片。我可以将一组图片发送到新 Controller ,但我不知道如何滑动以显示作为已发送数组一部分的第二张图片。我的代码如下:
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
最佳答案
您可以将第二个 View Controller 设置为 UIPageController,它内置了 swiping
功能。然后您只需提供一组图像和一个 currentIndex
用于所选图像.然后用户可以滑动浏览其他相关图像。如果愿意,您还可以添加 UIPageControl
,以向用户显示有更多可用图像。
在您代码的 didSelect 方法中,我建议进行一些改进:
- 在 switch 语句之后调用
self.navigationController?.pushViewController(Vc, animated: true)
防止代码重复 - 为图像使用数组,然后您只需在 indexPath.row 中选择图像
- 一旦你有了这个数组,你就可以将整个数组和 indexPath 传递给你的第二个 vc
关于ios - 滑动以在新的 View Controller 中显示第二张图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52359386/