您的位置:首页 > 产品设计 > UI/UE

[自定义组件之二]两种方式改造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的方式,比类别功能强大,能声明变量。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: