target-action設計模式

 target-action是模仿系統的button。即將事件的處理交給外界,不在本身內部寫死。設計模式

 target-action設計模式主要涉及到兩方面的內容dom

 target:目標atom

 action:動做spa

 target-action可讓不一樣的實例對象在相同的時間點執行不一樣的方法,從而達到不一樣的效果設計

 target-action設計模式存在的意義即將事件分離,View只負責顯示,具體的事件處理交給controller。是MVCView層和Controller層通訊的一種方式。orm

#import <UIKit/UIKit.h>

@interface TouchView : UIView

#warning 1.給外界提供目標和動做屬性,讓外界能夠設置。
@property(nonatomic, retain)id tagret; // 目標
@property(nonatomic, assign)SEL action; // 動做

@end

  

#import "TouchView.h"

@implementation TouchView
#warning 2.重寫dealloc方法 釋放對應的實例變量
- (void)dealloc
{
    [_tagret release];
    [super dealloc];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
#warning 4.在某個時間點讓目標去執行對應的方法
    [_tagret performSelector:_action withObject:self];
}

  

#import "RootViewController.h"
#import "TouchView.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    TouchView *colorView = [[TouchView alloc] initWithFrame:CGRectMake(130, 50, 100, 100)];
    colorView.backgroundColor = [UIColor redColor];

#warning 3.在外界設置target、action。
    colorView.tagret = self; 
    colorView.action = @selector(changeColor:);
    [self.view addSubview:colorView];
    [colorView release];
    
    TouchView *positionView = [[TouchView alloc] initWithFrame:CGRectMake(130, 250, 100, 100)];
    positionView.backgroundColor = [UIColor greenColor];
    positionView.tagret = self;
    positionView.action = @selector(changePosition:);
    [self.view addSubview:positionView];
    [positionView release];
    
    TouchView *sizeView = [[TouchView alloc] initWithFrame:CGRectMake(130, 450, 100, 100)];
    sizeView.backgroundColor = [UIColor blueColor];
    sizeView.tagret = self;
    sizeView.action = @selector(changeSize:);
    [self.view addSubview:sizeView];
    [sizeView release];
    
}

#warning 5.實現action方法,方法按需求實現
- (void)changeColor:(TouchView *)touchView
{
    touchView.backgroundColor = [UIColor colorWithRed:(arc4random() % 256 / 255.0) green:(arc4random() % 256 / 255.0) blue:(arc4random() % 256 / 255.0) alpha:1];
}

- (void)changePosition:(TouchView *)touchView
{
    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;
    
    touchView.center = CGPointMake(x, y);
}

- (void)changeSize:(TouchView *)touchView
{
    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;
    
    touchView.bounds = CGRectMake(0, 0, width, height);
}
相關文章
相關標籤/搜索