博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS小技巧整理
阅读量:4567 次
发布时间:2019-06-08

本文共 4196 字,大约阅读时间需要 13 分钟。

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可以很准确的做到。

 

 

如果在某些特殊的字段上,你不想用默认的键盘,而是用类似于Picker这样的拾取器,你只需要设置inputView就行了,用你自定义的视图去替换掉键盘;如果你想在键盘上面再增加一个视图,比如toolbar,那么你可以不用自己对toolbar的位置进行控制,只需要设置inputAccessoryView就行了,这个值默认为nil,设置的视图将在你的控件变成第一响应者的时候显示在inputView的上方。

转载于:https://www.cnblogs.com/wcLT/p/4731393.html

你可能感兴趣的文章
Hive内置函数详解
查看>>
【转】MyEclipse快捷键大全
查看>>
IT职业技能图谱10--Hadoop家族技能图谱
查看>>
Java - 反射(1)
查看>>
控制台中显示执行的Sql语句
查看>>
Linux(Centos7)下搭建SVN服务器
查看>>
安卓开发的Tasks and Back Stack
查看>>
Ansi,UTF8,Unicode编码
查看>>
原子变量的性能问题
查看>>
Sybase PowerDesigner 15.0 完美版+特别文件
查看>>
快速傅立叶之二
查看>>
cetos 6.3 安装 apache+mysql+php
查看>>
js编写简单的贪吃蛇游戏
查看>>
2018/12/01 一个64位操作系统的实现 第四章 导入kernel.bin(4)
查看>>
如何在windows xp professional安装iis的解决方法
查看>>
抽象类和接口
查看>>
使用ASP.NET Atlas AutoComplete Behavior或AutoComplete Extender实现自动完成功能(下)
查看>>
golang 常见疑惑总结
查看>>
8大你不得不知的Android调试工具
查看>>
pc端元素拖拽
查看>>