码迷,mamicode.com
首页 > 其他好文 > 详细

基础知识3

时间:2015-03-01 13:05:09      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

在Quartz中如何设置旋转点

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

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

创建.plist文件并存储

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

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

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

读取plist文件并转化为NSDictionary

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

读取一般性文档文件

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

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

隐藏NavigationBar

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

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

基础知识3

标签:

原文地址:http://www.cnblogs.com/haolianxue/p/4306851.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!