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;
頭文件的引用
#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方法就可以寫成文件。下麵是代碼:
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"];
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”];
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];
在想隱藏的ViewController中使用就可以了。
轉自iOS分享網--https://iosshare.cnwhile(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];
在想隱藏的ViewController中使用就可以了。
最後更新:2017-04-02 15:14:44