iOS开发笔记

移动开发 iOS
作为iOS开发者,大家获取对它的基础知识有了些了解,但是对于它所深入的东西或许有的还接触不到,下面,就将技术大牛整理好的开发笔记奉献给大家。

退回输入键盘:

  1.  - (BOOL) textFieldShouldReturn:(id)textField{  
  2.     [textField  resignFirstResponder];  
  3. }   

CGRect

CGPoint & CGSize

  1. CGPoint aPoint = CGPointMake(x, y);    CGSize aSize = CGSizeMake(width, height);   

设置透明度

  1. [myView setAlpha:value];   (0.0 < value < 1.0)   

 

 

 

 

设置背景色

  1. [myView setBackgroundColor:[UIColor redColor]];  
  2.    (blackColor;darkGrayColor;lightGrayColor;whiteColor;grayColor; redColor; greenColor; blueColor; cyanColor;yellowColor;magentaColor;  
  3. orangeColor;purpleColor;brownColor; clearColor; )   

自定义颜色:

  1. UIColor *newColor = [[UIColor alloc] initWithRed:(float) green:(float) blue:(float) alpha:(float)];      0.0~1.0   

宽度和高度

1
768X1024     1024X768    状态栏高 20 像素高   导航栏 工具栏 44像素高

隐藏状态栏:

  1. [[UIApplication shareApplication] setStatusBarHidden: YES animated:NO]   

横屏:

  1. [[UIApplication shareApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight].  
  2. orientation == UIInterfaceOrientationLandscapeLeft  
  3. window=[[UIWindow alloc] initWithFrame:[UIScreen mainScreen] bounds];全屏   

自动适应父视图大小:

  1. aView.autoresizingSubviews = YES;  
  2. aView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);   

定义按钮

  1.  UIButton *scaleUpButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  2. [scaleUpButton setTitle:@"放 大"forState:UIControlStateNormal];  
  3. scaleUpButton.frame = CGRectMake(40, 420, 100, 40);  
  4. [scaleUpButton addTarget:self action:@selector(scaleUp) forControlEvents:UIControlEventTouchUpInside];   

设置视图背景图片

  1. UIImageView *aView;  
  2. [aView setImage:[UIImage imageNamed:@”name.png”]];  
  3. view1.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"image1.png"]];  
  4.    
  5. UISlider *slider = (UISlider *) sender;  
  6. NSString *newText = [[NSString alloc] initWithFormat:@”%d”, (int)(slider.value + 0.5f)];  
  7. label.text = newText;   

活动表单 <UIActionSheetDelegate>

  1. - (IBActive) someButtonPressed:(id) sender  
  2. {  
  3.     UIActionSheet *actionSheet = [[UIActionSheet alloc]  
  4.                     initWithTitle:@”Are you sure?”  
  5.                     delegate:self  
  6.                     cancelButtonTitle:@”No way!”  
  7.                     destructiveButtonTitle:@”Yes, I’m Sure!”  
  8.                     otherButtonTitles:nil];  
  9.     [actionSheet showInView:self.view];  
  10.     [actionSheet release];  
  11. }   

警告视图 <UIAlertViewDelegate>

  1.   - (void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex  
  2. {  
  3.      if(buttonIndex != [actionSheet cancelButtonIndex])  
  4.      {  
  5.           NSString *message = [[NSString alloc] initWithFormat:@”You can  
  6.                    breathe easy, everything went OK.”];  
  7.           UIAlertView *alert = [[UIAlertView alloc]  
  8.                                initWithTitle:@”Something was done”  
  9.                                 message:message  
  10.                                 delegate:self  
  11.                                 cancelButtonTitle:@”OK”  
  12.                                 otherButtonTitles:nil];  
  13.           [alert show];  
  14.           [alert release];  
  15.           [message release];  
  16.      }  
  17. }   

动画效果

  1.  -(void)doChange:(id)sender  
  2. {  
  3. if(view2 == nil)  
  4. {  
  5. [self loadSec];  
  6. }  
  7. [UIView beginAnimations:nil context:NULL];  
  8. [UIView setAnimationDuration:1];  
  9. [UIView setAnimationTransition:([view1 superview] ? UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight)forView : self.view cache:YES];  
  10.    
  11.     if([view1 superview]!= nil)  
  12. {  
  13. [view1 removeFromSuperview];  
  14. [self.view addSubview:view2];  
  15.    
  16. }else{  
  17.    
  18. [view2 removeFromSuperview];  
  19. [self.view addSubview:view1];  
  20. }  
  21. [UIView commitAnimations];  
  22. }   

Table View <UITableViewDateSource>

  1.  
  2.  #pragma mark -  
  3. #pragma mark Table View Data Source Methods  
  4. //指定分区中的行数,默认为1  
  5. - (NSInteger)tableView:(UITableView *)tableView  
  6.  numberOfRowsInSection:(NSInteger)section  
  7. {  
  8. return[self.listDatacount];  
  9. }  
  10.    
  11. //设置每一行cell显示的内容  
  12. - (UITableViewCell *)tableView:(UITableView *)tableView  
  13. cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  14. {  
  15. staticNSString *SimpleTableIndentifier = @"SimpleTableIndentifier";  
  16. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIndentifier];  
  17. if(cell == nil) {  
  18. cell = [[[UITableViewCell alloc]  
  19. initWithStyle:UITableViewCellStyleSubtitle  
  20. reuseIdentifier:SimpleTableIndentifier]  
  21. autorelease];  
  22. }  
  23.      UIImage *image = [UIImage imageNamed:@"13.gif"];  
  24. cell.imageView.image = image;  
  25.    
  26. NSUInteger row = [indexPath row];  
  27. cell.textLabel.text = [listData objectAtIndex:row];  
  28.      cell.textLabel.font = [UIFont boldSystemFontOfSize:20];  
  29.    
  30.      if(row < 5)  
  31. cell.detailTextLabel.text = @"Best friends";  
  32. else  
  33.     cell.detailTextLabel.text = @"friends";  
  34. returncell;  
  35. }   

图像:如果设置图像,则它显示在文本的左侧

文本标签:这是单元的主要文本(UITableViewCellStyleDefault 只显示文本标签)

详细文本标签:这是单元的辅助文本,通常用作解释性说明或标签

  1.  
  2.  
  3.   
  4.  
  5.  UITableViewCellStyleSubtitle  
  6. UITableViewCellStyleDefault  
  7. UITableViewCellStyleValue1  
  8. UITableViewCellStyleValue2  
  9.    
  10. <UITableViewDelegate>  
  11. #pragma mark -  
  12. #pragma mark Table View Delegate Methods  
  13. //把每一行缩进级别设置为其行号  
  14. - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath  
  15. {  
  16. NSUInteger row = [indexPath row];  
  17. returnrow;  
  18. }  
  19. //获取传递过来的indexPath值  
  20. - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  21. {  
  22. NSUInteger row = [indexPath row];  
  23. if(row == 0)  
  24. returnnil;  
  25. returnindexPath;  
  26. }  
  27.    
  28. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  29. {  
  30. NSUInteger row = [indexPath row];  
  31. NSString *rowValue = [listData objectAtIndex:row];  
  32. NSString *message = [[NSString alloc] initWithFormat:@"You selected %@",rowValue];  
  33. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Row Selected"  
  34. message:message  
  35.     delegate:nil  
  36.   cancelButtonTitle:@"Yes, I did!"  
  37.   otherButtonTitles:nil];  
  38. [alert show];  
  39. [alert release];  
  40. [message release];  
  41. [tableView deselectRowAtIndexPath:indexPath animated:YES];  
  42. }  
  43.    
  44. //设置行的高度  
  45. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  
  46. {  
  47. return40;  
  48. }   

随机数的使用

 

  1. 头文件的引用 
  2. #import <time.h> 
  3. #import <mach/mach_time.h> 
  4.   
  5. srandom()的使用 
  6. srandom((unsigned)(mach_absolute_time() & 0xFFFFFFFF)); 
  7.   
  8. 直接使用 random() 来调用随机数 

在UIImageView 中旋转图像

  1. float rotateAngle = M_PI;  
  2. CGAffineTransform transform =CGAffineTransformMakeRotation(rotateAngle);  
  3. imageView.transform = transform;   

以上代码旋转imageView, 角度为rotateAngle, 方向可以自己测试哦!

在Quartz中如何设置旋转点

  1. UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg.png"]];  
  2. imageView.layer.anchorPoint = CGPointMake(0.5, 1.0);   

这个是把旋转点设置为底部中间。记住是在QuartzCore.framework中才得到支持。

#p#

创建.plist文件并存储

  1.   
  2.  NSString *errorDesc; //用来存放错误信息  
  3. NSMutableDictionary *rootObj = [NSMutableDictionary dictionaryWithCapacity:4];//NSDictionary, NSData等文件可以直接转化为plist文件  
  4. NSDictionary *innerDict;  
  5. NSString *name;  
  6. Player *player;  
  7. NSInteger saveIndex;  
  8.    
  9. for(int i = 0; i < [playerArraycount]; i++) {  
  10.       player = nil;  
  11.       player = [playerArray objectAtIndex:i];  
  12.       if(player == nil)  
  13.              break;  
  14.       name = player.playerName;// This “Player1″ denotes the player name could also be the computer name  
  15.       innerDict = [self getAllNodeInfoToDictionary:player];  
  16.       [rootObj setObject:innerDict forKey:name];// This “Player1″ denotes the person who start this game  
  17. }  
  18. player = nil;  
  19. NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:(id)rootObj format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorDesc];   

***2行可以忽略,只是给rootObj添加一点内容。这个plistData为创建好的plist文件,用其writeToFile方法就可以写成文件。下面是代码:

  1.  
  2. 17  /*得到移动设备上的文件存放位置*/  
  3.         NSString *documentsPath = [self getDocumentsDirectory];  
  4.         NSString *savePath = [documentsPath stringByAppendingPathComponent:@"save.plist"];  
  5.    
  6.         /*存文件*/  
  7.         if(plistData) {  
  8.                 [plistData writeToFile:savePath atomically:YES];  
  9.          }  
  10.          else{  
  11.                 NSLog(errorDesc);  
  12.                 [errorDesc release];  
  13.         }  
  14.    
  15.         - (NSString *)getDocumentsDirectory {  
  16.                 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  17.                 return[paths objectAtIndex:0];  
  18.         }   

读取plist文件并转化为NSDictionary

  1.   
  2. NSString *documentsPath = [self getDocumentsDirectory];  
  3. NSString *fullPath = [documentsPath stringByAppendingPathComponent:@"save.plist"];  
  4. NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:fullPath];   

读取一般性文档文件

  1. NSString *tmp;  
  2. NSArray *lines;/*将文件转化为一行一行的*/  
  3. lines = [[NSString    stringWithContentsOfFile:@"testFileReadLines.txt"]  
  4.                componentsSeparatedByString:@”\n”];  
  5.    
  6.  NSEnumerator *nse = [lines objectEnumerator];  
  7.    
  8.  // 读取<>里的内容  
  9.  while(tmp = [nse nextObject]) {  
  10.           NSString *stringBetweenBrackets = nil;  
  11.           NSScanner *scanner = [NSScanner scannerWithString:tmp];  
  12.           [scanner scanUpToString:@"<"intoString:nil];  
  13.           [scanner scanString:@"<"intoString:nil];  
  14.           [scanner scanUpToString:@">"intoString:&stringBetweenBrackets];  
  15.    
  16.           NSLog([stringBetweenBrackets description]);  
  17.   }   

对于读写文件,还有补充,暂时到此。随机数和文件读写在游戏开发中经常用到。所以把部分内容放在这,以便和大家分享,也当记录,便于查找。

隐藏NavigationBar

  1. [self.navigationController setNavigationBarHidden:YES animated:YES];   

在想隐藏的ViewController中使用就可以了。

如果无法保证子类行为的一致性,那么就用委托

  1. If the subClass cann’t keep with superClass,use delegate rather than inheritance.  

屏幕上看到的,都是UIVew

  1. Everything you see on Screen is UIView.  

如果对性能要求高,慎用Interface Build

  1. if application’s performance is important,be discreet for the interface build.  

copy是创建,retain是引用

  1. the copy operation is create a new one,but the retain operation is just a reference.  

alloc需要release,convenient不需要release

  1. alloc method need corresponding release method,but convenient method not.  

加载到NSArray/NSMutableArray里的对象,不需要负责release

  1. The objects added to NSArray/NSMutableArray need not to be released.  

IBOutlet,IBAction为你开启了访问Interface Build中对象的大门

  1. IBOutlet and IBAction open the door to access the objects in Interface build.  

UIApplicationDelegate负责应用程序的生命周期,而UIViewController负责View的生命周期

  1. UIApplicationDelegate is responsible for the application life cycle,but UIViewController for the UIView.  

为了程序的健壮性,请尽量实现Delegate的生命周期函数

  1. if you want to develop a robust application,implement the life cycle methods as more as possbile.  

you触摸的不是UIEvent,而是NSSet的UIView

  1. what you touch on screen is not UIEvent but UIView  

UITextField不响应键盘:

  1. 方法1: TextField的的Touch Cancel响应中,添加[textFied resignFirstResponder];  
  2.    
  3.     方法: - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{  
  4.    
  5. [textFied resignFirstResponder]; }   

更改响应键盘return按钮:

  1. TextField.returnKeyType=UIReturnKeyDone;  
  2. select:  
  3.    UIReturnKeyDefault,  
  4.    UIReturnKeyGo,  
  5.    UIReturnKeyGoogle,  
  6.    UIReturnKeyJoin,  
  7.    UIReturnKeyNext,  
  8.    UIReturnKeyRoute,  
  9.    UIReturnKeySearch,  
  10.    UIReturnKeySend,  
  11.    UIReturnKeyYahoo,  
  12.    UIReturnKeyDone,  
  13.    UIReturnKeyEmergencyCall,   

尺寸问题:

 

  1. iPhone应用程序图标大小:57*57; 
  2.   
  3. iPhone全屏UIView大小:320*460 添加UITabBar后大小:320*411 
  4.   
  5. UITabelViewCell默认大小: 320*44 

绘制控件方法

  1.  
  2.   //--alloc  
  3. -(UITextField *)GetDefaultTextField:(CGRect)frame{  
  4.    
  5.     UITextField *textField=[[UITextField alloc] initWithFrame:frame];  
  6.     textField.borderStyle=UITextBorderStyleRoundedRect;  
  7.     textField.font=[UIFont fontWithName:@"Arial"size:12.0];  
  8.     textField.textAlignment=UITextAlignmentCenter;  
  9.     textField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;  
  10.     textField.keyboardType=UIKeyboardTypeNumbersAndPunctuation;  
  11.     textField.returnKeyType=UIReturnKeyDone;  
  12.     textField.delegate=self;  
  13.     returntextField;  
  14.    
  15. }  
  16. //--alloc  
  17. -(UILabel *)GetDefaultLabel:(CGRect)frame{  
  18.    
  19.     UILabel *label = [[UILabel alloc] initWithFrame: frame];  
  20.     label.textAlignment=UITextAlignmentCenter;  
  21.     label.textColor=[UIColor blackColor];  
  22.     label.backgroundColor=[UIColor clearColor];  
  23.     label.font=[UIFont boldSystemFontOfSize:12.0];  
  24.     returnlabel;  
  25. }  
  26. //--alloc  
  27. -(UIButton *)GetDefaultButton:(CGRect)frame{  
  28.    
  29.     UIButton *button=[[UIButton alloc] initWithFrame:frame];  
  30.     [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];  
  31.     [button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];  
  32.     [button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];  
  33.     [button.titleLabel setFont:[UIFont boldSystemFontOfSize:14.0]];  
  34.     [button.titleLabel setLineBreakMode:UILineBreakModeCharacterWrap];  
  35.     [button addTarget:self action:@selector(btnTradeTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];  
  36.     [button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];  
  37.    
  38.                 [button setBackgroundImage:[UIImage imageNamed:@"png1.png"] forState:UIControlStateNormal];  
  39.                 [button setBackgroundColor:[UIColor lightGrayColor]];  
  40.                 button.tag=kButtonTag;  
  41.    
  42.      returnbutton;}   

多使用宏定义常量。tag,frame大小,一些判断标志位。

1
#define kIndexValueTag 1

苹果屏幕截图快捷键

一般在Mac上用Command-Shif-3/4来截图。注:Command=苹果键 其实还有几个辅助键,来起到不同的截图功能……

 

  1. 1)Command-Shift-3(适用于OS9,10.1X和10.2) 
  2. 将整个屏幕拍下并保存到桌面。 
  3. 2)Command-Shift-4(适用于OS9,10.1X和10.2) 
  4. 将屏幕的一部分拍下并保存到桌面。当按下着几个键后,光标会变为一个十字,可以拖拉来选取拍报区域。 
  5. 3)Command-Shift-Control-3(适用于OS9和10.2) 
  6. 将整个屏幕拍下并保存到剪贴板,可以Command+V直接粘贴到如Photoshop等软件中编辑。 
  7. 4)Command-Shift-Control-4(适用于OS9和10.2) 
  8. 将屏幕的一部分拍下并保存到剪贴板。 
  9. 5)Command-Shift-4再按空格键(适用于10.2) 
  10. 光标会变成一个照相机,点击可拍下当前窗口或菜单或Dock以及图标等,只要将照相机移动到不用区域(有效区域会显示为浅蓝色)点击。 
  11. 6)Command-Shift-Control-4再按空格键(适用于10.2) 
  12. 将选取的窗口或其他区域的快照保存到剪贴板。 
  13. 7)Command-Shift-Capslock-4(适用于OS9) 
  14. 将当前的窗口拍下并保存到桌面。 
  15. 8)Command-Shift-Capslock-Control-4(适用于OS9) 
  16. 将当前的窗口拍下并保存到剪贴板。 
责任编辑:张叶青 来源: 开源社区
相关推荐

2015-10-14 10:16:26

安安卓开发EventBus

2012-05-17 11:45:12

iPhone

2021-11-11 17:36:07

鸿蒙HarmonyOS应用

2022-08-15 22:09:37

设备开发开发笔记

2011-08-03 10:49:46

2009-06-17 14:33:08

java项目开发

2021-02-03 09:59:02

鸿蒙HarmonyOS应用开发

2011-08-22 15:43:08

IOS开发数据库

2022-08-09 07:57:25

Linux操作系统Windows

2011-08-16 14:59:31

IOS开发ViewDidUnloiOS 5

2011-08-03 09:44:18

IOS开发 UITextFiel UITableVie

2011-08-09 16:08:58

IOS游戏Cocos2d

2012-05-14 16:59:40

iOS

2013-03-28 09:45:34

iOS学习笔记总结整理

2015-09-28 11:23:09

iOS8iOS 9 开发

2014-07-29 13:25:43

WWDC 2014 S

2011-08-17 13:27:08

iPhone游戏开发objective-c

2014-07-23 13:17:53

iOSUITextField

2016-03-18 09:36:13

ios基础框架

2011-05-11 09:55:18

iOSObjective-C
点赞
收藏

51CTO技术栈公众号