[iOS学习笔记]自学过程中积累的知识点(一)

来源:互联网 发布:连接数据库的方法 编辑:程序博客网 时间:2024/05/14 09:57

1. tag的作用

用于在事件中通过sender.tag确定是哪个控件传出的消息

2. UIView的三个位置属性

  • frame 可通过size和origin获取、设置控件的位置、大小
  • bounds 通过设置size可以改变控件的大小(origin的x、y值都为0)
  • center 通过设置origin可以改变控件的中心位置

UIView不能直接修改本身的frame、bounds、center来改变自身的位置、大小只能先将其取出来,然后修改,最后赋值进去
e.g.:

CGRect rect = self.headImageView.frame; // 1. 取值rect.origin.y -= kMovingDelta;          // 2. 修改self.headImageView.frame = rect;        // 3. 赋值

3. 设置动画

[UIView beginAnimations:nil context:nil];// 开始动画[UIView setAnimationDelay:2.0];         // 设置动画延迟时间self.headImageView.bounds = rect;       // 需要伴随着动画的代码写在这里self.headImageView.alpha = 0.5;[UIView commitAnimations];              // 提交动画(开始动画)

4. 手写UI代码

-(void) viewDidLoad {    UIButton *btn = [[UIButton alloc] init];    btn.frame = CGRectMake(20, 20, 100, 100);       // 指定位置    [btn setTitle: @"点我啊" forState:UIControlStateNormal];    [btn setBackgroundImage:[UIImage imageNamed: @"btn_01"] forState:UIControlStateNormal];    [btn setTitleColor:[UIColor cyanColor] forState:UIControlStateNormal];    [btn setTitle: @"摸我啊" forState:UIControlStateHighlighted];    [btn setBackgroundImage:[UIImage imageNamed: @"btn_02"] forState:UIControlStateHighlighted];    [self.view addSubview: btn];                    // 添加视图}

5. UIImageView动画

-(void) tomAnimation:(NSString *) img count:(int) count{    if ([self.tom isAnimating]) return;    NSMutableArray *array = [NSMutableArray array];    for (int i = 0; i < count; i++) {        NSString *imageName = [NSString stringWithFormat:@"%@_%02d", img, i];        NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:@"jpg"];        UIImage *image = [UIImage imageWithContentsOfFile: path];        [array addObject:image];    }    [self.tom setAnimationImages:array];    [self.tom setAnimationDuration: count * 0.075];    [self.tom setAnimationRepeatCount: 1];    [self.tom startAnimating];       //开始动画    [self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration];    // 在动画结束后清除缓存}

6. 将plist数据加载进NSArray

-(NSArray *) appList{    if (!_appList) {        NSString *path = [[NSBundle mainBundle] pathForResource:@"app" ofType:@"plist"];        NSArray *array = [NSArray arrayWithContentsOfFile: path];        NSMutableArray *arrayM = [NSMutableArray array];        for (NSDictionary *dict in array) {            AppInfo *appInfo = [AppInfo appInfoWithDict:dict];            [arrayM addObject:appInfo];        }        _appList = arrayM;    }    return _appList;}

7. xib的加载以及使用

NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"AppInfoView" owner:nil options:nil];     // 加载xibUIView *appView = [array firstObject];appView.frame = CGRectMake(x, y, viewWidth, viewHeight);[self.view addSubview: appView];UIImageView *imageView = (UIImageView *)[appView viewWithTag:1];

8. UIView动画

8.1 常用方法

  • +(void)setAnimationDelegate:(id)delegate
    设置动画代理对象,当动画开始或者结束时会发消息给代理对象
  • +(void)setAnimationWillStartSelector:(SEL)selector
    当动画即将开始时,执行delegate对象的selector,并且把beginAnimations:context:中传入的参数传进selector
  • +(void)setAnimationDidStopSelector:(SEL)selector
    当动画结束时,执行delegate对象的selector,并且把beginAnimations:context:中传入的参数传进selector
  • +(void)setAnimationDuration:(NSTimeInterval)duration
    动画的持续时间,秒为单位
  • +(void)setAnimationDelay:(NSTimeInterval)delay
    动画延迟delay秒后再开始
  • +(void)setAnimationStartDate:(NSDate *)startDate
    动画的开始时间,默认为now
  • +(void)setAnimationCurve:(UIViewAnimationCurve)curve
    动画的节奏控制,具体看下面的”备注”
  • +(void)setAnimationRepeatCount:(float)repeatCount
    动画的重复次数
  • +(void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses
    如果设置为YES,代表动画每次重复执行的效果会跟上一次相反
  • +(void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache
    设置视图view的过渡效果, transition指定过渡类型, cache设置YES代表使用视图缓存,性能较好

8.2 Block块动画

  • +(void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

    参数解析:

    • duration:动画的持续时间
    • delay:动画延迟delay秒后开始
    • options:动画的节奏控制
    • animations:将改变视图属性的代码放在这个block中
    • completion:动画结束后,会自动调用这个block
  • +(void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

    参数解析:

    • duration:动画的持续时间
    • view:需要进行转场动画的视图
    • options:转场动画的类型
    • animations:将改变视图属性的代码放在这个block中
    • completion:动画结束后,会自动调用这个block
  • +(void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion
    方法调用完毕后,相当于执行了下面两句代码:
    // 添加toView到父视图
    [fromView.superview addSubview:toView];
    // 把fromView从父视图中移除
    [fromView.superview removeFromSuperview];

    参数解析:

    • duration:动画的持续时间
    • options:转场动画的类型
    • animations:将改变视图属性的代码放在这个block中
    • completion:动画结束后,会自动调用这个block

8.3 UIImageView的帧动画

UIImageView可以让一系列的图片在特定的时间内按顺序显示
相关属性解析:

  • animationImages:要显示的图片(一个装着UIImage的NSArray)
  • animationDuration:完整地显示一次animationImages中的所有图片所需的时间
  • animationRepeatCount:动画的执行次数(默认为0,代表无限循环)

相关方法解析:

  • -(void)startAnimating; 开始动画
  • -(void)stopAnimating; 停止动画
  • -(BOOL)isAnimating; 是否正在运行动画

8.4 UIActivityIndicatorView

UIActivityIndicatorView是一个旋转进度轮,可以用来告知用户有一个操作正在进行中,一般用initWithActivityIndicatorStyle初始化

方法解析:

  • -(void)startAnimating; 开始动画
  • -(void)stopAnimating; 停止动画
  • -(BOOL)isAnimating; 是否正在运行动画

UIActivityIndicatorViewStyle有3个值可供选择:
UIActivityIndicatorViewStyleWhiteLarge //大型白色指示器
UIActivityIndicatorViewStyleWhite //标准尺寸白色指示器
IUIActivityIndicatorViewStyleGray //灰色指示器,用于白色背景

9. Xcode中常用的快捷键

新建

  • shift + cmd + n 新建项目
  • cmd + n 新建文件

视图

  • option + cmd + 回车 打开助理编辑器
  • cmd + 回车 显示主窗口
  • cmd + 0 导航窗口
  • option + cmd + 0 工具窗口
  • 在.m & .h之间切换 control + cmd + 上/下
  • 按照浏览文件的前后顺序切换 control + cmd + 左右
  • 查看头文件 control + cmd + j
  • 切换到对应的函数 control + 6 支持智能输入,注意输入法

运行

  • cmd + r 运行
  • cmd + . 停止
  • cmd + b 编译
  • cmd + shift + b 静态内存分析编译,可以检查程序结构上是否存在内存泄露

排版

  • control + i 将选中按钮重新缩进
  • cmd + ] 向右增加缩进
  • cmd + [ 向左减少缩进
  • cmd + / 注释/取消注释,提示:取消注释时,注释双斜线必须在行首
  • cmd + 向上 到文件开始位置
  • cmd + 向下 到文件末尾位置

10. 使用KVC(key-value coding)加载数据

  • 第一种方式
[self setValue:dict[@"answer"] forKeyPath:@"answer"];[self setValue:dict[@"title"]  forKeyPath:@"title"];[self setValue:dict[@"icon"]  forKeyPath:@"icon"];[self setValue:dict[@"options"]  forKeyPath:@"options"];

要注意模型对象类中的属性与plist文件中字段要相同,否则会报错
- 第二种方式

[self setValuesForKeysWithDictionary:dict];

11. NSLog时数组中数据现实中文而不是Unicode

在Xcode6.3中选择新建Objective-c File, 在File Type中选择Category,在Class中选择NSArray,File随便写
然后在m文件中重写(NSString *) descriptionWithLocale:(id)locale方法

-(NSString *) descriptionWithLocale:(id)locale{    NSMutableString *strM = [NSMutableString string];    [strM appendString:@"{\n"];    for (id obj in self) {        [strM appendFormat:@"\t%@,\n", obj];    }    [strM appendString:@"}"];    return strM;}
0 0
原创粉丝点击