-
Notifications
You must be signed in to change notification settings - Fork 765
UIView
戴铭 edited this page Mar 17, 2015
·
1 revision
UIView有三个基类:UIResponder,UIView和UIControl。
- UIResponder:是UIView的父类。responder处理触摸,手势,远程控制事件。UIApplication和UIViewController也是UIResponder的子类。
- UIView:处理绘制和触摸时间。
- UIControl:有target/action模式。button,Date pickers,文本框都是。
避免使用drawRect:在CPU上做渲染,如果希望使用GPU渲染就使用现有的视图构建自定义视图。如果要实现一个带边框的圆形头像
- (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或通知
最方便的做法,也是最学院派的。
[self sendActionsForControlEvents:UIControlEventValueChanged];
//如果一个view controller管理这个视图
- (void)setupPieChart
{
[self.pieChart addTarget:self
action:@selector(updateSelection:)
forControlEvents:UIControlEventValueChanged];
}
- (void)updateSelection:(id)sender
{
NSLog(@"%@", self.pieChart.selectedSector);
}
[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
{
// 处理区块
}
@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];
}
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中移除观察者。
//头文件中
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
{
}
//注意在合适的地方退订通知