UIMenuController的使用,对UILabel拷贝以及定制菜单
2015-09-20 11:45
459 查看
1. Menu所处的View必须实现 – (BOOL)canBecomeFirstResponder, 且返回YES
2. Menu所处的View必须实现 – (BOOL)canPerformAction:withSender, 并根据需求返回YES或NO
3. 使Menu所处的View成为First Responder (becomeFirstResponder)
4. 定位Menu (- setTargetRect:inView:)
5. 展示Menu (- setMenuVisible:animated:)
[cpp] view
plaincopyprint?
@implementation UICopyLabel
// default is NO
- (BOOL)canBecomeFirstResponder{
return YES;
}
//"反馈"关心的功能
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
return (action == @selector(copy:));
}
//针对于copy的实现
-(void)copy:(id)sender{
UIPasteboard *pboard = [UIPasteboard generalPasteboard];
pboard.string = self.text;
}
//UILabel默认是不接收事件的,我们需要自己添加touch事件
-(void)attachTapHandler{
self.userInteractionEnabled = YES; //用户交互的总开关
UITapGestureRecognizer *touch = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
touch.numberOfTapsRequired = 2;
[self addGestureRecognizer:touch];
[touch release];
}
//绑定事件
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self attachTapHandler];
}
return self;
}
//同上
-(void)awakeFromNib{
[super awakeFromNib];
[self attachTapHandler];
}
-(void)handleTap:(UIGestureRecognizer*) recognizer{
[self becomeFirstResponder];
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setTargetRect:self.frame inView:self.superview];
[menu setMenuVisible:YES animated:YES];
}
@end
在view里添加一个UICopyLabel
现在可以使用UICopyLabel实现双击来对label的内容copy了
在你的view中
UICopyLabel *display = [[UICopyLabelalloc]initWithFrame:CGRectMake(30,100,250,30)];
awakeFromNib
在使用IB的时候才会涉及到此方法的使用,当.nib文件被加载的时候,会发送一个awakeFromNib的消息到.nib文件中的每个对象,每个对象都可以定义自己的awakeFromNib函数来响应这个消息,执行一些必要的操作。
看例子:
创建一个viewController with XIB
定义一个UIView的子类
打开xib,并把View的类型指定为上一步骤定义的子类
然后在TestView.m中加入 awakeFromNib方法,运行程序发现此方法被调用了!!!
下面我们来定制菜单
在[b]attachTapHandler中添加长按压手势[/b]
[cpp] view
plaincopyprint?
-(void)attachTapHandler{
self.userInteractionEnabled = YES; //用户交互的总开关
//双击
UITapGestureRecognizer *touch = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
touch.numberOfTapsRequired = 2;
[self addGestureRecognizer:touch];
[touch release];
//长按压
UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
press.minimumPressDuration = 1.0;
[self addGestureRecognizer:press];
[press release];
}
添加方法longPress
[cpp] view
plaincopyprint?
- (void)longPress:(UILongPressGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
// TSTableViewCell *cell = (TSTableViewCell *)recognizer.view;
[self becomeFirstResponder];
UIMenuItem *flag = [[UIMenuItem alloc] initWithTitle:@"Flag" action:@selector(flag:)];
UIMenuItem *approve = [[UIMenuItem alloc] initWithTitle:@"Approve" action:@selector(approve:)];
UIMenuItem *deny = [[UIMenuItem alloc] initWithTitle:@"Deny" action:@selector(deny:)];
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setMenuItems:[NSArray arrayWithObjects:flag, approve, deny, nil]];
[menu setTargetRect:self.frame inView:self.superview];
[menu setMenuVisible:YES animated:YES];
NSLog(@"menuItems:%@",menu.menuItems);
}
}
- (void)flag:(id)sender {
NSLog(@"Cell was flagged");
}
- (void)approve:(id)sender {
NSLog(@"Cell was approved");
}
- (void)deny:(id)sender {
NSLog(@"Cell was denied");
}
修改canPerformAction
[cpp] view
plaincopyprint?
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
// return (action == @selector(copy:));
if (action == @selector(copy:)||action == @selector(flag:)||action == @selector(approve:)||action == @selector(deny:)) {
return YES;
}
}
ok。。。效果如图
2. Menu所处的View必须实现 – (BOOL)canPerformAction:withSender, 并根据需求返回YES或NO
3. 使Menu所处的View成为First Responder (becomeFirstResponder)
4. 定位Menu (- setTargetRect:inView:)
5. 展示Menu (- setMenuVisible:animated:)
[cpp] view
plaincopyprint?
@implementation UICopyLabel
// default is NO
- (BOOL)canBecomeFirstResponder{
return YES;
}
//"反馈"关心的功能
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
return (action == @selector(copy:));
}
//针对于copy的实现
-(void)copy:(id)sender{
UIPasteboard *pboard = [UIPasteboard generalPasteboard];
pboard.string = self.text;
}
//UILabel默认是不接收事件的,我们需要自己添加touch事件
-(void)attachTapHandler{
self.userInteractionEnabled = YES; //用户交互的总开关
UITapGestureRecognizer *touch = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
touch.numberOfTapsRequired = 2;
[self addGestureRecognizer:touch];
[touch release];
}
//绑定事件
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self attachTapHandler];
}
return self;
}
//同上
-(void)awakeFromNib{
[super awakeFromNib];
[self attachTapHandler];
}
-(void)handleTap:(UIGestureRecognizer*) recognizer{
[self becomeFirstResponder];
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setTargetRect:self.frame inView:self.superview];
[menu setMenuVisible:YES animated:YES];
}
@end
在view里添加一个UICopyLabel
现在可以使用UICopyLabel实现双击来对label的内容copy了
在你的view中
UICopyLabel *display = [[UICopyLabelalloc]initWithFrame:CGRectMake(30,100,250,30)];
awakeFromNib
在使用IB的时候才会涉及到此方法的使用,当.nib文件被加载的时候,会发送一个awakeFromNib的消息到.nib文件中的每个对象,每个对象都可以定义自己的awakeFromNib函数来响应这个消息,执行一些必要的操作。
看例子:
创建一个viewController with XIB
定义一个UIView的子类
打开xib,并把View的类型指定为上一步骤定义的子类
然后在TestView.m中加入 awakeFromNib方法,运行程序发现此方法被调用了!!!
下面我们来定制菜单
在[b]attachTapHandler中添加长按压手势[/b]
[cpp] view
plaincopyprint?
-(void)attachTapHandler{
self.userInteractionEnabled = YES; //用户交互的总开关
//双击
UITapGestureRecognizer *touch = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
touch.numberOfTapsRequired = 2;
[self addGestureRecognizer:touch];
[touch release];
//长按压
UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
press.minimumPressDuration = 1.0;
[self addGestureRecognizer:press];
[press release];
}
添加方法longPress
[cpp] view
plaincopyprint?
- (void)longPress:(UILongPressGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
// TSTableViewCell *cell = (TSTableViewCell *)recognizer.view;
[self becomeFirstResponder];
UIMenuItem *flag = [[UIMenuItem alloc] initWithTitle:@"Flag" action:@selector(flag:)];
UIMenuItem *approve = [[UIMenuItem alloc] initWithTitle:@"Approve" action:@selector(approve:)];
UIMenuItem *deny = [[UIMenuItem alloc] initWithTitle:@"Deny" action:@selector(deny:)];
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setMenuItems:[NSArray arrayWithObjects:flag, approve, deny, nil]];
[menu setTargetRect:self.frame inView:self.superview];
[menu setMenuVisible:YES animated:YES];
NSLog(@"menuItems:%@",menu.menuItems);
}
}
- (void)flag:(id)sender {
NSLog(@"Cell was flagged");
}
- (void)approve:(id)sender {
NSLog(@"Cell was approved");
}
- (void)deny:(id)sender {
NSLog(@"Cell was denied");
}
修改canPerformAction
[cpp] view
plaincopyprint?
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
// return (action == @selector(copy:));
if (action == @selector(copy:)||action == @selector(flag:)||action == @selector(approve:)||action == @selector(deny:)) {
return YES;
}
}
ok。。。效果如图
相关文章推荐
- A014-values资源
- UCGUI使用外部字库芯片显示汉字
- poj 2031--Building a Space Station(prim)
- poj 2031--Building a Space Station(prim)
- chmod\chown\chgrp\umask\SUID \SGID 详解
- [LeetCode#272] Closest Binary Search Tree Value II
- 洛谷2524 Uim的情人节礼物·其之弐解题报告
- iOS 完全复制UIView
- UIBarButtonItem随着屏幕切换变化的问题
- 修改系统TabBar上图片和文字的颜色
- Android UI高级控件之ArrayAdapter
- iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片上下垂直居中对齐
- 基于UIControl控件实现ios点赞功能
- JUnit4.8.2来源分析-2 org.junit.runner.Request
- Volley框架解析(七)-----Request解析
- 3.3.1 SequenceMatcher对象
- 3.3.1 SequenceMatcher对象
- stack实现queue ; list实现stack
- Unique Binary Search Trees 解答
- 如何自己写xuetr(一) 每次改变的驱动名和服务名