我在每个单元格中都有一个带有UIImageView的UIImageView,现在我想添加复制标注,就像在Photos.app中:
我在UICollectionViewDelegate中看到了这种方法:
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
经过几分钟的研究,我找到了UIMenuController类,据我所知,我必须使用它来获得菜单,但无论如何,我认为必须有更简单的方法来创建UIGestureRecognizer,以及创建、定位等我的UIMenu。
,我在正确的轨道上吗?如何实现此功能?
发布于 2012-11-19 17:33:03
这是完全的解决办法:
- (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中。
发布于 2012-11-29 03:12:25
是的,你走在正确的轨道上。您还可以使用此技术实现除剪切、复制和粘贴之外的自定义操作。
UICollectionView的自定义操作
// 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更改行为
发布于 2013-09-10 17:20:14
也许有点晚了,但我可能找到了一个更好的解决方案,对于那些仍然在寻找这个的人:
在viewDidLoad of your UICollectionViewController中添加您的项目:
UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"Title" action:@selector(action:)];
[[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObject:menuItem]];
添加以下委托方法:
//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,如果您还没有。添加为项指定的方法:
- (void)action:(UIMenuController*)menuController {
}
这样,您就不需要任何becomeFirstResponder或其他方法。所有操作都位于一个位置,如果您调用一个通用方法,并将单元本身作为参数,则可以轻松地处理不同的单元格。
编辑: uicollectionview需要这个方法的存在(您的自定义操作不需要这个方法,我认为uicollectionview只是检查是否存在)。
- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
}
https://stackoverflow.com/questions/13458503
复制相似问题