在UICollectionView中复制标注 [英] Copy Callout in UICollectionView

查看:117
本文介绍了在UICollectionView中复制标注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在每个单元格中都有一个带UIImageView的UICollectionView,现在我想添加Copy Callout,就像在Photos.app中一样:

I have a UICollectionView with UIImageView in each cell, now I want to add Copy Callout, like in Photos.app:

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

I saw this method in the UICollectionViewDelegate:

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

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

After few additional minutes of research I found UIMenuController Class, as I understood, I must to work with it to get the Menu, but anyway, I think that there must to be more simple way then creating UIGestureRecognizer, and creating, positioning etc. my UIMenu.

我是否在正确的轨道上?你怎么能实现这个功能?

推荐答案

这是完整的解决方案:

- (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。

In my case, I'm allowing only Copy feature in my CollectionView, and if the copy is pressed, I'm copying the image that is inside the cell to the PasteBoard.

这篇关于在UICollectionView中复制标注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆