iPhone开发:iPad的自定义菜单项研究

3.2开始可以自定义菜单项了,根据iPadProgrammingGuide小小的研究了一下,

参考了如下链接http://github.com/billymeltdown/copylabel

 

基本思路如下

custom一个UIView或UIControl,通过touch使它获取焦点即[self becomeFirstResponder],然后把菜单show出来就可以了

 

代码

#import <UIKit/UIKit.h>

 

@interface CustomControl : UIControl {

CGPoint thePoint;

}

 

- (void)showMenu;

 

@end

 

 

 

 

#import "CustomControl.h"

 

@implementation CustomControl

 

-(void)changeColor:(id)sender{

NSLog(@"changeColor");

static int rand = -1;

if (++rand==4)

rand=0;

UIColor *color = nil;

switch (rand) {

case 0:

color = [UIColor greenColor];

break;

case 1:

color = [UIColor yellowColor];

break;

case 2:

color = [UIColor magentaColor];

break;

case 3:

color = [UIColor purpleColor];

break;

default:

break;

}

 

self.backgroundColor = color;

}

 

-(void)noTouch:(id)sender{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Don't touch me!" 

message:nil 

  delegate:nil 

  cancelButtonTitle:nil 

  otherButtonTitles:@"I Know",nil];

[alert show];

[alert release];

}

 

- (void)showMenu {

UIMenuController *theMenu = [UIMenuController sharedMenuController];

CGRect theFrame = [[self superview] frame];

CGRect selectionRect = CGRectMake(thePoint.x, theFrame.origin.y - 5.0, 0, 0);

UIMenuItem *menuItem = [[[UIMenuItem alloc] initWithTitle:@"Change Color"

  action:@selector(changeColor:)] autorelease];

UIMenuItem *menuItem1 = [[[UIMenuItem alloc] initWithTitle:@"Don't touch me!"

action:@selector(noTouch:)] autorelease];

//theMenu.arrowDirection = UIMenuControllerArrowLeft;

theMenu.menuItems = [NSArray arrayWithObjects:menuItem,menuItem1,nil]; 

[theMenu setTargetRect:selectionRect inView:self];

[theMenu setMenuVisible:YES animated:YES];

}

 

- (void)copy:(id)sender {

 

NSLog(@"copy");

//override

}

- (void)paste:(id)sender{

NSLog(@"paste");

//override

}

 

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {

BOOL answer = NO;

if (action == @selector(copy:)){

//answer = YES;

}

if (action == @selector(paste:)) {

//answer = YES;

}

if (action == @selector(changeColor:)) {

answer = YES;

}

if (action == @selector(noTouch:)) {

answer = YES;

}

return answer;

}

 

- (BOOL)canBecomeFirstResponder {

    return YES;

}

 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

if ([self canBecomeFirstResponder]) {

[self becomeFirstResponder];

UITouch *touch = [touches anyObject];

thePoint = [touch locationInView:self];

[self performSelector:@selector(showMenu) withObject:nil afterDelay:0.8f];

}

}

 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

 

}

 

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {

 

}

 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

}

 

 

- (void)dealloc {

 

[super dealloc];

}

 

@end

加载它的对象就可以看到效果
可以通过- ( BOOL)canPerformAction:( SEL)action withSender:( id)sender屏蔽一些菜单项,return YES可以让该项显示
如果UIMenuItem的selector为nil,这一项也不会显示,因为空项没有意义,大概apple是这么想的吧
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值