- 在工程中添加Undo、Redo的按钮图片,每个按钮都有可用和不可用两种状态图片,共4个图片。
- 在StoryBoard中添加两个按钮,设置图片属性和自动布局约束。
- 按住 Ctrl 拖动按钮到 ViewController.mm 中,创建属性变量和动作方法。
@interface ViewController () @property (weak, nonatomic) IBOutlet UIButton *undoButton; @property (weak, nonatomic) IBOutlet UIButton *redoButton; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } - (IBAction)undo:(id)sender { } - (IBAction)redo:(id)sender { } @end
- 在 ViewController.mm 中导入
GiViewHelper.h
,在 viewDidLoad 中启用Undo功能,实现Undo/Redo按钮方法:- (void)viewDidLoad { [super viewDidLoad]; NSString *path = [LIBRARY_FOLDER stringByAppendingString:@"undo"]; [[GiViewHelper sharedInstance]startUndoRecord:path]; } - (IBAction)undo:(id)sender { [[GiViewHelper sharedInstance]undo]; } - (IBAction)redo:(id)sender { [[GiViewHelper sharedInstance]undo]; }
运行程序,画几个图,试试Undo/Redo按钮吧。虽然能工作了,但按钮可用状态未显示出来。
- 在 ViewController 中实现
GiPaintViewDelegate
观察者协议的onContentChanged:
方法,注册此观察者。@interface ViewController ()<GiPaintViewDelegate> ... - (void)viewDidLoad { [super viewDidLoad]; ... GiViewHelper *hlp = [GiViewHelper sharedInstance]; [hlp startUndoRecord:[LIBRARY_FOLDER stringByAppendingString:@"undo"]]; [hlp addDelegate:self]; [self onContentChanged:hlp.view]; } - (void)onContentChanged:(id)view { GiViewHelper *hlp = [GiViewHelper sharedInstance]; self.undoButton.enabled = [hlp canUndo]; self.redoButton.enabled = [hlp canRedo]; }
时间: 2024-10-24 11:38:10