码迷,mamicode.com
首页 > 移动开发 > 详细

iOS设计模式之Target-Action

时间:2014-08-31 09:18:31      阅读:9530      评论:0      收藏:0      [点我收藏+]

标签:target-action设计模式 u

目标-行为(Target-Action)模式(目的在于让代码解耦合,使代码与代码之间关联性降低,便于后期开发维护)

Target-action----这个设计模式用按钮,等控件把用户的交互变成代码,让程序可以执行;

Target-action :通俗易懂的说也就是
一个对象包含一些生成一个消息表达式的元素,当一个确定事件出现时,把这些元素到一起组成消息和发送这个消息。
有些概念是比较抽象模糊的,下面通过一个实例来诠释

Target-Action模式


首先先看一下效果图:
bubuko.com,布布扣


通过效果图可以清楚的知道,通过点击相应的view视图产生相应的效果.在这里就运用到了Target-Action模式

在这里先构建框架思路,不至于看起来那么茫然:在这里其实很简单,分为两个步骤:
第一:创建三个UIView对象
第二:构造Target-Action(关键)
在这里运用到了随机色:
实现随机颜色的方法:

+ (UIColor *)randomColor

{

   return [selfcolorWithRed:arc4random() %256 /255.0green:arc4random() %256 /255.0blue:arc4random() %256 /255.0alpha:1];

}


//第一创建三个UIView对象

代码:

//  MHTViewController.m

//  LessonTragetAction

//  Copyright (c) 2014 Summer. All rights reserved.

#import"MHTViewController.h"

#import"CustomView.h"

#import"UIColor+RandomColor.h"

#define kCyanView_Frame CGRectMake(100,130, 120, 120)

#define kGreenView_Frame CGRectMake(15,260, 120, 120)

#define kGrayView_Frame CGRectMake(185,260, 120, 120)

@interfaceMHTViewController ()

@end

@implementation MHTViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNilbundle:nibBundleOrNil];

    if (self) {

       // Custom initialization

    }

   return self;

}

- (void)viewDidLoad

{

    [superviewDidLoad];

   // Do any additional setup after loading the view.

   self.view.backgroundColor = [UIColoryellowColor];

//view1

    CustomView *greenView = [[CustomViewalloc]initWithFrame:kGreenView_Frame];

    greenView.backgroundColor = [UIColorgreenColor];

    greenView.layer.cornerRadius =60;

    [greenViewaddTarget:selfaction:@selector(changeSuperviewColor:)forControlEvents:UIControlEventTouchUpInside];

    greenView.layer.masksToBounds =YES;

    [self.viewaddSubview:greenView];

    [greenViewrelease];

  //view2

    CustomView *cyanView = [[CustomViewalloc]initWithFrame:kCyanView_Frame];

    cyanView.backgroundColor = [UIColorcyanColor];

    [self.viewaddSubview:cyanView];

    cyanView.layer.cornerRadius =60;

    [cyanViewaddTarget:selfaction:@selector(changeSelfColor:)forControlEvents:UIControlEventTouchDown];

    cyanView.layer.masksToBounds =YES;

    [cyanViewrelease];

  //view3

    CustomView*grayView = [[CustomViewalloc]initWithFrame:kGrayView_Frame];

    grayView.backgroundColor = [UIColorgrayColor];

    [self.viewaddSubview:grayView];

 [grayViewaddTarget:selfaction:@selector(changeSelfLocation:)forControlEvents:UIControlEventTouchUpInside];

    grayView.layer.cornerRadius =60;

    grayView.layer.masksToBounds =YES;

    [grayViewrelease];


    NSArray *titles =@[@"更改父视图的颜色" ,@"更改自身的颜色" ,@"更改自身的位置"];

    for (int i = 0; i < 3; i++) {

        UILabel *lable = [[UILabelalloc]initWithFrame:CGRectMake(0,0,120, 120)];

        lable.layer.cornerRadius =60;

        lable.layer.masksToBounds =YES;

        lable.text = titles[i];

        lable.font = [UIFontfontWithName:@"EuphemiaUCAS"size:15];

        lable.textAlignment =NSTextAlignmentCenter;

        lable.backgroundColor = [UIColorclearColor];

        if (0 == i) {

            [greenViewaddSubview:lable];

        }elseif (1 == i){

            [cyanViewaddSubview:lable];

        }elseif (2 == i){

            [grayViewaddSubview:lable];

        }

        [lablerelease]; 

        //设置分割线

        UILabel *hintLine = [[UILabelalloc]initWithFrame:CGRectMake(50,80,220, 2)];

        hintLine.backgroundColor = [UIColorgrayColor];

        hintLine.layer.cornerRadius =5;

        hintLine.layer.masksToBounds =YES;

        [self.viewaddSubview:hintLine];

        [hintLinerelease];

       //设置功能的提示内容

        UILabel *hintLable = [[UILabelalloc]initWithFrame:CGRectMake(50,60,220, 20)];

        hintLable.textAlignment =NSTextAlignmentCenter;

        hintLable.text =@"文本显示:";

        hintLable.font = [UIFontfontWithName:@"AlNile"size:12];

        [self.viewaddSubview:hintLable];

        [hintLablerelease];

        

       //copyrightLine(版权)

        UILabel *copyrightLine = [[UILabelalloc]initWithFrame:CGRectMake(20,520,280, 2)];

        copyrightLine.backgroundColor = [UIColorgrayColor];

        copyrightLine.layer.cornerRadius =5;

        copyrightLine.layer.masksToBounds =YES;

        [self.viewaddSubview:copyrightLine];

        [copyrightLinerelease];

        UILabel *copyrightLable = [[UILabelalloc]initWithFrame:CGRectMake(20,480,280, 20)];

        copyrightLable.textAlignment =NSTextAlignmentCenter;

        copyrightLable.text =@"Copyright ? 2014 summer";

        copyrightLable.font = [UIFontfontWithName:@"AlNile"size:12];

        [self.viewaddSubview:copyrightLable];

        [copyrightLablerelease];

        UILabel *copyrightLable1 = [[UILabelalloc]initWithFrame:CGRectMake(20,500,280, 20)];

        copyrightLable1.textAlignment =NSTextAlignmentCenter;

        copyrightLable1.text =@"summer2014mht@sina.com";

        copyrightLable1.font = [UIFontfontWithName:@"AlNile"size:12];

        [self.viewaddSubview:copyrightLable1];

        [copyrightLable1release];

}

//修改父视图的颜色

- (void)changeSuperviewColor:(CustomView *)view

{

    view.superview.backgroundColor = [UIColorrandomColor];

}

//修改自身的颜色

- (void)changeSelfColor:(CustomView *)view

{

    view.backgroundColor = [UIColorrandomColor];

}

//修改自身的位置

- (void)changeSelfLocation:(CustomView *)view

{

    view.center =CGPointMake(arc4random() % (260 -60 +1) + 60,arc4random() % (420 -60 + 1) + 60);

}

第二 构建Target-Action

//  CustomView.h

//  LessonTragetAction

//  Copyright (c) 2014 Summer. All rights reserved.

#import<UIKit/UIKit.h>

@interface CustomView :UIView

//创建方法

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;

@end

//*********************

//  CustomView.m

//  LessonTragetAction

//  Copyright (c) 2014 Summer. All rights reserved.

#import"CustomView.h"

#import"UIColor+RandomColor.h"

@interfaceCustomView()

{

    id _target;  //目标

    SEL _action; //行为

    UIControlEvents _controlEvents;

}

@end

@implementation CustomView


- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

       // Initialization code 

    }

   return self;

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

   if (UIControlEventTouchDown ==_controlEvents) {

       //当当前视图接触到触摸事件之后,交由target去处理

        [_targetperformSelector:_actionwithObject:self];

    }

}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

{

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

   if (UIControlEventTouchUpInside ==_controlEvents) {

        [_targetperformSelector:_actionwithObject:self];

    }

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

}

//为当前视图指定当视图接收到响应事件之后,target来通过action方法进行响应.

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents

{

   //用实例变量存储外界传入的参数,方便在其他方法中使用.

    _target = target;

    _action = action;

    _controlEvents = controlEvents;//触发时机

}

@end




iOS设计模式之Target-Action

标签:target-action设计模式 u

原文地址:http://blog.csdn.net/mhtios/article/details/38947121

(2)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!