iPhone開發小技術

來源:互聯網
上載者:User

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];

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.