小编Chr*_*bin的帖子

在Swift 2中的绘图应用程序中创建“撤消”功能

我正在尝试在Swift 2中创建绘图应用程序,并且在尝试实现“撤消”按钮时碰到了墙。我对此很陌生(实际上是非常新的),并且尽了最大的努力弄清楚了,但是我发现的所有其他示例都使用不同的版本或语言,或者根本不适用。也许是由于我缺乏知识阻碍了我在这里的进步。

我已经尝试过使用“ undoManager”,但是我不知道如何使用它。我尝试阅读可用的在线指南,但仍然一无所知!

这是我的按钮:

@IBAction func undoDrawing(sender: AnyObject) {
}
Run Code Online (Sandbox Code Playgroud)

我已经建立了touchesBegan

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?){
        isSwiping    = false
    if let touch = touches.first{
        lastPoint = touch.locationInView(imageView)
    }
}
Run Code Online (Sandbox Code Playgroud)

touchesMoved

override func touchesMoved(touches: Set<UITouch>,
                           withEvent event: UIEvent?){
    isSwiping = true;
    if let touch = touches.first{

        let currentPoint = touch.locationInView(imageView)
        UIGraphicsBeginImageContext(self.imageView.frame.size)
        self.imageView.image?.drawInRect(CGRectMake(0, 0, self.imageView.frame.size.width, self.imageView.frame.size.height))
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y)
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y)
        CGContextSetLineCap(UIGraphicsGetCurrentContext(),CGLineCap.Round)
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), myCGFloat)
        CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), self.selectedColor.CGColor)
        CGContextStrokePath(UIGraphicsGetCurrentContext())
        self.imageView.image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        lastPoint = currentPoint
    } …
Run Code Online (Sandbox Code Playgroud)

core-graphics ios swift

2
推荐指数
1
解决办法
1741
查看次数

标签 统计

core-graphics ×1

ios ×1

swift ×1