target/action设计模式

耦合是衡量⼀个程序写的好坏的标准之⼀,耦合是衡量模块与模块之间关联程度的指标“⾼内聚,低耦合”是⾯向对象编程的核⼼思想。

这里, 我们使用target/action设计模式对视图进行控制.

先定义一个继承于UIView的自定义视图, 编写3个函数

//随机改变位置
- (void) randFrame {
    self.center = CGPointMake(arc4random() % 200, arc4random() % 200);
}
//随机改变形状
- (void)randomSize {
    self.bounds = CGRectMake(0, 0, arc4random() % 200 + 10, arc4random() % 200 + 50);
}
- (void)randromColor {
    //范围是0 - 1
    CGFloat red = arc4random() % 256 / 255.0;
    CGFloat green = arc4random() % 256 / 255.0;
    CGFloat blue = arc4random() % 256 / 255.0;
    self.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1];
}

定义属性:

@property (nonatomic, retain) id target;
@property (nonatomic, assign) SEL action;

建立根视图控制器, 定义三个自定义的View

- (void)viewDidLoad {
    [super viewDidLoad];
    WJQView *view1 = [[WJQView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    view1.backgroundColor = [UIColor orangeColor];
    view1.tag = 1;
    view1.target = self;
    view1.action = @selector(newView:);
    [self.view addSubview:view1];
    [view1 release];
    
    WJQView *view2 = [[WJQView alloc] initWithFrame:CGRectMake(100, 250, 100, 100)];
    view2.backgroundColor = [UIColor purpleColor];
    view2.tag = 2;
    //谁服从的协议, 这个self就是谁
    view2.delegate = self;
    [self.view addSubview:view2];
    [view2 release];
    
    WJQView *view3 = [[WJQView alloc] initWithFrame:CGRectMake(100, 400, 100, 100)];
    view3.backgroundColor = [UIColor yellowColor];
    view3.tag = 3;
    [self.view addSubview:view3];
    [view3 release];
}

编写newView函数, 里面实现里3中不同的改变视图的形式, 分别是改变颜色, 改变位置以及改变形状

- (void)newView:(WJQView *)view {
    //范围是0 - 1
    CGFloat red = arc4random() % 256 / 255.0;
    CGFloat green = arc4random() % 256 / 255.0;
    CGFloat blue = arc4random() % 256 / 255.0;
    view.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1];
    view.center = CGPointMake(arc4random() % 200, arc4random() % 200);
}

我们还可以通过代理实现上述功能

在自定义视图的.h文件中进行设置

<span style="font-size:12px;">@class WJQView;
@protocol WJQViewDelegate <NSObject>
@optional
- (void)touchEndWJQView:(WJQView *)view;
- (void)touchBeginWJQView:(WJQView *)view;
- (void)touchMovedWJQView:(WJQView *)view;

@end

@interface WJQView : UIView
//声明一个delegate属性
@property (nonatomic, assign) id <WJQViewDelegate>delegate;
@property (nonatomic, retain) id target;
@property (nonatomic, assign) SEL action;
@end</span>
.m文件中实现:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    //判断:
    //1. 是否设置代理
    //2. 是否实现了协议方法
    //设置成为代理的这个类, 有没有实现touchBeganWJQView:这个方法
    if (_delegate != nil && [_delegate respondsToSelector:@selector(touchBeginWJQView:)]) {
         [_delegate touchBeginWJQView:self];
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [_delegate touchMovedWJQView:self];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    
    // 给传进来的对象发送一个什么样的消息
    //
    [self.target performSelector:_action withObject:self];
    
    //让代理去执行协议中的方法, 把参数传过去
    [_delegate touchEndWJQView:self]; 
}

//  实现协议中的方法
- (void)touchEndWJQView:(WJQView *)view {
    if (view.tag == 1) {
        [self newView:view];
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值