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

Target—Action

时间:2015-08-26 01:27:37      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

题目:

?自定义view,实现点击事件。 

不同的实例,点击效果不同:点击视图改变颜色、点击视图改变位置,点击视图修改??。

#import "AppDelegate.h"

#import "TargetActionViewController.h"

@interface AppDelegate ()

 

@end

 

@implementation AppDelegate

 

- (void)dealloc

{

    [_window release];

    [super dealloc];

}

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor whiteColor];

    TargetActionViewController *rootVC = [[TargetActionViewController alloc]init];

    self.window.rootViewController = rootVC;

    [rootVC release];

    

    [self.window makeKeyAndVisible];

    return YES;

}

 

 

 

 

 

#import "TargetActionViewController.h"

#import "TargetActionView.h"

@interface TargetActionViewController ()

 

@end

 

@implementation TargetActionViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    TargetActionView *view1 = [[TargetActionView alloc]initWithFrame:CGRectMake(130, 100, 100, 100)];

    view1.backgroundColor = [UIColor magentaColor];

    [view1 addTarget:self Action:@selector(changeColor:)];

    [self.view addSubview:view1];

    

    TargetActionView *view2 = [[TargetActionView alloc]initWithFrame:CGRectMake(40, 250, 100, 100)];

    view2.backgroundColor = [UIColor greenColor];

    [view2 addTarget:self Action:@selector(changePosition:)];

    [self.view addSubview:view2];

    

    TargetActionView *view3 = [[TargetActionView alloc]initWithFrame:CGRectMake(230, 250, 100, 100)];

    view3.backgroundColor = [UIColor purpleColor];

    [view3 addTarget:self Action:@selector(changeSize:)];

    [self.view addSubview:view3];

    

    [view1 release];

    [view2 release];

    [view3 release];

}

 

- (void)changeColor:(TargetActionView *)touchView1

{

    touchView1.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];

}

- (void)changePosition:(TargetActionView *)touchView2

{

    CGFloat minX = 0;

    CGFloat maxX = [[UIScreen mainScreen]bounds].size.width;

    CGFloat temp1 = arc4random() % (int)(maxX - minX + 1) + minX;

    

    CGFloat minY = 0;

    CGFloat maxY = [[UIScreen mainScreen]bounds].size.height;

    CGFloat temp2 = arc4random() % (int)(maxY - minY + 1) + minY;

    

    touchView2.center = CGPointMake(temp1, temp2);

}

- (void)changeSize:(TargetActionView *)touchView3

{

    CGFloat minWidth = 38;

    CGFloat maxWidth = [[UIScreen mainScreen]bounds].size.width;

    CGFloat temp1 = arc4random() % (int)(maxWidth - minWidth + 1) + minWidth;

    

    CGFloat minHeight = 38;

    CGFloat maxHeight = [[UIScreen mainScreen]bounds].size.height;

    CGFloat temp2 = arc4random() % (int)(maxHeight - minHeight + 1) + minHeight;

    

    touchView3.bounds = CGRectMake(0, 0, temp1, temp2);

    

}

 

 

 

 

 

#import <UIKit/UIKit.h>

 

@interface TargetActionView : UIView

 

@property(nonatomic,retain)id target;

@property(nonatomic,assign)SEL action;

 

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

 

 

 

@end

 

 

 

 

 

 

#import "TargetActionView.h"

 

@implementation TargetActionView

 

- (void)dealloc

{

    [_target release];

    [super dealloc];

}

 

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

{

    self = [super init];

    if (self) {

        _target = target;

        _action = action;

    }

}

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

{

    [_target performSelector:_action withObject:self];

}

 

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

{

    

}

 

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

{

    

}

 

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

{

    

}

 

/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    // Drawing code

}

*/

 

@end

Target—Action

标签:

原文地址:http://www.cnblogs.com/jx451578429/p/4759064.html

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