target - action 设计模式



#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>


@property (strong, nonatomic) UIWindow *window;

@end


//=============================

#import "AppDelegate.h"

#import "RootViewController.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];

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

    self.window.rootViewController = rootVC;

    [rootVC release];

    [self.window makeKeyAndVisible];

    return YES;

}


- (void)applicationWillResignActive:(UIApplication *)application {

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}


- (void)applicationDidEnterBackground:(UIApplication *)application {

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}


- (void)applicationWillEnterForeground:(UIApplication *)application {

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}


- (void)applicationDidBecomeActive:(UIApplication *)application {

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}


- (void)applicationWillTerminate:(UIApplication *)application {

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}


@end

//===============================

#import <UIKit/UIKit.h>


@interface RootViewController : UIViewController


@end

//===============================

#import "RootViewController.h"

#import "TouchView.h"

@interface RootViewController ()


@end


@implementation RootViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    // 创建三个实例对象.

    // target - action 设计模式

    /**

     target - action 是模仿系统的Button. 即将事件的处理交给外界, 不在自己的内部写死.

     target - action 设计模式主要设计到两方面的内容

     target: 目标

     action: 动作

     target - action可以让不同的实例对象在相同的时间点执行不同的方法, 从而达到不同的效果

     target - action设计模式存在的意义即将事件分离, view只负责显示, 具体的事件处理交给Controller.MVCView层和Controller层通信的一种方式

     **/

    

    // 1.OC学过的Delegate

    // 2.中午找个gif图片

    

    

    

    

    

    

    TouchView *changeColorView = [[TouchView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];

    changeColorView.backgroundColor = [UIColor yellowColor];

#warning 3.在外界设置target, action.

//    changeColorView.target = self;

//    changeColorView.action = @selector(changeColor:);

    [changeColorView addTarget:self action:@selector(changeColor:)];

    [self.view addSubview: changeColorView];

    

    TouchView *changePostionView = [[TouchView alloc]initWithFrame:CGRectMake(100, 400, 100, 100)];

    changePostionView.backgroundColor = [UIColor yellowColor];

    changePostionView.target = self;

    changePostionView.action = @selector(changePosition:);

    [self.view addSubview:changePostionView];

    

    

    TouchView *changeSizeView = [[TouchView alloc]initWithFrame:CGRectMake(100, 250, 100, 100)];

    changeSizeView.backgroundColor = [UIColor yellowColor];

    changeSizeView.target = self;

    changeSizeView.action = @selector(changeSize:);

    [self.view addSubview: changeSizeView];

    

    

}

#warning 5.实现action方法

- (void)changeColor:(TouchView *)touchView

{

    NSLog(@"11");

    // 1.修改self.view的颜色为红色

    self.view.backgroundColor = [UIColor redColor];

    // 2.修改当前点击视图的颜色为随机色

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


}


- (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);

}




- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/

@end

//===============================

#import <UIKit/UIKit.h>


@interface TouchView : UIView

#warning 1.给外界提供目标和动作属性, 让外界可以设置.

@property(nonatomic, retain)id target; // 目标

@property(nonatomic, assign)SEL action; // 动作


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

@end


//===============================


#import "TouchView.h"


@implementation TouchView


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

{

    _target = target;

    _action = action;

}




#warning 2.重写dealloc方法释放对应的实例变量.

- (void)dealloc

{

    [_target release];

    [super dealloc];

}





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

{

    // 1.我们现在面临了什么问题

    // 2.通过到目前所学的知识点, 怎样可以实现题目的需求

//    [self changeColor];

    // 更改为[目标 changeColor];

#warning 4.在某个时间点让目标去执行对应的方法;

    // [目标 方法];

    [_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

{

    

}


#pragma mark - 改变背景颜色

- (void)changeColor

{

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

}


#pragma mark - 改变位置

- (void)position

{

    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;

    self.center = CGPointMake(x, y);

}


- (void)changeSize

{

    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;

    // 注意此处的bounds

    self.bounds = CGRectMake(0, 0, width, height);

}


/*

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

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    // Drawing code

}

*/


@end




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值