iPhone开发:iPad的自定义菜单项研究
2010-06-09 17:30
295 查看
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是这么想的吧
参考了如下链接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是这么想的吧
相关文章推荐
- (iPhone/iPad开发)在UIWebView中自定义菜单栏
- (iPhone/iPad开发)在UIWebView中自定义菜单栏
- (iPhone/iPad开发)在UIWebView中自定义菜单栏
- iphone/ipad前端开发技巧
- iPhone开发 判断用户使用的是ipad还是iphone
- iOS开发那些事-构建自适应iPhone和iPad工程
- 诚聘iPhone/iPad软件开发经理 (上海)
- iOS(iPhone,iPad))开发(Objective-C)开发库常用库索引
- (iPhone/iPad开发)在iOS设备上实现摇晃手机体验的相关功能
- iPhone 和 iPad的ios 开发中 利用 WebViewJavascriptBridge组件,通过 UIWebView 对Html进行双向通讯
- iPhone开发 自定义uitabbar
- iOS开发iPad和iPhone的启动画面设置
- (iPhone/iPad开发)详解UIView的center属性
- iPhone/iPad高级应用与手游开发学习笔记:多点触摸与手势检测(二:UIPinchGestureRecognizer)
- ios(iphone/ipad)开发每日一练【2011-12-3】
- iPhone/iPad开发扎记 2012/02/11 - 玩转iOS Audio Service
- iOS:iPad和iPhone开发的异同(UIPopoverController、UISplitViewController)
- iPhone 和 iPad的ios 开发中 利用 WebViewJavascriptBridge组件,通过 UIWebView 对Html进行双向通讯
- iphone和iPad开发的区别