Xcode Swift: 无法关闭弹出图像
Xcode Swift: Cannot close popup Image
我创建了一个 ViewController
,它通过 JSON 解析三个单独的 UIImageViews
来显示三个图像和其他信息。当您单击任何图像时,它会将您带到另一个 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)
}
}
这是我看到的问题,如果您 "popping" 到根视图控制器,这意味着您必须将视图控制器推送到导航控制器的堆栈上,但我没有看到您将任何内容推送到导航堆栈。当然,除非出于某种原因 Apple 决定取消推送视图控制器,但我怀疑情况是否如此。因此,我在您的代码中看到的内容还有另一个问题。您只是通过呈现视图控制器来呈现视图控制器,如果您调用
self.navigationController?.popToRootViewControllerAnimated(true)
那么导航控制器将不会从堆栈中删除堆栈中的任何内容,因为您将 viewcontroller 模态显示在另一个 viewcontroller 上,而没有在视图控制器的导航控制器中显示模态。
解决方案,也许,但这不是 100%,因为我没有你的代码在我面前。
改变这个:
self.navigationController?.popToRootViewControllerAnimated(true)
像这样
self.dismissViewControllerAnimated(animated: true, completion:nil)
我不这样做 swift 所以我的解决方案是伪代码,请随意添加问号以及 Apple 出于某种原因认为没有价值的内容。
您也可以将您的演示文稿更改为:
self.navigationController?.presentViewController(controller, animated: true, completion: nil)
同样,上面是伪代码,但我想我把问号放在了正确的位置,这样它就可以完成它应该做的事情
此外,您可以参考这个,尽管 Apple 并没有真正彻底地告诉您高级导航堆栈的工作原理:
有时您可能需要同时拥有 4-10 个导航控制器 运行,因此请确保您了解它们如何与视图控制器交互,并确保您了解 POP、PUSH 和 PRESENT 的作用.祝你好运,祝你有美好的一天。
在关闭缩放中我认为你应该只使用
@IBAction func closeZoom(sender: AnyObject) {
dismissViewControllerAnimated(true, completion: nil)
}
因为您展示了那个 View Controller,所以当您推送它时会使用 popToRootViewControllerAnimated(true)
我创建了一个 ViewController
,它通过 JSON 解析三个单独的 UIImageViews
来显示三个图像和其他信息。当您单击任何图像时,它会将您带到另一个 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)
}
}
这是我看到的问题,如果您 "popping" 到根视图控制器,这意味着您必须将视图控制器推送到导航控制器的堆栈上,但我没有看到您将任何内容推送到导航堆栈。当然,除非出于某种原因 Apple 决定取消推送视图控制器,但我怀疑情况是否如此。因此,我在您的代码中看到的内容还有另一个问题。您只是通过呈现视图控制器来呈现视图控制器,如果您调用
self.navigationController?.popToRootViewControllerAnimated(true)
那么导航控制器将不会从堆栈中删除堆栈中的任何内容,因为您将 viewcontroller 模态显示在另一个 viewcontroller 上,而没有在视图控制器的导航控制器中显示模态。
解决方案,也许,但这不是 100%,因为我没有你的代码在我面前。
改变这个:
self.navigationController?.popToRootViewControllerAnimated(true)
像这样
self.dismissViewControllerAnimated(animated: true, completion:nil)
我不这样做 swift 所以我的解决方案是伪代码,请随意添加问号以及 Apple 出于某种原因认为没有价值的内容。
您也可以将您的演示文稿更改为:
self.navigationController?.presentViewController(controller, animated: true, completion: nil)
同样,上面是伪代码,但我想我把问号放在了正确的位置,这样它就可以完成它应该做的事情
此外,您可以参考这个,尽管 Apple 并没有真正彻底地告诉您高级导航堆栈的工作原理:
有时您可能需要同时拥有 4-10 个导航控制器 运行,因此请确保您了解它们如何与视图控制器交互,并确保您了解 POP、PUSH 和 PRESENT 的作用.祝你好运,祝你有美好的一天。
在关闭缩放中我认为你应该只使用
@IBAction func closeZoom(sender: AnyObject) {
dismissViewControllerAnimated(true, completion: nil)
}
因为您展示了那个 View Controller,所以当您推送它时会使用 popToRootViewControllerAnimated(true)