【问题标题】:Move image to other view with double tap双击将图像移动到其他视图
【发布时间】:2016-12-09 02:17:15
【问题描述】:

我是 Swift 新手,我正在寻找一种通过双击将图像移动到其他视图控制器的方法。

我把我的图片做成了滚动视图,所以我可以滑动来查看它。

这是我的代码。

class Quotes: UIViewController, UIScrollViewDelegate {

   @IBOutlet weak var scroll: UIScrollView!

let imageview = ["quotes1","quotes2","quotes3","quotes4"]
var imagine = UIImageView()
override func viewDidLoad() {
    let tap = UITapGestureRecognizer(target: self, action: #selector(doubletap))
    tap.numberOfTapsRequired = 2
    view.addGestureRecognizer(tap)
    self.navigationController?.setNavigationBarHidden(true, animated: true)
    quotesimageload()
    }
func quotesimageload() {
    for index in 0 ... imageview.count - 1
    {
        imagine = UIImageView (frame:CGRect(x: self.scroll.frame.width * CGFloat(index), y: 0 , width: self.scroll.frame.width, height: self.scroll.frame.height))
        imagine.image = UIImage(named : imageview[index])
        imagine.tag = index
        imagine.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        self.scroll.addSubview(imagine)
    }
    self.scroll.contentSize = CGSize(width: self.scroll.frame.width * CGFloat(imageview.count), height: self.scroll.frame.height)
  }
func doubletap(){
    let view = self.storyboard?.instantiateViewController(withIdentifier: "pinch")
    self.navigationController?.pushViewController(view!, animated: true)

        }

}

【问题讨论】:

    标签: ios swift uiscrollview uiimageview uitapgesturerecognizer


    【解决方案1】:

    您不会将图像完全移动到另一个视图控制器 - 您将其副本传递给目标 VC。

    在第二个 VC 中设置一个“var” UIImage(假设您将其称为图像),然后在将该 VC 推送到视图中之前,从第一个 VC 中填充它。

    还有一点需要注意 - 将 UIViewController 命名为“视图”可能会非常令人困惑,因为许多人会认为它是 UIView。所以假设你把它重命名为 pinchViewController,完整的语法是:

    第二个VC:

    var image:UIView!
    

    第一个 VC:

    func doubletap(){
        let  pinchViewController = self.storyboard?.instantiateViewController(withIdentifier: "pinch")
        pinchViewController.image = imagine.image
        self.navigationController?.pushViewController(view!, animated: true)
    }
    

    如果你在 IB 中有正确编码和/或连接的东西,你应该在第二个 VC 中使用 UIImageView 来显示图像。

    【讨论】:

    • 感谢您的回答,在我这样做之后它对我有用 let pinchViewController = self.storyboard?.instantiateViewController(withIdentifier: "pinch") as! Viewcontroller2name..
    • 我真的很感激@dfd :D
    • 但是当我双击它时,随机图像显示在下一个视图控制器@dfd
    • 需要使用scrollview的selectedImageIndex来表示选择了哪张图片。抱歉,我忘记了那部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多