iPhone 开发过程中的一些小技术的总结

2010年2月9日 | laifangwen

1 随机数的使用

  1. 头文件的引用
     
  2.         #import <time.h>
     
  3.         #import <mach/mach_time.h>
     

  4.  
  5.         srandom()的使用
     
  6.         srandom((unsigned)(mach_absolute_time() & 0xFFFFFFFF));
     

  7.  
  8.         直接使用 random() 来调用随机数
复制代码

2 在UIImageView 中旋转图像

  1. float rotateAngle = M_PI;
     
  2.         CGAffineTransform transform =CGAffineTransformMakeRotation(rotateAngle);
     
  3.         imageView.transform = transform;
复制代码

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

3 在Quartz中如何设置旋转点

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

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

4 创建.plist文件并存储

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

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

  1.    /*得到移动设备上的文件存放位置*/
     
  2.         NSString *documentsPath = [self getDocumentsDirectory];
     
  3.         NSString *savePath = [documentsPath stringByAppendingPathComponent:@"save.plist"];
     
  4.    
     
  5.         /*存文件*/
     
  6.         if (plistData) {
     
  7.                 [plistData writeToFile:savePath atomically:YES];
     
  8.          }
     
  9.          else {
     
  10.                 NSLog(errorDesc);
     
  11.                 [errorDesc release];
     
  12.         }
     

  13.  
  14.         - (NSString *)getDocumentsDirectory {  
     
  15.                 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
     
  16.                 return [paths objectAtIndex:0];  
     
  17.         }
复制代码

5 读取plist文件并转化为NSDictionary

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

6 读取一般性文档文件

  1.     NSString *tmp;
     
  2.         NSArray *lines; /*将文件转化为一行一行的*/
     
  3.         lines = [[NSString    stringWithContentsOfFile:@"testFileReadLines.txt"]
     
  4.                        componentsSeparatedByString:@"\n"];
     
  5.    
     
  6.          NSEnumerator *nse = [lines objectEnumerator];
复制代码

7 隐藏NavigationBar

  1. [self.navigationController setNavigationBarHidden:YES animated:YES];
复制代码

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

  1. // 读取<>里的内容
     
  2.          while(tmp = [nse nextObject]) {
     
  3.                   NSString *stringBetweenBrackets = nil;
     
  4.                   NSScanner *scanner = [NSScanner scannerWithString:tmp];
     
  5.                   [scanner scanUpToString:@"<" intoString:nil];
     
  6.                   [scanner scanString:@"<" intoString:nil];
     
  7.                   [scanner scanUpToString:@">" intoString:&stringBetweenBrackets];
     

  8.  
  9.                   NSLog([stringBetweenBrackets description]);
     
  10.           }
复制代码

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

Tags: UINavigationController  iPhone  object-c  navigation  viewcontroller  
相关文章:

控制UINavigationController跳转的方法  (2010-2-9 11:16:35)

iPhone程序开发的视图和控制器的层次图  (2010-2-9 10:55:37)

自定义NavigationBar的backBarButtonItem  (2010-2-9 10:46:36)

【翻译】在iPhone上使用UITableView开发九宫格视图  (2009-12-28 13:9:16)

【分享】 iPhone开发经典语录集锦  (2009-12-26 10:22:23)

收藏:iPhone开发技术多线程之NSInvocationOperation  (2009-8-28 16:53:10)

Three20 TTPhotoViewController使用向导  (2009-8-28 16:46:54)

UITable View实例教程:分组显示列表  (2009-6-18 11:31:30)

UITableView实例教程:创建Table View的detail view  (2009-6-16 17:32:57)

UITableView使用实例:创建一个简单的Table View  (2009-6-16 11:40:59)

引用通告地址: http://blog.laifangwen.com/cmd.asp?act=gettburl&id=271

评论: 0 | 引用: 0 | 浏览:
名称(*):
邮箱:
网站链接:
正文(*):
选 项:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。