ios学习笔记2

延后执行: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 URLWithString]

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();

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

其他函数:

dispatch_after

performSelector

dispatch_once实现单例(一次性代码,整个程序运行过程中只会运行一次)

单例类需要重写alloc方法和init方法

最好重写:allocWithZone方法

@synchronized(self){

}

static dispatch_once_t onceToken;

dispatch_once(&onceToken,^{

_instance = [super allocWithZone:zone];

})

dispatch_once专为单例设计

单例命名规则:share或default开头

提供类方法

+(instancetype)shareTool{

return [[self alloc]init];

}

重写copyWithZone方法

-(id)copyWithZone:(NSZone*) zone{

return _instance;

}

-(id)mutableCopyWithZone:(NSZone*) zone{

return _instance;

}

MRC下的单例

需要重写release、retain和retainCount方法

其中,retainCount方法为:

-(NSUInteger)retainCount{

return MAXFLOAT;

}

栅栏函数

dispatch_barrier_async(queue,^{

});

只有栅栏函数执行完成后才会执行后面的队列

快速迭代(迭代10次)

dispatch_apply(10,dispatch_get_global_queue(0,0),^(size_t index){

});

队列组

dispatch_queue_t queue = dispatch_get_global_queue();

dispatch_group_t group = dispatch_group_create();

dispatch_group_async(group,queue,^{

});

dispatch_group_notify(group,queue,^{

});//当队列组中所有线程执行完成后会调用此代码块

开始结束添加队列组函数

dispatch_group_enter(group);

dispatch_async();

dispatch_group_leave(group);

函数方式调用:

dispatch_async_f();

判断arc,mrc的宏

#if __has_feature(obj_arc)

NSInvocationOperation alloc initWithTarget

NSBlockOperation *oper = [NSBlockOperation blockeOperationWithBlock:^{

}];

[oper start]

两种方式都需要调用start方法

NSBlockOperation如果block数量大于1,系统会开多个线程,不一定为子线程,可能为主线程

[NSOperationQueue mainQueue]主队列和GCD中的主队列一样,串行队列

[[NSOperationQueue alloc]init]非主队列默认情况下是并发队列

[queue addOperation:oper];

[queue addOperationWithBlock:^{

}];

自定义NSOperation需要实现main方法

queue不需要start

[op1 addDependency:op4];//操作依赖,不能循环依赖

op3.completionBlock=^{

};//操作监听

画图:

UIGraphicsBeginImageContext(CGSizeMake(200,200));

[image1 drawInRect:CGRectMake(0,0,100,200)];

[image2 drawInRect:CGRectMake(100,0,100,200)];

UIImage *image=UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

到主线程更新UI

[[NSOperationQueue mainQueue] addOperationWithBlock:^{

}];

NSLog(@"%@",[NSSearchPathForDirectoriesInDomains(NSCacheDirectory,NSUserDomainMask,YES) lastObject]

);打印应用运行目录

猜你喜欢

转载自blog.csdn.net/ranjio_z/article/details/88819614