UIView自定义交互

视图层级

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或通知

Target action:

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

[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
{
     // 处理区块
}

使用Block

@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

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
{
}
//注意在合适的地方退订通知
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本文将讨论一些自定义视图、控件的诀窍和技巧。我们先概述一下 UIKit 向我们提供的控件,并介绍一些渲染技巧。随后...
    评评分分阅读 1,471评论 1 32
  • 本文将讨论一些自定义视图、控件的诀窍和技巧。我们先概述一下 UIKit 向我们提供的控件,并介绍一些渲染技巧。随后...
    爱敲代码的果果阅读 538评论 0 3
  • 在开发过程中,大家或多或少的都会碰到令人头疼的手势冲突问题,正好前两天碰到一个类似的bug,于是借着这个机会了解了...
    闫仕伟阅读 5,574评论 2 23
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,285评论 30 472
  • 好奇触摸事件是如何从屏幕转移到APP内的?困惑于Cell怎么突然不能点击了?纠结于如何实现这个奇葩响应需求?亦或是...
    Lotheve阅读 58,679评论 51 604