IB
1> IBAction : 如果需要监听按钮的点击方法,使用IBAction,就可以与IB进行连线,连线之后,在点击按钮时,就会执行相应的方法
2> IBOutlet : 如果需要获得/修改界面上控件的属性,使用IBOutlet,就可以与IB进行连线
关闭键盘
在所有的面相对象语言中,有一个原则:谁申请,谁释放!
对应键盘就应该,谁打开,谁关闭!
FirstResponder:光标所在的文本框
resign: 辞去
[self.num1 resignFirstResponder];
[self.num2 resignFirstResponder];
让视图关闭键盘,无论现在谁是第一响应者
参数表示是否强行关闭键盘,始终使用YES作为参数即可。
[self.view endEditing:YES];
关于frame
// 修改位置可以通过frame
// 在OC中,不允许直接修改"对象"的"结构体属性"的"成员"
// 但是允许修改"结构体属性"的"成员"
// self.iconButton.frame.origin.y -= 20;
// 如果要修改对象的结构体属性,需要三个步骤
// 1. 取出frame
CGRect frame = self.iconButton.frame;
// 2. 修改结构体的成员
frame.origin.y -= 20;
// 3. 重新为对象的结构体属性赋值
self.iconButton.frame = frame;
关于枚举
// 枚举类型本质上就是整数,定义的时候,如果只指定了第一个数值,后续的数值会依次递增
// 枚举类型是解决魔法数字比较常用的手段
typedef enum {
kMovingDirTop = 10,
kMovingDirBottom,
kMovingDirLeft,
kMovingDirRight,
} kMovingDir;
@proerty
1. 创建了getter & setter方法
2. 生成一个带_的成员变量,直接读取成员变量不会经过getter方法&setter方法
// 在getter方法中,不要再使用self. 否则会重复调用getter方法,造成死循环
strong & weak
* 控件
如果是用Stroyboard拖线,控件用weak
如果用代码创建界面,控件可以用strong
* 自定对象,需要使用strong
* NSString,使用copy
* 数字型的int,使用assign
// "包" Bundle [NSBundle mainBundle]编译安装之后对应的程序包
NSString *path = [[NSBundle mainBundle] pathForResource:@"ImageList" ofType:@"plist"];
NSLog(@"%@", path);
// 在OC中ContentsOfFile,通常需要完整的路径
_imageList = [NSArray arrayWithContentsOfFile:path];
imageNamed:系统推荐使用的,但是图像实例化之后的释放由系统负责
如果要自己释放图片,不能使用imageNamed方法!
而需要使用imageWithContentsOfFile
提示:如果放在Images.xcassets中的图片,不能使用imageWithContentsOfFile
Images.xcassets中不要 存放大的,不常用的图片
// 设置动画数组
self.tom.animationImages = arrayM;
// 重复1次
self.tom.animationRepeatCount = 1;
// 动画时长
self.tom.animationDuration = self.tom.animationImages.count * 0.075;
// 开始动画
[self.tom startAnimating];
// 动画"结束"之后,清理动画数组
// self.tom.animationImages = nil;
// performSelector定义在NSObject分类中
// [self performSelector:@selector(cleanup) withObject:nil afterDelay:self.tom.animationDuration];
[self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration];
}
JPG: 压缩比比较高,通常用于照片、网页
属于有损压缩(噪点)
解压缩时,对CPU消耗大,意味慢,费电
PNG:压缩比较高
无损压缩
解压缩效率高,对CPU消耗小,苹果推荐使用的
/ *** 一定不要使用以下方法,修改按钮标题
// button.titleLabel.text = @"aaa";
// 修改字体(titleLabel是只读的)
// readonly表示不允许修改titleLabel的指针,但是可以修改label的字体
// 提示:按钮的字体是不区分状态的!
button.titleLabel.font = [UIFont systemFontOfSize:12.0];
/**
instancetype 主要用于在类方法实例化对象时,让编译器主动推断对象的实际类型
以避免使用id,会造成开发中不必要的麻烦,减少出错几率!
instancetype是苹果在iOS7才开始主推的
C++11 auto
在swift语言中,绝大多数类的实例化,都不需要再指定类型
instancetype只能用于返回值使用!!!不能当做参数使用
*/
/** 通常在写模型的实例化方法时,以下两个方法,都需要实现 */
/** 使用字典实例化模型 */
- (instancetype)initWithDict:(NSDictionary *)dict;
/** 类方法可以快速实例化一个对象 */
+ (instancetype)appInfoWithDict:(NSDictionary *)dict;
// 用字典给属性赋值,所有与plist键值有关的方法,均在此处!
// self.name = dict[@"name"];
// self.icon = dict[@"icon"];
// KVC - key value coding键值编码
// 是一种间接修改/读取对象属性的一种方法
// KVC 被称为 cocoa 的大招!
// 参数:
// 1. 数值
// 2. 属性名称
// [self setValue:dict[@"name"] forKeyPath:@"name"];
// [self setValue:dict[@"icon"] forKeyPath:@"icon"];
// setValuesForKeysWithDictionary本质上就是调用以上两句代码
[self setValuesForKeysWithDictionary:dict];
// ^ 表示是block,块代码,是一个预先准备好的代码块,可以当做参数传递,在需要的时候执行!
// 块代码在OC中,使用的非常普遍!
[UIView animateWithDuration:1.0f animations:^{
NSLog(@"动画开始");
// 要修改的动画属性
label.alpha = 1.0;
} completion:^(BOOL finished) {
[UIView animateWithDuration:1.0 animations:^{
label.alpha = 0.0;
} completion:^(BOOL finished) {
// 动画完成后,所做的操作
NSLog(@"动画完成");
// button.enabled = NO;
[label removeFromSuperview];
}];
}];
NSLog(@"-------");
// 收尾式动画,不容易监听动画完成时间,而且不容易实现动画嵌套
// [UIView beginAnimations:nil context:nil];
// [UIView setAnimationDuration:1.0f];
// label.alpha = 1.0;
// [UIView commitAnimations];
不能够交互的
alpha <= 0.01
hidden = YES
userInteraction = NO
父视图不允许交互,也不能交互
在父视图可见范围内,可以交互,范围之外不能交互
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
/**
1> UIButton -> UIControl -> UIView
1.1 设置控件的状态
启用、禁用
@property(nonatomic,getter=isEnabled) BOOL enabled;
选中、不选中
@property(nonatomic,getter=isSelected) BOOL selected;
高亮或者不高亮
@property(nonatomic,getter=isHighlighted) BOOL highlighted;
1.2 设置控件内容的布局
垂直居中方向
@property(nonatomic) UIControlContentVerticalAlignment contentVerticalAlignment;
水平居中方向
@property(nonatomic) UIControlContentHorizontalAlignment contentHorizontalAlignment;
1.3 添加/删除监听方法
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
- (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
2> UILabel -> UIView
3> UIImageView -> UIView
4> UITextField -> UIControl
*** 代理设计模式,在OC中,使用最为广泛的一种设计模式
1> 代理的用处是什么?
* 监听那些不能通过addTarget监听的事件!
* 主要用来负责在两个对象之间,发生某些事件时,来传递消息或者数据
2> 代理的实现步骤
(1) 成为(子)控件的代理,父亲(控制器)成为儿子(文本框)的代理
(2) 遵守协议->利用智能提示,快速编写代码
(3) 实现协议方法
#pragma mark 文本框代理方法
// 在文本框中按回车的处理
- (BOOL)textFieldShouldReturn:(UITextField *)textField
contentSize 会根据边距调整offset
contentInset 不会调整offset
CGRectGetMaxY