[自定义组件之二]两种方式改造UIView

#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的方式,比类别功能强大,能声明变量。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值