gpt4 book ai didi

ios - iPad 撤消按钮(a-la Keynote 和其他应用程序)

转载 作者:可可西里 更新时间:2023-11-01 05:42:20 24 4
gpt4 key购买 nike

在 Keynote(和其他应用程序)中,我注意到执行撤消/重做的“标准”界面是在工具栏上提供撤消按钮。

单击按钮(始终启用)撤消最近的操作。(如果没有最近的操作要撤销,它会显示撤销/重做菜单)。

长按撤消按钮打开撤消/重做菜单。

我搜索了实现它的方法,目前我找到的最佳答案是 following link .

不知有没有人知道更简单的方法?

谢谢!

最佳答案

在查看所有方法并与 friend 讨论后,下面是我使用的解决方案,对于 UIBarButtonItem,它响应点击和长按 (TapOrLongPressBarButtonItem)。

它基于以下原则:

  1. 子类 UIBarButtonItem
  2. 使用自定义 View (因此处理长按真的很简单 - 因为我们的自定义 View 可以毫无问题地响应长按手势处理程序...)

... 到目前为止 - 这种方法在 other SO thread 中- 我不喜欢这种方法,因为我找不到足够简单的方法来使自定义 View 看起来像 iPad 导航栏按钮......太棒了......

使用 UIGlossyButton作者 Water Lou(感谢水!)。这种使用被封装在子类中...

结果代码如下:

@protocol TapOrPressButtonDelegate;
@interface TapOrPressBarButtonItem : UIBarButtonItem {
UIGlossyButton* _tapOrPressButton;
__weak id<TapOrPressButtonDelegate> _delegate;
}
- (id)initWithTitle:(NSString*)title andDelegate:(id<TapOrPressButtonDelegate>)delegate;
@end

@protocol TapOrPressButtonDelegate<NSObject>
- (void)buttonTapped:(UIButton*)button withBarButtonItem:(UIBarButtonItem*)barButtonItem;
- (void)buttonLongPressed:(UIButton*)button withBarButtonItem:(UIBarButtonItem*)barButtonItem;
@end

@implementation TapOrPressBarButtonItem
- (void)buttonLongPressed:(UILongPressGestureRecognizer*)gesture {
if (gesture.state != UIGestureRecognizerStateBegan)
return;
if([_delegate respondsToSelector:@selector(buttonLongPressed:withBarButtonItem:)]) {
[_delegate buttonLongPressed:_tapOrPressButton withBarButtonItem:self];
}
}

- (void)buttonTapped:(id)sender {
if (sender != _tapOrPressButton) {
return;
}

if([_delegate respondsToSelector:@selector(buttonTapped:withBarButtonItem:)]) {
[_delegate buttonTapped:_tapOrPressButton withBarButtonItem:self];
}
}

- (id)initWithTitle:(NSString*)title andDelegate:(id<TapOrPressButtonDelegate>)delegate {
if (self = [super init]) {
// Store delegate reference
_delegate = delegate;

// Create the customm button that will have the iPad-nav-bar-default appearance
_tapOrPressButton = [UIGlossyButton buttonWithType:UIButtonTypeCustom];
[_tapOrPressButton setTitle:title forState:UIControlStateNormal];
[_tapOrPressButton setNavigationButtonWithColor:[UIColor colorWithRed:123.0/255 green:130.0/255 blue:139.0/255 alpha:1.0]];
// Calculate width...
CGSize labelSize = CGSizeMake(1000, 30);
labelSize = [title sizeWithFont:_tapOrPressButton.titleLabel.font constrainedToSize:labelSize lineBreakMode:UILineBreakModeMiddleTruncation];
_tapOrPressButton.frame = CGRectMake(0, 0, labelSize.width+20, 30);

// Add a handler for a tap
[_tapOrPressButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
// Add a handler for a long-press
UILongPressGestureRecognizer* buttonLongPress_ = [[UILongPressGestureRecognizer alloc] initWithTarget:self
action:@selector(buttonLongPressed:)];
[_tapOrPressButton addGestureRecognizer:buttonLongPress_];

// Set this button as the custom view of the bar item...
self.customView = _tapOrPressButton;
}
return self;
}

// Safe guards...
- (id)initWithImage:(UIImage *)image style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action {
NSLog(@"%s not supported!", __FUNCTION__);
return nil;
}

- (id)initWithImage:(UIImage *)image landscapeImagePhone:(UIImage *)landscapeImagePhone style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action {
NSLog(@"%s not supported!", __FUNCTION__);
return nil;
}

- (id)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action {
NSLog(@"%s not supported!", __FUNCTION__);
return nil;
}
- (id)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(id)target action:(SEL)action {
NSLog(@"%s not supported!", __FUNCTION__);
return nil;
}

- (id)initWithCustomView:(UIView *)customView {
NSLog(@"%s not supported!", __FUNCTION__);
return nil;
}

@end

你需要做的就是:

<强>1。实例化如下:

TapOrPressBarButtonItem* undoMenuButton = [[TapOrPressBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Undo", @"Undo Menu Title") andDelegate:self];

<强>2。将按钮连接到导航栏:

[self.navigationItem setLeftBarButtonItem:undoMenuButton animated:NO];

<强>3。实现 TapOrPressButtonDelegate 协议(protocol),您就完成了...

-(void)buttonTapped:(UIButton*)button withBarButtonItem:(UIBarButtonItem*)barButtonItem {
[self menuItemUndo:barButtonItem];
}

-(void)buttonLongPressed:(UIButton*)button withBarButtonItem:(UIBarButtonItem*)barButtonItem {
[self undoMenuClicked:barButtonItem];
}

希望这对其他人有帮助...

关于ios - iPad 撤消按钮(a-la Keynote 和其他应用程序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9799001/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com