iphone開發中的一些小技術總結
1 隨機數的使用
頭文件的引用
#import <time.h>
#import <mach/mach_time.h>
srandom()的使用
srandom((unsigned)(mach_absolute_time() & 0xFFFFFFFF));
直接使用 random() 來調用隨機數
2 在UIImageView 中旋轉圖像
float rotateAngle = M_PI;
CGAffineTransform transform =CGAffineTransformMakeRotation(rotateAngle);
imageView.transform = transform;
以上代碼旋轉imageView, 角度爲rotateAngle, 方向可以自己測試哦!
3 在Quartz中如何設置旋轉點
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"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 = [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];
紅色部分可以忽略,只是給rootObj添加一點內容。這個plistData爲創建好的plist文件,用其writeToFile方法就可以寫成文件。下面是代碼:
/*得到移動設備上的文件存放位置*/
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];
}
4 讀取plist文件並轉化爲NSDictionary
NSString *documentsPath = [self getDocumentsDirectory];
NSString *fullPath = [documentsPath stringByAppendingPathComponent:@"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];
[scanner scanUpToString:@"<" intoString:nil];
[scanner scanString:@"<" intoString:nil];
[scanner scanUpToString:@">" intoString:&stringBetweenBrackets];
NSLog([stringBetweenBrackets description]);
}
對於讀寫文件,還有補充,暫時到此。隨機數和文件讀寫在遊戲開發中經常用到。所以把部分內容放在這,以便和大家分享,也當記錄,便於查找。
6 隱藏NavigationBar
[self.navigationController setNavigationBarHidden:YES animated:YES];
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.