iOS 控件定制iOS点点滴滴iOS Blogs

iOS-UIMenuController搭配UIPasteboa

2016-04-28  本文已影响1254人  iOSerYJ

基本概念

UIKit框架中,可以直接执行拷贝黏贴操作的有 : UITextView、UITextField和UIWebView,其他控件需要实现相关方法

关于UIPasteboard

@property(getter=isPersistent,nonatomic) BOOL persistent;
UIPasteboard *generalPasteboard = [UIPasteboard generalPasteboard];

具体实现

以label为例,假设label轻触两下或者长按显示编辑菜单


屏幕快照 2016-04-28 下午8.55.08.png

过程将会涉及

注 : 展示编辑菜单之前,系统会发送canPerformAction:withSender:消息给第一响应者,所以控件需要成为第一响应者来接收消息,并且在这个方法中,根据上下文的环境评估命令是否可用,例如当黏贴板没有可处理的数据时,响应者应该返回NO,阻止黏贴命令

0.首先打开用户交互,这个不要忘记,添加手势

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(showMenu:)];
    longPress.minimumPressDuration = 0.5;
    [self addGestureRecognizer:longPress];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    
    if (touch.tapCount == 2) {
        
        [self showMenu:nil];
        
    }
}

1.是否可以成为第一响应者,默认为NO

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

2.成为第一响应者

[self becomeFirstResponder];

3.创建编辑菜单

//获得单例对象
UIMenuController *menu = [UIMenuController sharedMenuController];
//view是参照物,指向以view为参照物的rect
[menu setTargetRect:self.bounds inView:self];
//显示
[menu setMenuVisible:YES animated:YES];

4.实现canPerformAction:withSender:方法,参数action具体会有哪些方法,基本都在UIResponderStandardEditActions声明了

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    NSLog(@"%@",NSStringFromSelector(action));
    NSLog(@"%@",sender);
    
    if (action == @selector(copy:) && self.text) {
        
        //显示条件是文本不为空
        return YES;
        
    }
    else if (action ==@selector(cut:) && self.text) {
        
        //显示条件是文本不为空
        return YES;
        
    }
    else if (action == @selector(paste:) && [UIPasteboard generalPasteboard].string) {
        
        //显示条件是黏贴板不为空
        return YES;
        
    }
    else {
        
        return NO;
        
    }
}

5.实现copy、cut、paste等方法

//实现剪切
- (void)cut:(id)sender
{
    [self copy:sender];
    
    self.text = nil;
}
//实现拷贝
- (void)copy:(id)sender
{
    [UIPasteboard generalPasteboard].string = self.text;
}
//实现黏贴
- (void)paste:(id)sender
{
    UIPasteboard *generalPasteboard = [UIPasteboard generalPasteboard];
    
    NSMutableArray *types = [[NSMutableArray alloc] init];
    [types addObjectsFromArray:UIPasteboardTypeListString];
    
    if ([generalPasteboard containsPasteboardTypes:types]) {
        
        self.text = generalPasteboard.string;
        
    }
}

6.如果想要添加自定义命令,可以创建UIMenuItem

//添加自定义菜单项
UIMenuItem *item = [[UIMenuItem alloc] initWithTitle:@"Change Color" action:@selector(changeColor:)];
menu.menuItems = @[item];

再以imageView为例,展示图片拷贝黏贴操作

#import "CustomImageView.h"

@implementation CustomImageView

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(showMenu:)];
    longPress.minimumPressDuration = 0.5;
    [self addGestureRecognizer:longPress];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    
    if (touch.tapCount == 2) {
        
        [self showMenu:nil];
        
    }
}
/**
 *  4.响应者是否可以执行该方法
 *
 *  @param action 方法
 *  @param sender 请求对象
 */
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(copy:) && self.image) {
        
        return YES;
        
    }
    else if (action == @selector(cut:) && self.image) {
        
        return YES;
        
    }
    else if (action == @selector(paste:) && [UIPasteboard generalPasteboard].image) {
        
        return YES;
        
    }
    else if (action == @selector(changeColor:)) {
        
        return YES;
        
    }
    else {
        
        return NO;
        
    }
}
/**
 *  1.是否可以成为第一响应者,默认为NO
 */
- (BOOL)canBecomeFirstResponder
{
    return YES;
}

- (void)showMenu:(UILongPressGestureRecognizer *)longPress
{
    if (longPress.state == UIGestureRecognizerStateEnded || longPress == nil) {
        
        //2.成为第一响应者
        [self becomeFirstResponder];
        
        //添加自定义菜单项
        UIMenuItem *item = [[UIMenuItem alloc] initWithTitle:@"Change Color" action:@selector(changeColor:)];
        
        //3.创建编辑菜单
        UIMenuController *menu = [UIMenuController sharedMenuController];
        [menu setTargetRect:self.bounds inView:self];
        
        menu.menuItems = @[item];
        
        [menu setMenuVisible:YES animated:YES];
        
    }
}

- (void)cut:(id)sender
{
    [self copy:sender];
    
    self.image = nil;
}

- (void)copy:(id)sender
{
    [UIPasteboard generalPasteboard].image = self.image;
}

- (void)paste:(id)sender
{
    UIPasteboard *generalPasteboard = [UIPasteboard generalPasteboard];
    
    NSMutableArray *types = [[NSMutableArray alloc] init];
    [types addObjectsFromArray:UIPasteboardTypeListImage];
    
    if ([generalPasteboard containsPasteboardTypes:types]) {
        
        self.image = generalPasteboard.image;
        
    }
}

- (void)changeColor:(id)sender
{
    self.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0  blue:arc4random_uniform(256)/255.0  alpha:1.0];
}
屏幕快照 2016-04-28 下午9.28.28.png
屏幕快照 2016-04-28 下午9.30.34.png

References :
Displaying and Managing the Edit Menu
Copy, Cut, and Paste Operations

上一篇下一篇

猜你喜欢

热点阅读