Xcode Swift:无法关闭弹出图像

Posted

技术标签:

【中文标题】Xcode Swift:无法关闭弹出图像【英文标题】:Xcode Swift: Cannot close popup Image 【发布时间】:2015-08-15 23:49:08 【问题描述】:

我创建了一个ViewController,它通过三个独立的UIImageViews 通过JSON 解析显示三个图像和其他信息。当您单击任何图像时,它会将您带到另一个ViewController,在背景中弹出一个UIScrollView,一个链接到所有三个图像的UIImageView,以及一个将关闭弹出窗口的Button。向上ViewController 并将其恢复到上一个​​。这是screenshot。我遇到的问题是我添加了这段代码:

func removeZoom()
    
        UIView.animateWithDuration(0.25, animations: 
            self.view.transform = CGAffineTransformMakeScale(1.3, 1.3)
            self.view.alpha = 0.0;
            , completion:(finished : Bool)  in
                if (finished)
                
                    self.view.removeFromSuperview()
                
        );
    

    @IBAction func closeZoom(sender: AnyObject) 

        self.navigationController?.popToRootViewControllerAnimated(true)

    

当我尝试点击关闭按钮时,什么也没有发生。不知道我错过了什么。任何指导都会有所帮助。

这里我会放两个控制器的代码:

JnsDetail.swift

import Foundation
import UIKit

class JnsDetail: UIViewController 

    @IBOutlet var tituloLabel : UILabel!
    @IBOutlet var marcaLabel : UILabel!
    @IBOutlet var colorLabel : UILabel!
    @IBOutlet var tipoLabel : UILabel!
    @IBOutlet var refLabel : UILabel!

    @IBOutlet var imageView : UIImageView!
    @IBOutlet var imageView2 : UIImageView!
    @IBOutlet var imageView3 : UIImageView!

    @IBOutlet var backbutton : UIButton!

    var jsonextrct : JsonExtrct!

    var photos : [String]!

    var transitionOperator = TransitionOperator()

    override func viewDidLoad() 
        super.viewDidLoad()

        //titulo = jsonextrct.titulo

        tituloLabel.font = UIFont(name: mTheme.fontName, size: 21)
        tituloLabel.textColor = UIColor.blackColor()
        tituloLabel.text = jsonextrct.titulo

        //marca = jsonextrct.marca

        marcaLabel.font = UIFont(name: mTheme.fontName, size: 21)
        marcaLabel.textColor = UIColor.blackColor()
        marcaLabel.text = jsonextrct.marca

        //color = jsonextrct.color

        colorLabel.font = UIFont(name: mTheme.fontName, size: 21)
        colorLabel.textColor = UIColor.blackColor()
        colorLabel.text = jsonextrct.color

        //tipo = jsonextrct.tipo

        tipoLabel.font = UIFont(name: mTheme.fontName, size: 21)
        tipoLabel.textColor = UIColor.blackColor()
        tipoLabel.text = jsonextrct.tipo

        //ref = jsonextrct.ref

        refLabel.font = UIFont(name: mTheme.fontName, size: 21)
        refLabel.textColor = UIColor.blackColor()
        refLabel.text = "\(jsonextrct.ref)"

        if let imageData = jsonextrct.imageData 
            imageView.image = UIImage(data: imageData)
        else
            Utils.asyncLoadJsonImage(jsonextrct, imageView: imageView)
        
        //topImageViewHeightConstraint.constant = 240

        imageView.layer.borderColor = UIColor(white: 0.2, alpha: 1.0).CGColor
        imageView.layer.borderWidth = 0.5

        if let imageData2 = jsonextrct.imageData2 
            imageView2.image = UIImage(data: imageData2)
        else
            Utils.asyncLoadJsonImage(jsonextrct, imageView2: imageView2)
        

        imageView2.layer.borderColor = UIColor(white: 0.2, alpha: 1.0).CGColor
        imageView2.layer.borderWidth = 0.5

        if let imageData3 = jsonextrct.imageData3 
            imageView3.image = UIImage(data: imageData3)
        else
            Utils.asyncLoadJsonImage(jsonextrct, imageView3: imageView3)
        

        imageView3.layer.borderColor = UIColor(white: 0.2, alpha: 1.0).CGColor
        imageView3.layer.borderWidth = 0.5

        var tapGestureZoom = UITapGestureRecognizer(target: self, action: "zoomJns:")
        tapGestureZoom.numberOfTapsRequired = 1
        tapGestureZoom.numberOfTouchesRequired = 1
        imageView.userInteractionEnabled = true
        imageView.addGestureRecognizer(tapGestureZoom)

        var tapGestureZoom2 = UITapGestureRecognizer(target: self, action: "zoomJns2:")
        tapGestureZoom2.numberOfTapsRequired = 1
        tapGestureZoom2.numberOfTouchesRequired = 1
        imageView2.userInteractionEnabled = true
        imageView2.addGestureRecognizer(tapGestureZoom2)

        var tapGestureZoom3 = UITapGestureRecognizer(target: self, action: "zoomJns3:")
        tapGestureZoom3.numberOfTapsRequired = 1
        tapGestureZoom3.numberOfTouchesRequired = 1
        imageView3.userInteractionEnabled = true
        imageView3.addGestureRecognizer(tapGestureZoom3)

    

    override func preferredStatusBarStyle() -> UIStatusBarStyle 
        return UIStatusBarStyle.Default
    

    func backTapped(sender: AnyObject?)
        dismissViewControllerAnimated(true, completion: nil)
    

    @IBAction func zoomJns(sender: AnyObject?)
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewControllerWithIdentifier("JnsZoomController") as! JnsZoomController
        self.modalPresentationStyle = UIModalPresentationStyle.Custom
        controller.transitioningDelegate = transitionOperator
        controller.jsonextrct = jsonextrct

        presentViewController(controller, animated: true, completion: nil)
    

    @IBAction func zoomJns2(sender: AnyObject?)
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewControllerWithIdentifier("JnsZoomController") as! JnsZoomController
        self.modalPresentationStyle = UIModalPresentationStyle.Custom
        controller.transitioningDelegate = transitionOperator
        controller.jsonextrct = jsonextrct

        presentViewController(controller, animated: true, completion: nil)
    

    @IBAction func zoomJns3(sender: AnyObject?)
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewControllerWithIdentifier("JnsZoomController") as! JnsZoomController
        self.modalPresentationStyle = UIModalPresentationStyle.Custom
        controller.transitioningDelegate = transitionOperator
        controller.jsonextrct = jsonextrct

        presentViewController(controller, animated: true, completion: nil)
    


JnsZoomController.swift

import Foundation
import UIKit

class JnsZoomController : UIViewController 

    @IBOutlet var scrollView : UIScrollView!
    @IBOutlet var jnsImageView : UIImageView!
    @IBOutlet var jnsImageView2 : UIImageView!
    @IBOutlet var jnsImageView3 : UIImageView!

    var jsonextrct : JsonExtrct!

    override func viewDidLoad() 
        super.viewDidLoad()

        if let imageData = jsonextrct.imageData 

            let image = UIImage(data: imageData)
            jnsImageView.image = UIImage(data: imageData)
            //jnsImageView.bounds = CGRectMake(0, 0, image?.size.width, image?.size.height);
        

        if let imageData2 = jsonextrct.imageData2 

            let image2 = UIImage(data: imageData2)
            jnsImageView2.image = UIImage(data: imageData2)
            //jnsImageView2.bounds = CGRectMake(0, 0, image?.size.width, image?.size.height);
        

        if let imageData3 = jsonextrct.imageData3 

            let image3 = UIImage(data: imageData3)
            jnsImageView3.image = UIImage(data: imageData3)
            //jnsImageView3.bounds = CGRectMake(0, 0, image?.size.width, image?.size.height);
        

        scrollView.contentSize = jnsImageView.frame.size
        scrollView.contentSize = jnsImageView2.frame.size
        scrollView.contentSize = jnsImageView3.frame.size

    

    func removeZoom()
    
        UIView.animateWithDuration(0.25, animations: 
            self.view.transform = CGAffineTransformMakeScale(1.3, 1.3)
            self.view.alpha = 0.0;
            , completion:(finished : Bool)  in
                if (finished)
                
                    self.view.removeFromSuperview()
                
        );
    

    @IBAction func closeZoom(sender: AnyObject) 

        self.navigationController?.popToRootViewControllerAnimated(true)

    

【问题讨论】:

【参考方案1】:

这是我看到的问题,如果您“弹出”到根视图控制器,这意味着您必须将视图控制器推送到导航控制器的堆栈上,并且我没有看到您将任何内容推送到导航堆栈上。当然,除非出于某种原因,Apple 决定取消 Pushing 视图控制器,但我怀疑情况是否如此。所以,我在你的代码中看到的还有另一个问题。您只是通过展示一个视图控制器来展示视图控制器,如果您调用,我看不到您在哪里通过使用导航控制器展示视图控制器来展示视图控制器。

     self.navigationController?.popToRootViewControllerAnimated(true)

那么堆栈上没有导航控制器要从堆栈中删除的任何内容,因为您以模态方式将视图控制器呈现在另一个视图控制器之上,而没有在视图控制器的导航控制器中呈现模态。

可能是解决方案,但这不是 100%,因为我面前没有您的代码。

改变这个:

     self.navigationController?.popToRootViewControllerAnimated(true)

这样的事情

self.dismissViewControllerAnimated(animated: true, completion:nil)

我不快速,所以我的解决方案是伪代码,请随意添加问号,以及 Apple 出于某种原因认为没有价值的东西。

您也可以将演示文稿更改为:

self.navigationController?.presentViewController(controller, animated: true, completion: nil)

同样,上面是伪代码,但我认为我将问号放在正确的位置,以便它完成它应该做的事情

此外,您可以参考这里,尽管 Apple 并没有真正彻底地告诉您高级导航堆栈的工作原理:

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/#//apple_ref/occ/instm/UINavigationController/pushViewController:animated:

有时您可能需要同时运行 4-10 个导航控制器,因此请确保您了解它们如何与视图控制器交互,并确保您了解 POP、PUSH 和 PRESENT 的作用。祝你好运,祝你有美好的一天。

【讨论】:

我去看看有没有我们遗漏的东西 我现在看到的另一个问题可能是视图控制器底部动画中的“self.view.removeFromSuperview()”,除非您先从视图中删除任何内容如果离开导航堆栈的顶部,则弹出或关闭它。我的意思是视图控制器 就我所见,他根本没有使用self.view.removeFromSuperview(),也许这是一种失败的方法,但无论如何不要这样做:P 就在底部附近,距离他的帖子底部大约 10 行 成功了!将其更改为 self.navigationController?.popToRootViewControllerAnimated(true)self.dismissViewControllerAnimated(true, completion:nil) 并从另一个文件中删除了一些其他代码,并且运行良好。我还删除了removeZoom 方法。谢谢!【参考方案2】:

在closeZoom中我认为你应该只使用

@IBAction func closeZoom(sender: AnyObject) 

    dismissViewControllerAnimated(true, completion: nil)


因为你提交了那个 View Controller,所以你推送的时候会用到 popToRootViewControllerAnimated(true)

【讨论】:

谢谢!打算试一试。

以上是关于Xcode Swift:无法关闭弹出图像的主要内容,如果未能解决你的问题,请参考以下文章

SQL fetch 数组中的弹出图像显示相同的图像

R中DT悬停时弹出图像

elFinder 文件管理器防止双击时弹出图像

Gridview - 单击时在另一个框中弹出图像

JavaScript 调整弹出图像尺寸的大小

PHP Javascript - 弹出图像redimensionado desde padre。