iPad开发笔记
2011年08月26日
[b]退回输入键盘: [/b]
- (BOOL) textFieldShouldReturn:(id)textField{ [textField resignFirstResponder]; }
[b]CGRect [/b]
CGRect frame = CGRectMake (origin.x, origin.y, size.width, size.height);矩形 NSStringFromCGRect(someCG) 把CGRect结构转变为格式化字符串; CGRectFromString(aString) 由字符串恢复出矩形; CGRectInset(aRect) 创建较小或较大的矩形(中心点相同),+较小 -较大 CGRectIntersectsRect(rect1, rect2) 判断两矩形是否交叉,是否重叠 CGRectZero 高度和宽度为零的/位于(0,0)的矩形常量
[b]CGPoint & CGSize [/b]
CGPoint aPoint = CGPointMake(x, y); CGSize aSize = CGSizeMake(width, height);
[b]设置透明度 [/b]
[myView setAlpha:value]; (0.0 [/b]
- (IBActive) someButtonPressed:(id) sender { UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@”Are you sure?” delegate:self cancelButtonTitle:@”No way!” destructiveButtonTitle:@”Yes, I’m Sure!” otherButtonTitles:nil]; [actionSheet showInView:self.view]; [actionSheet release]; }
[b]警告视图 [/b]
- (void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex { if(buttonIndex != [actionSheet cancelButtonIndex][NSString alloc]initWithFormat:@”You can breathe easy, everything went OK.”]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Something was done” message:message delegate:self cancelButtonTitle:@”OK” otherButtonTitles:nil]; [alert show]; [alert release]; [message release]; } }
[b]动画效果 [/b]
-(void)doChange:(id)sender { if(view2 == nil) { [self loadSec]; } [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:1]; [UIView setAnimationTransition:([view1 superview] ? UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight)forView : self.view cache:YES]; if([view1 superview]!= nil) { [view1 removeFromSuperview]; [self.view addSubview:view2]; }else { [view2 removeFromSuperview]; [self.view addSubview:view1]; } [UIView commitAnimations]; }
[b]Table View [/b]
#pragma mark - #pragma mark Table View Data Source Methods //指定分区中的行数,默认为1 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.listData count]; } //设置每一行cell显示的内容 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *SimpleTableIndentifier = @"SimpleTableIndentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableInden tifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SimpleTableIndentifier] autorelease]; } UIImage *image = [UIImage imageNamed:@"13.gif"]; cell.imageView.image = image; NSUInteger row = [indexPath row]; cell.textLabel.text = [listData objectAtIndex:row]; cell.textLabel.font = [UIFont boldSystemFontOfSize:20]; if(row #pragma mark - #pragma mark Table View Delegate Methods //把每一行缩进级别设置为其行号 - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath { NSUInteger row = [indexPath row]; return row; } //获取传递过来的indexPath值 - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSUInteger row = [indexPath row]; if (row == 0) return nil; return indexPath; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSUInteger row = [indexPath row]; NSString *rowValue = [listData objectAtIndex:row]; NSString *message = [[NSString alloc] initWithFormat:@"You selected %@",rowValue]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Row Selected" message:message delegate:nil cancelButtonTitle:@"Yes, I did!" otherButtonTitles:nil]; [alert show]; [alert release]; [message release]; [tableView deselectRowAtIndexPath:indexPath animated:YES]; } //设置行的高度 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 40; }
[b]随机数的使用 [/b]
头文件的引用 #import #import srandom()的使用 srandom((unsigned)(mach_absolute_time() & 0xFFFFFFFF)); 直接使用 random() 来调用随机数
[b]在UIImageView 中旋转图像 [/b]
float rotateAngle = M_PI; CGAffineTransform transform =CGAffineTransformMakeRotation(rotateAngle); imageView.transform = transform;
以上代码旋转imageView, 角度为rotateAngle, 方向可以自己测试哦! [b]在Quartz中如何设置旋转点 [/b]
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg.png"]]; imageView.layer.anchorPoint = CGPointMake(0.5, 1.0);
这个是把旋转点设置为底部中间。记住是在QuartzCore.framework中才得到支持。 [b]创建.plist文件并存储 [/b]
NSString *errorDesc; //用来存放错误信息 NSMutableDictionary *rootObj = [NSMutableDictionary dictionaryWithCapacity:4]; //NSDictionary, NSData等文件可以直接转化为plist文件 NSDictionary *innerDict; NSString *name; Player *player; NSInteger saveIndex; for(int i = 0; i 里的内容 while(tmp = [nse nextObject]) { NSString *stringBetweenBrackets = nil; NSScanner *scanner = [NSScanner scannerWithString:tmp]; [scanner scanUpToString:@"" intoString:&stringBetweenBrackets]; NSLog([stringBetweenBrackets description]); }
对于读写文件,还有补充,暂时到此。随机数和文件读写在游戏开发中经常用到。所以把部分内容放在这,以便和大家分享,也当记录,便于查找。 [b]隐藏NavigationBar [/b]
[self.navigationController setNavigationBarHidden:YES animated:YES];
在想隐藏的ViewController中使用就可以了。 [b]如果无法保证子类行为的一致性,那么就用委托 [/b]
If the subClass cann’t keep with superClass,use delegate rather than inheritance. [b]屏幕上看到的,都是UIVew [/b]
Everything you see on Screen is UIView. [b]如果对性能要求高,慎用Interface Build [/b]
if application’s performance is important,be discreet for the interface build. [b]copy是创建,retain是引用 [/b]
the copy operation is create a new one,but the retain operation is just a reference. [b]alloc需要release,convenient不需要release [/b]
alloc method need corresponding release method,but convenient method not. [b]加载到NSArray/NSMutableArray里的对象,不需要负责release [/b]
The objects added to NSArray/NSMutableArray need not to be released. [b]IBOutlet,IBAction为你开启了访问Interface Build中对象的大门 [/b]
IBOutlet and IBAction open the door to access the objects in Interface build. [b]UIApplicationDelegate负责应用程序的生命周期,而UIViewController负责View的生命周期 [/b]
UIApplicationDelegate is responsible for the application life cycle,but UIViewController for the UIView. [b]为了程序的健壮性,请尽量实现Delegate的生命周期函数 [/b]
if you want to develop a robust application,implement the life cycle methods as more as possbile. [b]you触摸的不是UIEvent,而是NSSet的UIView [/b]
what you touch on screen is not UIEvent but UIView [b]UITextField不响应键盘: [/b]
方法1: TextField的的Touch Cancel响应中,添加[textFied resignFirstResponder]; 方法: - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ [textFied resignFirstResponder]; }
[b]更改响应键盘return按钮: [/b]
TextField.returnKeyType=UIReturnKeyDone; select: UIReturnKeyDefault, UIReturnKeyGo, UIReturnKeyGoogle, UIReturnKeyJoin, UIReturnKeyNext, UIReturnKeyRoute, UIReturnKeySearch, UIReturnKeySend, UIReturnKeyYahoo, UIReturnKeyDone, UIReturnKeyEmergencyCall,
[b]尺寸问题: [/b]
iPhone应用程序图标大小:57*57; iPhone全屏UIView大小:320*460 添加UITabBar后大小:320*411 UITabelViewCell默认大小: 320*44
[b]绘制控件方法 [/b]
//--alloc -(UITextField *)GetDefaultTextField:(CGRect)frame{ UITextField *textField=[[UITextField alloc] initWithFrame:frame]; textField.borderStyle=UITextBorderStyleRoundedRect ; textField.font=[UIFont fontWithName:@"Arial" size:12.0]; textField.textAlignment=UITextAlignmentCenter; textField.contentVerticalAlignment=UIControlConten tVerticalAlignmentCenter; textField.keyboardType=UIKeyboardTypeNumbersAndPun ctuation; textField.returnKeyType=UIReturnKeyDone; textField.delegate=self; return textField; } //--alloc -(UILabel *)GetDefaultLabel:(CGRect)frame{ UILabel *label = [[UILabel alloc] initWithFrame: frame]; label.textAlignment=UITextAlignmentCenter; label.textColor=[UIColor blackColor]; label.backgroundColor=[UIColor clearColor]; label.font=[UIFont boldSystemFontOfSize:12.0]; return label; } //--alloc -(UIButton *)GetDefaultButton:(CGRect)frame{ UIButton *button=[[UIButton alloc] initWithFrame:frame]; [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; [button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted]; [button setContentHorizontalAlignment:UIControlContentHori zontalAlignmentLeft]; [button.titleLabel setFont:[UIFont boldSystemFontOfSize:14.0]]; [button.titleLabel setLineBreakMode:UILineBreakModeCharacterWrap]; [button addTarget:self action:@selector(btnTradeTouchUpInside:) forControlEvents:UIControlEventTouchUpInside]; [button setContentHorizontalAlignment:UIControlContentHori zontalAlignmentCenter]; [button setBackgroundImage:[UIImage imageNamed:@"png1.png"] forState:UIControlStateNormal]; [button setBackgroundColor:[UIColor lightGrayColor]]; button.tag=kButtonTag; return button;}
[b]多使用宏定义常量。tag,frame大小,一些判断标志位。 [/b]
#define kIndexValueTag 1
[b]苹果屏幕截图快捷键 [/b]
一般在Mac上用Command-Shif-3/4来截图。注:Command=苹果键 其实还有几个辅助键,来起到不同的截图功能…… 1)Command-Shift-3(适用于OS9,10.1X和10.2) 将整个屏幕拍下并保存到桌面。 2)Command-Shift-4(适用于OS9,10.1X和10.2) 将屏幕的一部分拍下并保存到桌面。当按下着几个键后,光标会变为一个十字,可以拖拉来选取拍报区域。 3)Command-Shift-Control-3(适用于OS9和10.2) 将整个屏幕拍下并保存到剪贴板,可以Command+V直接粘贴到如Photoshop等软件中编辑。 4)Command-Shift-Control-4(适用于OS9和10.2) 将屏幕的一部分拍下并保存到剪贴板。 5)Command-Shift-4再按空格键(适用于10.2) 光标会变成一个照相机,点击可拍下当前窗口或菜单或Dock以及图标等,只要将照相机移动到不用区域(有效区域会显示为浅蓝色)点击。 6)Command-Shift-Control-4再按空格键(适用于10.2) 将选取的窗口或其他区域的快照保存到剪贴板。 7)Command-Shift-Capslock-4(适用于OS9) 将当前的窗口拍下并保存到桌面。 8)Command-Shift-Capslock-Control-4(适用于OS9) 将当前的窗口拍下并保存到剪贴板。
2011年08月26日
[b]退回输入键盘: [/b]
- (BOOL) textFieldShouldReturn:(id)textField{ [textField resignFirstResponder]; }
[b]CGRect [/b]
CGRect frame = CGRectMake (origin.x, origin.y, size.width, size.height);矩形 NSStringFromCGRect(someCG) 把CGRect结构转变为格式化字符串; CGRectFromString(aString) 由字符串恢复出矩形; CGRectInset(aRect) 创建较小或较大的矩形(中心点相同),+较小 -较大 CGRectIntersectsRect(rect1, rect2) 判断两矩形是否交叉,是否重叠 CGRectZero 高度和宽度为零的/位于(0,0)的矩形常量
[b]CGPoint & CGSize [/b]
CGPoint aPoint = CGPointMake(x, y); CGSize aSize = CGSizeMake(width, height);
[b]设置透明度 [/b]
[myView setAlpha:value]; (0.0 [/b]
- (IBActive) someButtonPressed:(id) sender { UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@”Are you sure?” delegate:self cancelButtonTitle:@”No way!” destructiveButtonTitle:@”Yes, I’m Sure!” otherButtonTitles:nil]; [actionSheet showInView:self.view]; [actionSheet release]; }
[b]警告视图 [/b]
- (void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex { if(buttonIndex != [actionSheet cancelButtonIndex][NSString alloc]initWithFormat:@”You can breathe easy, everything went OK.”]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Something was done” message:message delegate:self cancelButtonTitle:@”OK” otherButtonTitles:nil]; [alert show]; [alert release]; [message release]; } }
[b]动画效果 [/b]
-(void)doChange:(id)sender { if(view2 == nil) { [self loadSec]; } [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:1]; [UIView setAnimationTransition:([view1 superview] ? UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight)forView : self.view cache:YES]; if([view1 superview]!= nil) { [view1 removeFromSuperview]; [self.view addSubview:view2]; }else { [view2 removeFromSuperview]; [self.view addSubview:view1]; } [UIView commitAnimations]; }
[b]Table View [/b]
#pragma mark - #pragma mark Table View Data Source Methods //指定分区中的行数,默认为1 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.listData count]; } //设置每一行cell显示的内容 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *SimpleTableIndentifier = @"SimpleTableIndentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableInden tifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SimpleTableIndentifier] autorelease]; } UIImage *image = [UIImage imageNamed:@"13.gif"]; cell.imageView.image = image; NSUInteger row = [indexPath row]; cell.textLabel.text = [listData objectAtIndex:row]; cell.textLabel.font = [UIFont boldSystemFontOfSize:20]; if(row #pragma mark - #pragma mark Table View Delegate Methods //把每一行缩进级别设置为其行号 - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath { NSUInteger row = [indexPath row]; return row; } //获取传递过来的indexPath值 - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSUInteger row = [indexPath row]; if (row == 0) return nil; return indexPath; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSUInteger row = [indexPath row]; NSString *rowValue = [listData objectAtIndex:row]; NSString *message = [[NSString alloc] initWithFormat:@"You selected %@",rowValue]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Row Selected" message:message delegate:nil cancelButtonTitle:@"Yes, I did!" otherButtonTitles:nil]; [alert show]; [alert release]; [message release]; [tableView deselectRowAtIndexPath:indexPath animated:YES]; } //设置行的高度 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 40; }
[b]随机数的使用 [/b]
头文件的引用 #import #import srandom()的使用 srandom((unsigned)(mach_absolute_time() & 0xFFFFFFFF)); 直接使用 random() 来调用随机数
[b]在UIImageView 中旋转图像 [/b]
float rotateAngle = M_PI; CGAffineTransform transform =CGAffineTransformMakeRotation(rotateAngle); imageView.transform = transform;
以上代码旋转imageView, 角度为rotateAngle, 方向可以自己测试哦! [b]在Quartz中如何设置旋转点 [/b]
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg.png"]]; imageView.layer.anchorPoint = CGPointMake(0.5, 1.0);
这个是把旋转点设置为底部中间。记住是在QuartzCore.framework中才得到支持。 [b]创建.plist文件并存储 [/b]
NSString *errorDesc; //用来存放错误信息 NSMutableDictionary *rootObj = [NSMutableDictionary dictionaryWithCapacity:4]; //NSDictionary, NSData等文件可以直接转化为plist文件 NSDictionary *innerDict; NSString *name; Player *player; NSInteger saveIndex; for(int i = 0; i 里的内容 while(tmp = [nse nextObject]) { NSString *stringBetweenBrackets = nil; NSScanner *scanner = [NSScanner scannerWithString:tmp]; [scanner scanUpToString:@"" intoString:&stringBetweenBrackets]; NSLog([stringBetweenBrackets description]); }
对于读写文件,还有补充,暂时到此。随机数和文件读写在游戏开发中经常用到。所以把部分内容放在这,以便和大家分享,也当记录,便于查找。 [b]隐藏NavigationBar [/b]
[self.navigationController setNavigationBarHidden:YES animated:YES];
在想隐藏的ViewController中使用就可以了。 [b]如果无法保证子类行为的一致性,那么就用委托 [/b]
If the subClass cann’t keep with superClass,use delegate rather than inheritance. [b]屏幕上看到的,都是UIVew [/b]
Everything you see on Screen is UIView. [b]如果对性能要求高,慎用Interface Build [/b]
if application’s performance is important,be discreet for the interface build. [b]copy是创建,retain是引用 [/b]
the copy operation is create a new one,but the retain operation is just a reference. [b]alloc需要release,convenient不需要release [/b]
alloc method need corresponding release method,but convenient method not. [b]加载到NSArray/NSMutableArray里的对象,不需要负责release [/b]
The objects added to NSArray/NSMutableArray need not to be released. [b]IBOutlet,IBAction为你开启了访问Interface Build中对象的大门 [/b]
IBOutlet and IBAction open the door to access the objects in Interface build. [b]UIApplicationDelegate负责应用程序的生命周期,而UIViewController负责View的生命周期 [/b]
UIApplicationDelegate is responsible for the application life cycle,but UIViewController for the UIView. [b]为了程序的健壮性,请尽量实现Delegate的生命周期函数 [/b]
if you want to develop a robust application,implement the life cycle methods as more as possbile. [b]you触摸的不是UIEvent,而是NSSet的UIView [/b]
what you touch on screen is not UIEvent but UIView [b]UITextField不响应键盘: [/b]
方法1: TextField的的Touch Cancel响应中,添加[textFied resignFirstResponder]; 方法: - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ [textFied resignFirstResponder]; }
[b]更改响应键盘return按钮: [/b]
TextField.returnKeyType=UIReturnKeyDone; select: UIReturnKeyDefault, UIReturnKeyGo, UIReturnKeyGoogle, UIReturnKeyJoin, UIReturnKeyNext, UIReturnKeyRoute, UIReturnKeySearch, UIReturnKeySend, UIReturnKeyYahoo, UIReturnKeyDone, UIReturnKeyEmergencyCall,
[b]尺寸问题: [/b]
iPhone应用程序图标大小:57*57; iPhone全屏UIView大小:320*460 添加UITabBar后大小:320*411 UITabelViewCell默认大小: 320*44
[b]绘制控件方法 [/b]
//--alloc -(UITextField *)GetDefaultTextField:(CGRect)frame{ UITextField *textField=[[UITextField alloc] initWithFrame:frame]; textField.borderStyle=UITextBorderStyleRoundedRect ; textField.font=[UIFont fontWithName:@"Arial" size:12.0]; textField.textAlignment=UITextAlignmentCenter; textField.contentVerticalAlignment=UIControlConten tVerticalAlignmentCenter; textField.keyboardType=UIKeyboardTypeNumbersAndPun ctuation; textField.returnKeyType=UIReturnKeyDone; textField.delegate=self; return textField; } //--alloc -(UILabel *)GetDefaultLabel:(CGRect)frame{ UILabel *label = [[UILabel alloc] initWithFrame: frame]; label.textAlignment=UITextAlignmentCenter; label.textColor=[UIColor blackColor]; label.backgroundColor=[UIColor clearColor]; label.font=[UIFont boldSystemFontOfSize:12.0]; return label; } //--alloc -(UIButton *)GetDefaultButton:(CGRect)frame{ UIButton *button=[[UIButton alloc] initWithFrame:frame]; [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; [button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted]; [button setContentHorizontalAlignment:UIControlContentHori zontalAlignmentLeft]; [button.titleLabel setFont:[UIFont boldSystemFontOfSize:14.0]]; [button.titleLabel setLineBreakMode:UILineBreakModeCharacterWrap]; [button addTarget:self action:@selector(btnTradeTouchUpInside:) forControlEvents:UIControlEventTouchUpInside]; [button setContentHorizontalAlignment:UIControlContentHori zontalAlignmentCenter]; [button setBackgroundImage:[UIImage imageNamed:@"png1.png"] forState:UIControlStateNormal]; [button setBackgroundColor:[UIColor lightGrayColor]]; button.tag=kButtonTag; return button;}
[b]多使用宏定义常量。tag,frame大小,一些判断标志位。 [/b]
#define kIndexValueTag 1
[b]苹果屏幕截图快捷键 [/b]
一般在Mac上用Command-Shif-3/4来截图。注:Command=苹果键 其实还有几个辅助键,来起到不同的截图功能…… 1)Command-Shift-3(适用于OS9,10.1X和10.2) 将整个屏幕拍下并保存到桌面。 2)Command-Shift-4(适用于OS9,10.1X和10.2) 将屏幕的一部分拍下并保存到桌面。当按下着几个键后,光标会变为一个十字,可以拖拉来选取拍报区域。 3)Command-Shift-Control-3(适用于OS9和10.2) 将整个屏幕拍下并保存到剪贴板,可以Command+V直接粘贴到如Photoshop等软件中编辑。 4)Command-Shift-Control-4(适用于OS9和10.2) 将屏幕的一部分拍下并保存到剪贴板。 5)Command-Shift-4再按空格键(适用于10.2) 光标会变成一个照相机,点击可拍下当前窗口或菜单或Dock以及图标等,只要将照相机移动到不用区域(有效区域会显示为浅蓝色)点击。 6)Command-Shift-Control-4再按空格键(适用于10.2) 将选取的窗口或其他区域的快照保存到剪贴板。 7)Command-Shift-Capslock-4(适用于OS9) 将当前的窗口拍下并保存到桌面。 8)Command-Shift-Capslock-Control-4(适用于OS9) 将当前的窗口拍下并保存到剪贴板。