首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UICollectionView中的复制标注

UICollectionView中的复制标注
EN

Stack Overflow用户
提问于 2012-11-19 16:54:59
回答 3查看 8.4K关注 0票数 8

我在每个单元格中都有一个带有UIImageView的UIImageView,现在我想添加复制标注,就像在Photos.app中:

我在UICollectionViewDelegate中看到了这种方法:

代码语言:javascript
运行
复制
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

经过几分钟的研究,我找到了UIMenuController类,据我所知,我必须使用它来获得菜单,但无论如何,我认为必须有更简单的方法来创建UIGestureRecognizer,以及创建、定位等我的UIMenu。

,我在正确的轨道上吗?如何实现此功能?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-11-19 17:33:03

这是完全的解决办法:

代码语言:javascript
运行
复制
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
        return YES;
    }

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
        if ([NSStringFromSelector(action) isEqualToString:@"copy:"])
            return YES;
        else
            return NO;
    }

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
        if ([NSStringFromSelector(action) isEqualToString:@"copy:"]) {
            UIPasteboard *pasteBoard = [UIPasteboard pasteboardWithName:UIPasteboardNameGeneral create:NO];
            pasteBoard.persistent = YES;
            NSData *capturedImageData = UIImagePNGRepresentation([_capturedPhotos objectAtIndex:indexPath.row]);
            [pasteBoard setData:capturedImageData forPasteboardType:(NSString *)kUTTypePNG];
        }
    }

在我的例子中,我只允许在我的CollectionView中复制功能,如果按下该副本,我将将单元格内的图像复制到PasteBoard中。

票数 10
EN

Stack Overflow用户

发布于 2012-11-29 03:12:25

是的,你走在正确的轨道上。您还可以使用此技术实现除剪切、复制和粘贴之外的自定义操作。

UICollectionView的自定义操作

代码语言:javascript
运行
复制
// ViewController.h
@interface ViewController : UICollectionViewController

// ViewController.m
-(void)viewDidLoad
{
    [super viewDidLoad];
    self.collectionView.delegate = self;

    UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"Custom Action"
                                                      action:@selector(customAction:)];
    [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObject:menuItem]];

}

#pragma mark - UICollectionViewDelegate methods
- (BOOL)collectionView:(UICollectionView *)collectionView
      canPerformAction:(SEL)action
    forItemAtIndexPath:(NSIndexPath *)indexPath
            withSender:(id)sender {
    return YES;  // YES for the Cut, copy, paste actions
}

- (BOOL)collectionView:(UICollectionView *)collectionView
shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)collectionView:(UICollectionView *)collectionView
         performAction:(SEL)action
    forItemAtIndexPath:(NSIndexPath *)indexPath
            withSender:(id)sender {
    NSLog(@"performAction");
}

#pragma mark - UIMenuController required methods
- (BOOL)canBecomeFirstResponder {
    // NOTE: The menu item will on iOS 6.0 without YES (May be optional on iOS 7.0)
    return YES;
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    NSLog(@"canPerformAction");
     // The selector(s) should match your UIMenuItem selector
    if (action == @selector(customAction:)) {
        return YES;
    }
    return NO;
}

#pragma mark - Custom Action(s)
- (void)customAction:(id)sender {
    NSLog(@"custom action! %@", sender);
}

注释: iOS 7.0更改行为

  1. 在您的UICollectionViewCell子类中,您需要添加自定义操作方法,否则什么都不会出现。 // Cell.m #导入"Cell.h“@实现单元-(Id)initWithFrame:(CGRect)框架{ self = super :frame;if (self) { //定制逻辑}返回self;}-(Void)customAction:(Id)发件人{ NSLog(@"Hello");if(self.delegate NSLog{ self.delegate定制操作:发件人forCell:self;} @end )
  2. 您需要创建一个委托协议,并在每个单元上设置它,以便调用维护您的UIController的UICollectionView。这是因为单元格不应该与您的模型无关,因为它只涉及显示内容。 // Cell.h #import @class Cell;//转发声明自定义单元为属性@ MyMenuDelegate @可选-(无效)customAction:( id )发送方forCell:(Cell *)单元;@end @接口单元: UICollectionViewCell @property (强,非原子) UILabel*标签;@property (弱,非原子)id委托;@end
  3. 在您的ViewController或UICollectionViewController子类中,您需要遵守协议并实现新方法。 // ViewController.m @interface ViewController () @end // @implementation ViewController .- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;{ Cell *cell = cv ViewController forIndexPath:indexPath;cell.delegate = self;返回单元格;} // . // iOS 7.0的委托方法从UICollectionViewCell -(Void)customAction:(Id)发件人forCell:(单元格*)单元{NSLog(@“定制操作!%@",发件人)获得操作;}

  1. 可选:在UIView子类中,如果在这里实现了方法canPerformAction,则可以重写默认的剪切、复制和粘贴,而不是在UIViewController中。否则,该行为将在自定义方法之前显示默认方法。 // Cell.m - (BOOL)canPerformAction:(SEL)action withSender:(Id)发件人{ NSLog(@"canPerformAction");//选择器应该匹配您的UIMenuItem选择器NSLog(@“发件人:%@",发送方);if ( == @选择器(customAction:)){返回是;}返回否;}

票数 18
EN

Stack Overflow用户

发布于 2013-09-10 17:20:14

也许有点晚了,但我可能找到了一个更好的解决方案,对于那些仍然在寻找这个的人:

在viewDidLoad of your UICollectionViewController中添加您的项目:

代码语言:javascript
运行
复制
UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"Title" action:@selector(action:)];
[[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObject:menuItem]];

添加以下委托方法:

代码语言:javascript
运行
复制
//This method is called instead of canPerformAction for each action (copy, cut and paste too)
- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
        if (action == @selector(action:)) {
            return YES;
        }
        return NO;
    }
    //Yes for showing menu in general
    - (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
        return YES;
    }

子类UICollectionViewCell,如果您还没有。添加为项指定的方法:

代码语言:javascript
运行
复制
- (void)action:(UIMenuController*)menuController {

}

这样,您就不需要任何becomeFirstResponder或其他方法。所有操作都位于一个位置,如果您调用一个通用方法,并将单元本身作为参数,则可以轻松地处理不同的单元格。

编辑: uicollectionview需要这个方法的存在(您的自定义操作不需要这个方法,我认为uicollectionview只是检查是否存在)。

代码语言:javascript
运行
复制
- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {

}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13458503

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档