ios笔记

延后执行:performSelector withObject afterDelay

imageNamed内存不释放(赋值时自动retain)

imageWithContentOfFile置为nil时释放内存(赋值时不retain)

self.imageView.animationImages=nil引用数清零后也能清理内存

毛玻璃效果:imageView加载图片上addSubview,subview为toolbar,toolbar设置透明度

颜色处理(生成UIColor对象):[UIColor colorWithRed:(243/255.0) Green:(243/255.0) Blue:(243/255.0)]

NSString *path=【NSBundle mainBundle】pathForResource:@“mysong1.mp3” ofType: nil]

NSUrl *url=[NSUrl fileWithPath:path] weak引用计数不加一(快捷方式)

strong引用计数加一(硬链接)

ios为ARC架构

button的文字设置需要用setTitle forState,不能直接修改titleLabel属性

取最后一个子组件[.subViews lastObject]

初始化数组使用@:

NSArray<NSDictionay *> *dataArr =@[

@{@"name":@"aaaa1",@"icon":@"aaaaa1.png"},

@{@"name":@"aaaa2",@"icon":@"aaaaa2.png"},

@{@"name":@"aaaa3",@"height":@3.0},

@{@"name":@"aaaa4",@"icon":@"aaaaa4.png"},

@{@"name":@"aaaa5",@"icon":@"aaaaa5.png"}

];

 

调试:

po NSHomeDirectory()

自定义控件:

1、继承UIView

2、实现init方法(不要传宽高)

3、实现layoutSubviews方法

在layoutSubviews方法里一定要调用一次[super layoutSubviews]

在layoutSubviews方法里通过

self.frame.size.with,self.frame.size.height获得整体宽度和高度

在layoutSubviews方法中设置子组件的宽度和高度

 

新建button 必须使用[UIButton buttonWithType:]方法新建

 

1、在initWithFrame方法中添加子控件,提供便利构造方法

2、在layoutSubviews方法中设置子控件的frame(一定要调用[super layoutSubviews])

3、增加模型属性

 

Xib的加载:

UIView *carView =【[[NSBundle mainBundle] loadNibNamed:@"xib文件名" owner:nil options:nil】firstObject

 

方式二

UINib *nib =[UINib nibWithNibName:@"" bundle:nil];

UIView *carview =[[nib instantiateWithOwner:nil options:nil]firstObject]

xib可以指定对应的View类

xib不可以通过alloc init方式创建对象,只能通过以上两种方式创建

重写initWithCoder方法实现初始化

 

受保护拉伸图片:

image stretchableImageWithLeftCapWidth:imageWidth*0.5 topCapHeight: imageHeight*0.5

右侧自动计算:width - leftCapWidth - 1

底部自动计算:height - topCapHeight - 1

scrollView.pagingEnable=YES开启自动分页功能

 

[pageControl setValue:[UIImage imageNamed:@"current"] forKeyPath:@"_currentPageImage"]

[pageControl setValue:[UIImage imageNamed:@"other"] forKeyPath:@"_pageImage"]

 

KVO:KV Observer 关键addObserver

KVC:KV Coding

 

作用:修改timer在runloop中的模式为NSRunLoopCommonModes

目的:不管主线程在做什么操作,都会分配一定的时间处理定时器

[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

 

Xib启动方法:awakeFromNib

 

[self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]

删除scrollView中的所有子组件

 

autoresizing:

view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoResizingFlexibleTopMargin;

 

awakeFromNib中设置自动伸缩

 

代码的方式滚动,可以滚动到一个不合理的位置

autolayout功能更强

使用autolayout需要禁止autoresizing

view.translateAutoresizingMaskIntoConstraints = NO;

 

VFL:Visual Format Language

NSString * hvfl =@"H:|-20-[redView]-20-|";

NSArray *hlcs = [NSLayoutConstraint constraitsWithVisualFormat:hlcs options:kNilOptions metrics:nil views:nil];

[self.view addConstraints:hlcs]

自动布局的核心计算公式:obj1.property1 =(obj2.property2 * multiplier)*constant value

 

需要通过修改约束的方式修改位置和尺寸

约束的本质:自动转化为frame,当做动画时,需要先修改约束,然后在动画代码中强制刷新:

self.redViewW.constraint = 50;

[UIView animateWithDuration:2.0 animations:^{

[self.view layoutIfNeeded];//强制刷新

}];

 

ios的websocket:facebook开源的 SocketRocket 框架

 

 

masonry框架用法

#import "Masonry.h"

 

[redView mas_makeConstraints:^(MASConstraintMaker *make){

make.top.equalTo(self.view.mas_top).offset(20);

make.left.equalTo(self.view.mas_left).multipliedBy(1.0).offset(20);

}];

 

make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(20,20,20,20));

 

make.width.mas_equalTo(100);

make.height.mas_equalTo(100);

make.centerX.mas_equalTo(self.view);

make.centerY.mas_equalTo(self.view);

 

 

省略mas_前缀

#define MAS_SHORTHAND

#define MAS_SHORTHAND_GLOBALS

 

更新约束:

updateConstraints:

 

tableCell自动行高(ios8以后有效):

viewDidLoad{

【super viewDidLoad】;

self.tableView.rowHeight = UITableViewAutomaticDimension;

self.tableView.estimateRowHeight =44;//估算高度

}

Label:lines设置为0自动换行

 

拉约束进入代码变量

设置constraint=100或者constrait=0

 

注意Xcode中很多地方用到按住Control拖拉

 

GCD用来替代NSThread,充分使用多核,C语言写的(经常使用)

 

NSOperation是oc对GCD的包装(经常使用)

 

pthread版:pthread_create启动线程

NSThread start启动线程

detachNewThreadSelector

self performSelectorInBackground 启动后台线程

@synchronized加锁同步操作

atomic会对set方法加锁

nonatomic不会对set方法加锁

线程间通信常用方法:

1、performSelectorOnMainThread

2、performSelector onThread

 

下载图片操作:

NSURL

NSData *imageData=[NSData dataWithContentsOfURL: url];

[UIImage imageWithData:imageData]

 

时间操作

NSDate

CFTimeInterval start=CFAbsoluteTimeGetCurrent();//函数调用

 

[self.imageView performSelectorOnMainThread:@selector(setImage:) withObject: image waitUntilDone:YES];

 

GDC异步函数并发队列

dispath_queue_t queue = dispath_queue_create("com.520it.download",DISPATCH_QUEUE_CONCURRENT);

dispatch_async(queue,^{

NSLog(@"download1----%@",[NSThread currentThread]);

});

dispatch_async(queue,^{

NSLog(@"download2----%@",[NSThread currentThread]);

});

GDC异步函数串行队列

dispath_queue_t queue = dispath_queue_create("com.520it.download",DISPATCH_QUEUE_SERIAL);

dispatch_async(queue,^{

NSLog(@"download1----%@",[NSThread currentThread]);

});

dispatch_async(queue,^{

NSLog(@"download2----%@",[NSThread currentThread]);

});

 

同步函数:

dispatch_sync,不会开线程

异步函数

dispatch_async,会开线程:并发队列,开多个线程;串行队列开一个线程

dispatch_async 函数会将传入的block块放入指定的queue里运行。这个函数是异步的,这就意味着它会立即返回而不管block是否运行结束。因此,我们可以在block里运行各种耗时的操作(如网络请求) 而同时不会阻塞UI线程。 

dispatch_get_global_queue 会获取一个全局队列,我们姑且理解为系统为我们开启的一些全局线程。我们用priority指定队列的优先级,而flag作为保留字段备用(一般为0)。 

dispatch_get_main_queue 会返回主队列,也就是UI队列。它一般用于在其它队列中异步完成了一些工作后,需要在UI队列中更新界面(比如上面代码中的[self updateUIWithResult:result])的情况。

dispath_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

获得主队列(UI队列)

dispath_queue_t queue =dispatch_get_main_queue();

注意:主队列不能使用同步函数

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值