UIMenuController的使用,對UILabel拷貝以及定製菜單
1. Menu所處的View必須實現 – (BOOL)canBecomeFirstResponder, 且返回YES app
2. Menu所處的View必須實現 – (BOOL)canPerformAction:withSender, 並根據需求返回YES或NO 函數
3. 使Menu所處的View成爲First Responder (becomeFirstResponder) ui
4. 定位Menu (- setTargetRect:inView:) spa
5. 展現Menu (- setMenuVisible:animated:) .net
- @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 orm
如今能夠使用UICopyLabel實現雙擊來對label的內容copy了 對象
在你的view中 事件
UICopyLabel *display = [[UICopyLabelalloc]initWithFrame:CGRectMake(30,100,250,30)]; get
awakeFromNib string
在使用IB的時候纔會涉及到此方法的使用,當.nib文件被加載的時候,會發送一個awakeFromNib的消息到.nib文件中的每一個對象,每一個對象均可以定義本身的awakeFromNib函數來響應這個消息,執行一些必要的操做。
看例子:
建立一個viewController with XIB
定義一個UIView的子類
打開xib,並把View的類型指定爲上一步驟定義的子類
而後在TestView.m中加入 awakeFromNib方法,運行程序發現此方法被調用了!!!
下面咱們來定製菜單
在attachTapHandler中添加長按壓手勢
- -(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
- - (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");
- }
- -(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。。。效果如圖