UI - Target-Action设计模式

target -action设计模式分5个步骤来完成
warning部分即为步骤顺序

#TouchView.h

#import <UIKit/UIKit.h>
@interface TouchView : UIView
#warning 1.给外界提供目标和动作属性,让外界可以设置
@property(nonatomic,retain)id target; // 目标
@property(nonatomic,assign)SEL action; // 动作
@end

#TouchView.m

#import "TouchView.h"
@implementation TouchView
#warning 2.重写dealloc方法释放对应的实例变量
- (void)dealloc
{
    [_target release];
    [super dealloc];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 1.我们现在面临了什么问题
    // 2.通过到目前为止学过的知识点 怎样可以实现题目的需求
//    [self ChangeColor];
    // 更改为目标去执行[目标 方法];
#warning 4.在某个时间点让目标取执行对应的方法
    [_target performSelector:_action withObject:self];
    // 让target去执行action这个方法并且把self参数传出去 (self:当前点击的touchView)
}

#RootViewController.m

#import "RootViewController.h"
#import "TouchView.h"
@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // target - action设计模式
    /**
     target - action 是模仿系统的Button 即将事件的处理交给外界 不在自己的内部写死
     target-action设计模式主要涉及到两方面的内容
     target:目标
     action:动作
     target-action可以让不同的实例对象在相同的时间点执行不用的方法 从而达到不同的效果
     target-action设计模式存在的意义即将时间分离 View只负责显示 具体的事情处理交给Controller 是MVC中View层和Controller层通信的一种方式
     */

    // 创建三个实例对象
    TouchView *changeColorView = [[TouchView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];
    changeColorView.backgroundColor = [UIColor yellowColor];
#warning 3.在外界设置target action
    changeColorView.target = self;
    changeColorView.action = @selector(ChangeColor:);
    [self.view addSubview:changeColorView];

    TouchView *changePostionView = [[TouchView alloc]initWithFrame:CGRectMake(50, 200, 100, 100)];
    changePostionView.backgroundColor = [UIColor redColor];
    changePostionView.target = self;
    changePostionView.action = @selector(ChangePostion:);
    [self.view addSubview:changePostionView];

    TouchView *changeSizeView = [[TouchView alloc]initWithFrame:CGRectMake(50, 350, 100, 100)];
    changeSizeView.backgroundColor = [UIColor purpleColor];
    changeSizeView.target = self;
    changeSizeView.action = @selector(ChangeBounds:);
    [self.view addSubview:changeSizeView];

    [changeColorView release];
    [changePostionView release];
    [changeSizeView release];
}
#warning 5.实现action方法
//- (void)ChangeColor:(TouchView *)view
//{
//    // 1.修改self.view的颜色为红色
    self.view.backgroundColor = [UIColor redColor];
//    // 2.修改当前点击视图的颜色为随机色
//    view.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
//}

#pragma mark - 改变背景颜色
- (void)ChangeColor:(TouchView *)view
{
    view.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
}
#pragma mark - 改变视图位置
- (void)ChangePostion:(TouchView *)view
{
    CGFloat minX = 0;
    CGFloat maxX = [[UIScreen mainScreen]bounds].size.width;
    CGFloat x = arc4random()%(int)(maxX - minX + 1) + minX;
    CGFloat minY = 0;
    CGFloat maxY = [[UIScreen mainScreen]bounds].size.height;
    CGFloat y = arc4random()%(int)(maxY - minY + 1) + minY;
    view.center = CGPointMake(x, y);
}
#pragma mark - 改变视图大小
- (void)ChangeBounds:(TouchView *)view
{
    CGFloat minWidth = 38;
    CGFloat maxWidth = [[UIScreen mainScreen]bounds].size.width;
    CGFloat width = arc4random()%(int)(maxWidth - minWidth + 1) + minWidth;
    CGFloat minHeight = 38;
    CGFloat maxHeight = [[UIScreen mainScreen]bounds].size.height;
    CGFloat height = arc4random()%(int)(maxHeight - minHeight + 1) + minHeight;
    view.bounds = CGRectMake(0, 0, width, height);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值