iOS繪圖例2:增加Undo/Redo功能

來源:互聯網
上載者:User

標籤:http   io   os   java   ar   for   art   cti   on   

  1. 在工程中添加Undo、Redo的按鈕圖片,每個按鈕都有可用和不可用兩種狀態圖片,共4個圖片。

  2. 在StoryBoard中添加兩個按鈕,設定圖片屬性和自動布局約束。

  3. 按住 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
  4. 在 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按鈕吧。雖然能工作了,但按鈕可用狀態未顯示出來。

  5. 在 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];}

範例代碼見 GitHub 或 OSC,歡迎 Fork。

iOS繪圖例2:增加Undo/Redo功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.