UI_快捷菜单

这里写图片描述

RootViewController.m
#import "RootViewController.h"
@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)UITableView *tableView;

@end

@implementation RootViewController

- (void)dealloc
{
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    1.创建列表视图
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
//    self.tableView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:self.tableView];
    [_tableView release];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 50;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];

        2.在cell种添加一个长按的手势.
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
        [cell addGestureRecognizer:longPress];
        [longPress release];
    }
    cell.textLabel.text = @"1";
    return cell;
}

3.长按的触发方法
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress {
    // 根据手势对象找视图.
    UITableViewCell *cell = (UITableViewCell *)longPress.view;
    // 创建快捷菜单.
    UIMenuController *menu = [UIMenuController sharedMenuController];
    // 给快捷菜单进行定位显示.
    [menu setTargetRect:cell.frame inView:cell.superview];
    // 让快捷菜单显示出来
    [menu setMenuVisible:YES animated:YES];

    // 自定义的功能
    UIMenuItem *addItem = [[UIMenuItem alloc] initWithTitle:@"add" action:@selector(addAction:)];
    UIMenuItem *flagItem = [[UIMenuItem alloc] initWithTitle:@"flag" action:@selector(flagAction:)];

    // 把功能添加到快捷菜单上.
    [menu setMenuItems:@[addItem, flagItem]];
    [addItem release];
    [flagItem release];

}

4.自定义的item必须实现方法,才能在菜单上显示.
- (void)addAction:(id)sander {

}

- (void)flagAction:(id)sander {

}

5.这个方法必须实现,如果不实现,快捷菜单无法显示
- (BOOL)canBecomeFirstResponder {
    return YES;
}

6.只要重写系统的这些方法,我们就可以在快捷菜单上显示.
- (void)delete:(id)sender {
    NSLog(@"delete");
}

- (void)copy:(id)sender {
    NSLog(@"copy");
}

- (void)select:(id)sender {
    NSLog(@"select");
}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值