0%

UIView

视图层级

UIView有三个基类:UIResponder,UIView和UIControl。

  • UIResponder:是UIView的父类。responder处理触摸,手势,远程控制事件。UIApplication和UIViewController也是UIResponder的子类。
  • UIView:处理绘制和触摸时间。
  • UIControl:有target/action模式。button,Date pickers,文本框都是。

渲染

避免使用drawRect:在CPU上做渲染,如果希望使用GPU渲染就使用现有的视图构建自定义视图。如果要实现一个带边框的圆形头像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

- (void)setupView

{

self.clipsToBounds = YES;

self.layer.cornerRadius = self.bounds.size.width / 2;

self.layer.borderWidth = 3;

self.layer.borderColor = [UIColor darkGrayColor].CGColor;

}

渲染速度比较 OpenGL -> Core Image -> Core Graphics

自定义交互

响应触摸,可以使用重写更加底层的touchesBegan,touchesMoved和touchesEnded方法。还可以使用其它不同的方法来解决比如target action,代理,block,KVO或通知

Target action:

最方便的做法,也是最学院派的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[self sendActionsForControlEvents:UIControlEventValueChanged];

//如果一个view controller管理这个视图

- (void)setupPieChart

{

[self.pieChart addTarget:self

action:@selector(updateSelection:)

forControlEvents:UIControlEventValueChanged];

}

- (void)updateSelection:(id)sender

{

NSLog(@"%@", self.pieChart.selectedSector);

}

使用代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[self.delegate pieChart:self didSelectSector:self.selectedSector];

//在view controller中代码

@interface MyViewController <PieChartDelegate>

...

- (void)setupPieChart

{

self.pieChart.delegate = self;

}

- (void)pieChart:(PieChart*)pieChart didSelectSector:(PieChartSector*)sector

{

// 处理区块

}

使用Block

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
@interface PieChart : UIControl

@property (nonatomic,copy) void(^selectionHandler)(PieChartSection* selectedSection);

@end

//在执行之前检查下block是否被赋值,未被赋值的block会崩

if (self.selectionHandler != NULL) {

self.selectionHandler(self.selectedSection);

}

//可以把相关的代码整合在view controller中

- (void)setupPieChart

{

self.pieChart.selectionHandler = ^(PieChartSection* section) {

// 处理区块

}

}

//避免引用循环

__weak id weakSelf = self;

self.pieChart.selectionHandler = ^(PieChartSection* section) {

MyViewController* strongSelf = weakSelf;

[strongSelf handleSectionChange:section];

}

使用KVO

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

self.selectedSegment = theNewSelectedSegment;

- (void)setupPieChart

{

[self.pieChart addObserver:self forKeyPath:@"selectedSegment" options:0 context:NULL];

}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

{

if(object == self.pieChart && [keyPath isEqualToString:@"selectedSegment"]) {

// 处理改变

}

}

//在viewWillDisappear:或dealloc中移除观察者。

使用通知

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

//头文件中

extern NSString* const SelectedSegmentChangedNotification;

//实现的文件中

NSString* const SelectedSegmentChangedNotification = @"selectedSegmentChangedNotification";

...

- (void)notifyAboutChanges

{

[[NSNotificationCenter defaultCenter] postNotificationName:SelectedSegmentChangedNotification object:self];

}

//现在订阅通知,在view controller中实现

- (void)setupPieChart

{

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(segmentChanged:)

name:SelectedSegmentChangedNotification

object:self.pieChart];

}

...

- (void)segmentChanged:(NSNotification*)note

{

}

//注意在合适的地方退订通知