CCMenu 事件传参
今天看到OC对UIButton的封装,可以自定响应事件的 今天就拿CCMenuItemImage做实验 首先在.h文件 CCMenuItemImage* DidItem(const char*imgName,SEL_MenuHandlerm_pfnSelectior);//声明自己的方法 点cpp文件 CCMenuItemImage* HelloWorld::DidItem(constchar*imgName,SEL_MenuHandler m_pfnSelectior){ CCMenuItemImage*item=CCMenuItemImage::create(imgName, imgName, this, m_pfnSelectior); return item; } 调用 SEL_MenuHandler m_pfnSelectior1 = (SEL_MenuHandler)(&HelloWorld::menuCloseCallback); SEL_MenuHandler m_pfnSelectior2 = (SEL_MenuHandler)(&HelloWorld::menu1Callback); CCMenuItemImage*itemImg=HelloWorld::DidItem("Icon.png", m_pfnSelectior1); CCMenuItemImage*itemImg1=HelloWorld::DidItem("Icon.png", m_pfnSelectior2); itemImg1->setPosition(0, 100); CCMenu* pMenu = CCMenu::create(itemImg,itemImg1, NULL); pMenu->setPosition(ccp(70, size.height)); this->addChild(pMenu); 不但可以封装他的初始化 还可以自定义他的响应回调方法; 千万不要问我 tag不能解决吗?解决是肯定的! 会有一些局限性! OC 事件自定义 - (UIButton *)buttonWithFrame:(CGRect)frame action:(SEL)action { UIImage *buttonBackgroundImage = [[UIImage imageNamed:@"button_background.png"]stretchableImageWithLeftCapWidth:5 topCapHeight:5]; UIImage *disabledButtonBackgroundImage = [[UIImageimageNamed:@"button_background_disabled.png"] stretchableImageWithLeftCapWidth:5topCapHeight:5]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = frame; [button setBackgroundImage:buttonBackgroundImage forState:UIControlStateNormal]; [button setBackgroundImage:disabledButtonBackgroundImageforState:UIControlStateDisabled]; [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [button setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled]; [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; return button; } 调用 loginButton = [self buttonWithFrame:CGRectMake(20, 100, 280, 40)action:@selector(loginButtonPressed)];