1 随机数的使用
头文件的引用
#import<time.h>
#import<mach/mach_time.h>
srandom()的使用
srandom((unsigned)(mach_absolute_time()& 0xFFFFFFFF));
直接使用 random() 来调用随机数
2 在UIImageView 中旋转图像
floatrotateAngle = M_PI;
CGAffineTransform transform=CGAffineTransformMakeRotation(rotateAngle);
imageView.transform =transform;
以上代码旋转imageView,角度为rotateAngle, 方向可以自己测试哦!
3 在Quartz中如何设置旋转点
UIImageView*imageView = [[UIImageView alloc] initWithImage:[UIImageimageNamed:@"bg.png"]];
imageView.layer.anchorPoint= CGPointMake(0.5, 1.0);
这个是把旋转点设置为底部中间。记住是在QuartzCore.framework中才得到支持。
4 创建.plist文件并存储
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 = [playerArrayobjectAtIndex:i];
if(player == nil)
break;
name = player.playerName;//This "Player1" denotes the player name could also be the computername
innerDict = [selfgetAllNodeInfoToDictionary:player];
[rootObj setObject:innerDictforKey:name]; // This "Player1" denotes the person who start thisgame
}
player = nil;
NSData*plistData = [NSPropertyListSerializationdataFromPropertyList:(id)rootObjformat:NSPropertyListXMLFormat_v1_0errorDescription:&errorDesc];
红色部分可以忽略,只是给rootObj添加一点内容。这个plistData为创建好的plist文件,用其writeToFile方法就可以写成文件。下面是代码:
NSString *documentsPath =[self getDocumentsDirectory];
NSString *savePath =[documentsPathstringByAppendingPathComponent:@"save.plist"];
if (plistData) {
[plistDatawriteToFile:savePath atomically:YES];
}
else {
NSLog(errorDesc);
[errorDescrelease];
}
-(NSString *)getDocumentsDirectory{
NSArray*paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
return[pathsobjectAtIndex:0];
}
4 读取plist文件并转化为NSDictionary
NSString*documentsPath = [self getDocumentsDirectory];
NSString *fullPath =[documentsPathstringByAppendingPathComponent:@"save.plist"];
NSMutableDictionary* plistDict= [[NSMutableDictionary alloc]initWithContentsOfFile:fullPath];
5 读取一般性文档文件
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];
[scannerscanUpToString:@"<" intoString:nil];
[scannerscanString:@"<" intoString:nil];
[scannerscanUpToString:@">"intoString:&stringBetweenBrackets];