码迷,mamicode.com
首页 > 其他好文 > 详细

自定义target - action 方法

时间:2015-10-31 23:03:37      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

HomeViewController.m

#import "HomeViewController.h"

#import "TouchViewPro.h"

 

@interface HomeViewController (){

    TouchViewPro *touchViewPro;

}

 

@end

 

@implementation HomeViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    touchViewPro = [[TouchViewPro alloc] initWithFrame:CGRectMake(65, 65, 100, 100)];

    touchViewPro.backgroundColor = [UIColor blackColor];

    [self.view addSubview:touchViewPro];

    [touchViewPro addTarget:self action:@selector(change)];

    [touchViewPro release];

}

 

- (void)change {

    touchViewPro.frame = CGRectMake(arc4random() % (325 - 50 + 1) + 50, arc4random() % (325 - 50 + 1) + 50, arc4random() % 51, arc4random() % 70);

}

 

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

 -----------------------------------------------<TouchViewPro.h>-----------------------------------

#import <UIKit/UIKit.h>

 

@interface TouchViewPro : UIView

- (void)addTarget:(id)target action:(SEL)action;

@end

 --------------------------------------------------<TouchViewPro.m>-------------------------------------------------

#import "TouchViewPro.h"

 

@interface TouchViewPro(){

    id _target;

    SEL _action;

}

 

@end

 

@implementation TouchViewPro

 

- (void)addTarget:(id)target action:(SEL)action {

    _action = action;

    _target = target;

}

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

    [_target performSelector:_action withObject:self];

    

//    self.frame = CGRectMake(arc4random() % (325 - 50 + 1) + 50, arc4random() % (325 - 50 + 1) + 50, arc4random() % 51, arc4random() % 70);

}

@end

 

自定义target - action 方法

标签:

原文地址:http://www.cnblogs.com/hsxblog/p/4926234.html

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