我跟随raywenderlich tutorial使用UIKit制作了一个绘图应用。我现在正在尝试添加撤消最后一笔中断的功能。理想情况下,我想撤消最多10个笔画。我想弄清楚这样做的最佳方法是什么。我正在考虑创建另一个ImageView,它只有最后一个笔划,并在用户按下时生成ImageView.image = nil
。在教程的代码中有类似的东西。当触摸结束时,最新的笔划将合并到imageview上,所有旧笔划都在右侧不透明度。我不确定如何将这第三个(可能更多)imageivews添加到此代码中以使其工作。任何想法/更好的方式来解决这个问题? touchesEnded的代码如下。
代码
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
}
答案 0 :(得分:2)
@SpaceShroomies:我找到了/想出了一个很好的解决方案..
我使用了nsHiptser和Raywenderlich tutorial中的部分。
这是我的解决方案:
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 应该回到I.e. undo.image = Temp2Image.image
否则它不会改变&#34;它&#34;回来
答案 1 :(得分:0)
所以我设法让一个撤销功能向后退一步,通过一个UIImageView存储最后一个笔划,然后在下一个笔划完成时添加到其余笔划...使用时会出现问题倒退10步的技术相同?我需要10个UIImageViews ...这会效率低吗/导致崩溃吗?代码如下:
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
if !swiped {
// draw a single point
drawLineFrom(lastPoint, toPoint: lastPoint)
}
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)
undo1.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: kCGBlendModeNormal, alpha: 1.0)
mainImageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
undo1.image = nil
UIGraphicsBeginImageContext(undo1.frame.size)
undo1.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)
undo1.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
tempImageView.image = nil
}