向绘图应用程序添加撤消功能 [英] Adding undo function to drawing app
问题描述
我遵循了 raywenderlich教程,制作绘图应用程序的UIKit。我现在正在尝试添加功能以撤消上一个笔划。理想情况下,我想撤销最多10个笔画。我正在尝试找出执行此操作的最佳方法。我正在考虑创建另一个仅具有最后笔触的ImageView,并在用户按下时使 ImageView.image = nil
。在本教程的代码中,有类似的内容。触摸结束时,最新笔画将合并到imageview中,所有旧笔画均以正确的不透明度进行合并。我不太确定如何在此代码中添加第三个(甚至可能更多)图像库以使其正常工作。有什么想法/更好的方法吗?触摸代码的结尾如下。
I followed a raywenderlich tutorial on using the UIKit to make a drawing app. I'm now trying to add in the functionality to undo the last stroke. Ideally I would like to undo up to 10ish strokes. I'm trying to figure out what is the best way to go about doing this. I was thinking of creating another ImageView which has only the last stroke and making the ImageView.image = nil
when the user presses back. In the code from the tutorial there's something similar to this. When the touches end, the newest stroke is merged onto the imageview with all of the old ones at the right opacity. I'm not really sure how I could add this third (and potentially more) imageivews to this code to make it work. Any ideas / a better way to go about this? Code for touchesEnded is below.
代码
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
if !swiped {
// draw a single point
drawLineFrom(lastPoint, toPoint: lastPoint)
}
// Merge tempImageView into mainImageView
UIGraphicsBeginImageContext(mainImageView.frame.size)
mainImageView.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: kCGBlendModeNormal, alpha: 1.0)
tempImageView.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: kCGBlendModeNormal, alpha: opacity)
mainImageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
tempImageView.image = nil
}
推荐答案
@SpaceShroomies:我发现/提出了一个很好的解决方案。
@SpaceShroomies: I found/came up with a great solution..
我使用了 nsHiptser 和 Raywenderlich教程。
这是我的解决方案:
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
//undoManager
let undo: UIImageView = undoManager?.prepareWithInvocationTarget(Temp2Image) as! UIImageView
UIGraphicsBeginImageContext(Temp2Image.frame.size)
Temp2Image.image?.drawInRect(CGRect(x: 0, y: 0, width: Temp2Image.frame.size.width, height: Temp2Image.frame.size.height), blendMode: kCGBlendModeNormal, alpha: 1.0)
TempImage.image?.drawInRect(CGRect(x: 0, y: 0, width: TempImage.frame.size.width, height: TempImage.frame.size.height), blendMode: kCGBlendModeNormal, alpha: opacity)
undo.image = Temp2Image.image
Temp2Image.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
TempImage.image = nil
}
这里重要的是设置,什么 UNDO 应该回到Ie undo.image = Temp2Image.image
否则它不会将 it改回
The important thing here is to set, what UNDO should go back to I.e. undo.image = Temp2Image.image
otherwise it will not change "it" back
这篇关于向绘图应用程序添加撤消功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!