【问题标题】:Setting UIImageView to nil将 UIImageView 设置为 nil
【发布时间】:2015-07-08 01:12:16
【问题描述】:

我正在尝试为我的简单绘图应用程序清除绘图画布。我创建了一个按钮,通过将UIImageView.image 设为 nil 来删除图像。但是,按下按钮时没有任何反应。 ViewController 代码:

import UIKit

var smoothLineView : SmoothLineView = SmoothLineView()

class ViewController: UIViewController {

@IBOutlet weak var mainDrawingCanvas: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    smoothLineView = SmoothLineView(frame: mainDrawingCanvas.bounds)
    self.view.addSubview(smoothLineView)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func deleteCurrentCanvas(sender: AnyObject) {
    mainDrawingCanvas.image = nil
}

}

完整的项目也可以找到:https://github.com/BananaSplitio/OpenSketch

【问题讨论】:

标签: ios swift uiimageview core cgcontext


【解决方案1】:

不完全确定您要在这里做什么。但是,这就是我认为正在发生的事情。

您似乎只使用图像的边界 (mainDrawingCanvas) 来制作另一个视图 (smoothlineView)。无论您之后对图像做什么,都没有关系,因为图像不是您查看的一部分。您的视图是 smoothlineView,而不是 mainDrawingCanvas。

要删除/删除图像(我假设是smoothlineView),尝试这样做,它应该可以工作。

import UIKit

var smoothLineView : SmoothLineView = SmoothLineView() 

class ViewController: UIViewController {

    @IBOutlet weak var mainDrawingCanvas: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        smoothLineView = SmoothLineView(frame: mainDrawingCanvas.bounds)
        self.view.addSubview(smoothLineView)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func deleteCurrentCanvas(sender: AnyObject) {
        smoothLineView.removeFromSuperview()
    }

}

【讨论】:

  • 我试过你的建议,但结果是一样的。我按照 NSDeveloper 的建议将背景颜色更改为蓝色。按钮消失。我只能看到它,因为背景很清楚。它正在被绘制
【解决方案2】:

你将smoothLineView添加到self.view,那么按钮将不会收到事件。将smoothLineView设置为蓝色,你就会明白为什么它不起作用了。

你需要做一个按钮的IBOutlet,在addsubviewsmoothLineView之后添加如下代码,

self.view.bringSubviewToFront(button)

然后您需要删除您忘记删除的按钮的一些 IBAction。 之后,您需要在 deleteCurrentCanvas 方法中更改您的清除操作。

【讨论】:

  • 这很有意义!这样,我实际上可以单击按钮并更改为 removeFromSuperview() 确实会删除画布。但是,由于子视图已被删除,它不会让您再次绘制
  • 你需要提供一个明确的方法,让smoothLineView不要删除它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-20
  • 2010-12-13
相关资源
最近更新 更多