2017-01-22 7 views
1

Swift 3.02(Xcode 8.2)を使用して、回答:https://stackoverflow.com/a/31450475/7448394に記載された元に戻す解を参照します。redoが機能しない(ios swift undoManager、Invocation)

以下のコードでは、このソリューションを使用して図面の取り消しとやり直しの両方をテストしています。 randomDrawingのボタンを押すと、0からランダムな点まで赤い線が引かれます。 この行を元に戻すボタンを押すか、最後の行が消えます。 しかし、私がredo-buttonを押すと、imageViewに何も起こりません。 明らかに、元に戻す機能が働いていますが、元に戻す操作の後にtrueに変わるundoManager?.canRedoもチェックしましたが、なぜこの再実行で結果が表示されないのですか?

import UIKit 

クラスのViewController:のUIViewController良い対策のために{

@IBOutlet var imageView: UIImageView! 

override func viewDidLoad() { 
    super.viewDidLoad() 
} 
override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
} 
@IBAction func undoDrawing(_ sender: AnyObject) { 
    undoManager?.undo() 
} 
@IBAction func redoDrawing(_ sender: AnyObject) { 
    undoManager?.redo() 
} 
@IBAction func randomDrawing(_ sender: AnyObject) { 
    let undo: UIImageView = undoManager?.prepare(withInvocationTarget: imageView) as! UIImageView 

    UIGraphicsBeginImageContext(self.imageView.frame.size) 
     self.imageView.image?.draw(in: CGRect(x: 0, y: 0, width: self.imageView.frame.size.width, height: self.imageView.frame.size.height)) 

     UIGraphicsGetCurrentContext()?.move(to: CGPoint(x: 0, y: 0))     
     UIGraphicsGetCurrentContext()?.addLine(to: CGPoint(x: Int(arc4random_uniform(100) + 100), y: Int(arc4random_uniform(100) + 100))) 
     UIGraphicsGetCurrentContext()?.setLineWidth(10.0) 
     UIGraphicsGetCurrentContext()?.setStrokeColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0) 
     UIGraphicsGetCurrentContext()?.strokePath() 

     undo.image = imageView.image 
     self.imageView.image = UIGraphicsGetImageFromCurrentImageContext() 

    UIGraphicsEndImageContext() 
} 

}

答えて

0
  1. 、あなたのREDOボタンが正しくInterface Builderで設定し、それはあなたをトリガーすることをされていることを確認redoDraving方法。

  2. 私はあなたと同じ問題がありました(再実行は起こりません)。 This answerが私を助けました。

関連する問題