1.关于代码规范
若想使用懒加载,
//懒加载
-(CADisplayLink *)displayLink{
if (!_displayLink) {
_displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(toDoDrawLine)];
_displayLink.frameInterval = 3;
}
return _displayLink;
}
在声明属性时,就需要使用类似下列形式
@property(nonatomic,strong) CADisplayLink *displayLink;
而不能写成
@interface BarView(){
CADisplayLink *_displayLink;
}
若写成带下划线形式的,那么懒加载方法是不会执行的。必须写成self形式
只有在不需要懒加载时,才可以定义为下划线形式。
2.float或double类型的四舍五入,无需其它方法,用字符串的stringWithFormat即可处理。如:
NSLog(@"%@", [NSString stringWithFormat:@"%.0f", 1.0003]);
NSLog(@"%@", [NSString stringWithFormat:@"%.0f", 1.9003]);
NSLog(@"%@", [NSString stringWithFormat:@"%.0f", 1.5003]);
NSLog(@"%@", [NSString stringWithFormat:@"%.0f", 1.4003]);
打印结果:

3.为了防止按钮重复点击,可以把它设置为几秒内不能点击即可。用
performSelector:@selector() withObject: afterDelay:
就行。
4.关于IOS的像素和点得概念
IOS中尺寸的单位是点而不是像素,在Retina屏中一个点等于两个像素,普通屏下一个点等于1个像素。
在Retina屏中图片一般采用“xxx@2x.png”命名,在代码中只需写成“xxx.png”,程序会自动根据屏幕类型在Retina屏下使用“xxx@2x.png”,在非Retina屏下使用“xxx.png”。
iPhone4之后(x,y,width,height)=(20,30,40,50),表示宽度为40个点,高度为50个点,放在(20,20)个点的位置上。这样就屏蔽了屏幕的差别,专注搞开发。
对于图片,单位一般是像素,如a.png,大小为30x40像素,在iPhone3GS和iPhone4中使用的时候,都是占据30X40个点,而在iPhone4上,因为一个点是2个像素,所以是30X40像素的图片,显示在60X80像素的屏幕上,所以有些模糊。
这时就需要有a@2x.png的图片。在iPhone6 plus上,还需要a@3x.png图片,道理一样。
5.关于手势的操作
比如一个文本框textField,点击时键盘弹起,点其它地方时键盘消失。那么可以用
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissTheKeyboard)];
[self.view addGestureRecognizer:tap];
-(void)dismissTheKeyboard{
[self.textField resignFirstResponder];
}
6.获取控件的各种位置,可以用CGRect。
其中,CGRect定义的是矩形。在计算多组件的位置时,就可以使用这种方法。
CGRectGetMaxY 获取下边界位置
CGRectGetMinY 获取上边界位置
CGRectGetMaxX 获取右边界位置
CGRectGetMinX 获取左边界位置
如:
- (void)viewDidLoad {
[super viewDidLoad];
UIView *view = [[UIView alloc]init];
view.frame = CGRectMake(80, 150, 100, 100);
view.backgroundColor = [UIColor blueColor];
[self.view addSubview:view];
CGRect labelRect = view.frame;
NSLog(@"----%@",NSStringFromCGRect(labelRect));
CGFloat maxY = CGRectGetMaxY(labelRect);
CGFloat minY = CGRectGetMinY(labelRect);
CGFloat maxX = CGRectGetMaxX(labelRect);
CGFloat minX = CGRectGetMinX(labelRect);
NSLog(@"maxY=%f,minY=%f,maxX=%f,minX=%f",maxY,minY,maxX,minX);
}
打印结果为:

分析为:

7.IOS获取键盘高度,处理view的位置,需要用到通知
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self registerForKeyboardNotifications];
}
- (void)registerForKeyboardNotifications
{
//键盘弹出
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//键盘隐藏
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}
-(void)keyboardWillShow:(NSNotification*)notification
{
//获取键盘高度
NSDictionary* info = [notification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
CGFloat kHeight = keyboardRect.size.height;
[UIView animateWithDuration:duration animations:^{
self.bottomView.top = self.view.frame.size.height-49-kHeight;
}];
}
- (void)keyboardWillBeHidden:(NSNotification*)notification
{
//获取键盘高度
NSDictionary* info = [notification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
CGFloat height = keyboardRect.size.height;
CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
[UIView animateWithDuration:duration animations:^{
self.bottomView.top = self.view.frame.size.height-49;
}];
}
















