[自定义组件之二]两种方式改造UIView
2016-02-27 10:51
459 查看
#import <UIKit/UIKit.h> @class MyUIViewWithDelegate; @class MYUIViewWithEvent; @protocol UIViewDrawRectDelegate <NSObject> @optional -(void)beforeDrawRect:(CGRect)rect; -(void)AfterDrawRect:(CGRect)rect; @end @interface ViewController : UIViewController<UIViewDrawRectDelegate> @property (strong, nonatomic) IBOutlet MyUIViewWithDelegate *ViewWithDelegate; @property (strong, nonatomic) IBOutlet MYUIViewWithEvent *ViewWithEvent; @end #pragma mark - MyUIViewWithDelegate 使用代理+继承方式 @interface MyUIViewWithDelegate :UIView @property (nonatomic,assign) id <UIViewDrawRectDelegate> delegate; -(void) drawRect:(CGRect)rect; @end #pragma mark - MYUIViewWithEvent 使用block+继承的方式(delphi写法) typedef void (^UIViewDrawRectEvent)(CGRect, id); @interface MYUIViewWithEvent:UIView @property (nonatomic,copy) UIViewDrawRectEvent BeforeDrawEVent; @property (nonatomic,copy) UIViewDrawRectEvent AfterDrawEVent; -(void)drawRect:(CGRect)rect; @end
#import "ViewController.h" @interface ViewController () @end @implementation ViewController { float _curX; float _curY; } -(void)beforeDrawRect:(CGRect)rect { CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(ctx, [[UIColor redColor] CGColor]); CGContextFillEllipseInRect(ctx, CGRectMake(_curX-15, _curY - 15, 30, 30)); } - (void)viewDidLoad { [super viewDidLoad]; self.ViewWithDelegate.delegate = self; self.ViewWithEvent.BeforeDrawEVent = ^(CGRect rect, id sender) { CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(ctx, [[UIColor greenColor] CGColor]); CGContextFillEllipseInRect(ctx, CGRectMake(_curX-15, _curY - 15, 30, 30)); }; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch* touch = [touches anyObject]; CGPoint point = [touch locationInView:self.view]; if ([self.ViewWithDelegate pointInside:point withEvent:event]) { _curX = point.x; _curY = point.y; [self.ViewWithDelegate setNeedsDisplay]; } else{ _curX = point.x; _curY = point.y - self.ViewWithDelegate.frame.size.height; [self.ViewWithEvent setNeedsDisplay]; } } @end #pragma mark - MyUIViewWithDelegate 使用代理+继承方式 @implementation MyUIViewWithDelegate -(void)drawRect:(CGRect)rect { if (self.delegate && [self.delegate respondsToSelector:@selector(beforeDrawRect:)]) { [self.delegate beforeDrawRect:rect]; } [super drawRect:rect]; if (self.delegate && [self.delegate respondsToSelector:@selector(AfterDrawRect:)]) { [self.delegate AfterDrawRect:rect]; } } @end #pragma mark - MYUIViewWithEvent 使用block+继承的方式(delphi写法) @implementation MYUIViewWithEvent -(void)drawRect:(CGRect)rect { if (self.BeforeDrawEVent) { self.BeforeDrawEVent(rect,self); } [super drawRect:rect]; if (self.AfterDrawEVent) { self.AfterDrawEVent(rect,self); } } @end
总结下吧:
1,利用了代理模式和block方式覆盖了uiview的drawRect,对其进行了扩展。
2,因类别不能有自己变量(runtime的没试验),都采取了继承方式。
3,delphi的后期版本能直接写成uiview:path.uiview的方式,比类别功能强大,能声明变量。
相关文章推荐
- 169. Majority Element My Submissions Question
- 修改UILable的字体位置
- UIKit继承图
- 去除UIDatePicker和UIPickerView中间Row上面的分割线
- javaWeb学习—getRequestURI,getRequestURL等的学习
- iOS9-by-Tutorials-学习笔记六:UIStackView-Auto-Layout-Changes
- 【Web API系列教程】3.7 — 实战:处理数据(创建UI视图)
- android.os.Build
- DE26 Continuation: Repeated Real Eigenvalues
- GlobalValue
- Increasing Triplet Subsequence
- 备注 break continue return
- 解决“Dynamic Web Module 3.0 requires Java 1.6
- ios 代码创建UIToolBar
- 2016/2/26 <marquee></marquee>实现多种滚动效果
- APUE之对于系统中缺失的error.c文件的阶段性总结
- [1005]: Number Sequence
- The 6th Zhejiang Provincial Collegiate Programming Contest->Problem I:A Stack or A Queue?
- 线上使用阿里Druid连接池首次连接MySQL异常问题
- UIdynamic