gpt4 book ai didi

ios - 在 UICollectionView 中复制标注

转载 作者:可可西里 更新时间:2023-11-01 03:02:07 28 4
gpt4 key购买 nike

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

enter image description here

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

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

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

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

最佳答案

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

Custom actions for the 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 改变了行为

  1. 在您的 UICollectionViewCell 子类中,您需要添加自定义操作方法,否则什么也不会出现。

    // Cell.m
    #import "Cell.h"

    @implementation Cell

    - (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
    // custom logic
    }
    return self;
    }

    - (void)customAction:(id)sender {
    NSLog(@"Hello");

    if([self.delegate respondsToSelector:@selector(customAction:forCell:)]) {
    [self.delegate customAction:sender forCell:self];
    }
    }
    @end
  2. 您需要创建一个委托(delegate)协议(protocol)并将其设置在每个单元格上以回调维护您的 UICollectionView 的 UIController。这是因为单元格不应该与您的模型有关,因为它只涉及显示内容。

    // Cell.h
    #import <UIKit/UIKit.h>

    @class Cell; // Forward declare Custom Cell for the property

    @protocol MyMenuDelegate <NSObject>
    @optional
    - (void)customAction:(id)sender forCell:(Cell *)cell;
    @end

    @interface Cell : UICollectionViewCell

    @property (strong, nonatomic) UILabel* label;
    @property (weak, nonatomic) id<MyMenuDelegate> delegate;
    @end
  3. 在您的 ViewController 或 UICollectionViewController 的子类中,您需要遵守协议(protocol)并实现新方法。

    // ViewController.m
    @interface ViewController () <MyMenuDelegate>
    @end

    // @implementation ViewController ...

    - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
    {
    Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"MY_CELL" forIndexPath:indexPath];
    cell.delegate = self;
    return cell;
    }
    // ...

    // Delegate method for iOS 7.0 to get action from UICollectionViewCell
    - (void)customAction:(id)sender forCell:(Cell *)cell {
    NSLog(@"custom action! %@", sender);
    }

    Custom longpress action menu for UICollectionView

  4. 可选:在您的 UIView 子类中,如果您在此处而不是在 UIViewController 中实现 canPerformAction 方法,则可以覆盖默认的剪切、复制和粘贴。否则,该行为将在您的自定义方法之前显示默认方法。

    // Cell.m
    - (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    NSLog(@"canPerformAction");
    // The selector(s) should match your UIMenuItem selector

    NSLog(@"Sender: %@", sender);
    if (action == @selector(customAction:)) {
    return YES;
    }
    return NO;
    }

    Custom Action from UICell canPerformAction

关于ios - 在 UICollectionView 中复制标注,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13458503/

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