是否可以在cocos2d中使用UIPickerView? [英] Is it possible to use UIPickerView in cocos2d?
问题描述
我使用cocos2d开发iPhone 2d游戏。我需要一个Picker。是可能在cocos2d中使用Picker!如果可能,请告诉我,如何在cocos2d中使用Picker?
I am developing iPhone 2d game by using cocos2d. I need a Picker. Is it possible to use Picker in cocos2d ! If possible then tell me, How can I use a Picker in cocos2d ?
推荐答案
是的,您可以混合和匹配基于标准UIView具有Cocos2D类的类。
Yes, you can mix and match standard UIView based classes with Cocos2D classes.
在您的应用程序委托中,当您启动Director时,您创建了一个UIWindow并将Director附加到它。您还可以在您的appdelegate中保存对窗口的引用。现在你可以创建和添加UIViews到窗口,以及操纵cocos2d节点照常通过导演。
In your application delegate, when you started up the Director, you created a UIWindow and attached the Director to it. You can also save a reference to the window in your appdelegate. Now you can create and add UIViews to the Window, as well as manipulate cocos2d nodes as usual through the director.
从这里,这只是一个问题,创建一个UIPickerView,并将其添加到窗口。配置UIPickerView是一个完整的任务本身... Nitrex88有一个关于此主题的好视频。此外,请查看 UICatalog 的实体不是UIPickerView的例子,而是许多更多的UIView子类。
From here, it's just a matter of creating a UIPickerView, and adding it to the window. Configuring the UIPickerView is a whole task unto itself... Nitrex88 has a good video on the subject . Also, check out UICatalog for a solid example not just of UIPickerView, but many many more UIView subclasses.
这里是一个添加一个简单的UIPicker到cocos2d应用程序的例子:
Here's an example of adding a trivial UIPicker to a cocos2d app:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "cocos2d.h"
@interface AppDelegate {
UIWindow *window;
NSArray *pickerValues;
}
@property (nonatomic, retain) UIWindow window;
@property (nonatomic, retain) NSArray *pickerValues;
@end
@implementation AppDelegate
@synthesize window, pickerValues;
-(void)applicationDidFinishLaunching:(UIApplication *)application {
// Create Window
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[window setUserInteractionEnabled:YES];
[window setMultipleTouchEnabled:YES];
// Set up Director and attach to window
[[Director sharedDirector] attachInWindow:window];
[[Director sharedDirector] setLandscape:YES];
[[Director sharedDirector] runWithScene:[MyScene node]];
// Create one large view and rotate the coordinates to landscape
UIView *parentView = [[UIView alloc] initWithFrame:CGRectMake(0.0f,0.0f,480.0f, 320.0f)];
parentView.transform = CGAffineTransformIdentity;
parentView.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
parentView.bounds = CGRectMake(0.0f, 0.0f, 480.0f, 320.0f);
// Initialize picker and its data source
pickerValues = [[NSArray alloc] initWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",nil];
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0f, 195.0f, 320.0f, 125.0f)];
[pickerView setDelegate:self];
// Attach picker to parent view and parent view to window
[parentView addSubview:pickerView];
[window addSubview:parentView];
[window makeKeyAndVisible];
}
- (void) dealloc {
[window release];
[pickerValues release];
[super dealloc];
}
// ====================
// UIPicker Callbacks
// ====================
// Fire when new picker values are selected
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSString *numberSequence = [NSString stringWithFormat:@"Sequence: %@%@%@",
[pickerValues objectAtIndex:[thePickerView selectedRowInComponent:0]],
[pickerValues objectAtIndex:[thePickerView selectedRowInComponent:1]],
[pickerValues objectAtIndex:[thePickerView selectedRowInComponent:2]]];
NSLog(numberSequence);
}
// Number of picker wheels in the picker
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 3;
}
// Number of items in each picker wheel
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [pickerValues count];
}
// Title for Row #
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [pickerValues objectAtIndex:row];
}
// Row height in pixels
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
return 40.0;
}
// Column width in pixels
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
return 90.0f;
}
// ====================
@end
这篇关于是否可以在cocos2d中使用UIPickerView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!