天天看點

IOS小技巧整理

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

UIKeyboardWillShowNotification

UIKeyboardWillHideNotification

UIKeyboard...

iOS 5新增加了一些

UIKeyboardDidChangeFrameNotification(will)

一般情況下,前兩個事件已經可以完成你要做的事情。在你的事件處理方法中加上NSNotification參數可以為你獲得更多的東西:

  1. - (void)keyboardWillShow:(NSNotification *)notification  
  2. {   
  3.     CGPoint beginCentre = [[[notification userInfo] valueForKey:UIKeyboardCenterBeginUserInfoKey] CGPointValue];  
  4.     CGPoint endCentre = [[[notification userInfo] valueForKey:UIKeyboardCenterEndUserInfoKey] CGPointValue];  
  5.     CGRect keyboardBounds = [[[notification userInfo] valueForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];  
  6.     CGRect keyboardFrames = [[[notification userInfo] valueForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];  
  7.     UIViewAnimationCurve animationCurve = [[[notification userInfo] valueForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];  
  8.     NSTimeInterval animationDuration = [[[notification userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];  
  9. }  

如果你要在程式中使用鍵盤的高度和寬度,永遠不要嘗試去手動指定,動态擷取也很簡單而且一定準确,不會出現鍵盤擋住輸入框的問題。

你可以利用這些參數把動畫做的和鍵盤一緻。假設你要把一個控件放在Window上,并且想讓它的互動方式和鍵盤一樣,

如果隻是簡單的做個向下偏移動畫并不能很好的完成,因為你還要考慮從導航欄中Pop出來的時候,這個時候的鍵盤動畫是

在x軸上偏移的,你用UIKeyboardFrameEndUserInfoKey擷取的frame可以很準确的做到。