UI - Target-Action设计模式
来源:互联网 发布:淘宝店自动充值软件 编辑:程序博客网 时间:2024/06/04 17:55
target -action设计模式分5个步骤来完成
warning部分即为步骤顺序
#TouchView.h#import <UIKit/UIKit.h>@interface TouchView : UIView#warning 1.给外界提供目标和动作属性,让外界可以设置@property(nonatomic,retain)id target; // 目标@property(nonatomic,assign)SEL action; // 动作@end#TouchView.m#import "TouchView.h"@implementation TouchView#warning 2.重写dealloc方法释放对应的实例变量- (void)dealloc{ [_target release]; [super dealloc];}-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ // 1.我们现在面临了什么问题 // 2.通过到目前为止学过的知识点 怎样可以实现题目的需求// [self ChangeColor]; // 更改为目标去执行[目标 方法];#warning 4.在某个时间点让目标取执行对应的方法 [_target performSelector:_action withObject:self]; // 让target去执行action这个方法并且把self参数传出去 (self:当前点击的touchView)}#RootViewController.m#import "RootViewController.h"#import "TouchView.h"@interface RootViewController ()@end@implementation RootViewController- (void)viewDidLoad { [super viewDidLoad]; // target - action设计模式 /** target - action 是模仿系统的Button 即将事件的处理交给外界 不在自己的内部写死 target-action设计模式主要涉及到两方面的内容 target:目标 action:动作 target-action可以让不同的实例对象在相同的时间点执行不用的方法 从而达到不同的效果 target-action设计模式存在的意义即将时间分离 View只负责显示 具体的事情处理交给Controller 是MVC中View层和Controller层通信的一种方式 */ // 创建三个实例对象 TouchView *changeColorView = [[TouchView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)]; changeColorView.backgroundColor = [UIColor yellowColor];#warning 3.在外界设置target action changeColorView.target = self; changeColorView.action = @selector(ChangeColor:); [self.view addSubview:changeColorView]; TouchView *changePostionView = [[TouchView alloc]initWithFrame:CGRectMake(50, 200, 100, 100)]; changePostionView.backgroundColor = [UIColor redColor]; changePostionView.target = self; changePostionView.action = @selector(ChangePostion:); [self.view addSubview:changePostionView]; TouchView *changeSizeView = [[TouchView alloc]initWithFrame:CGRectMake(50, 350, 100, 100)]; changeSizeView.backgroundColor = [UIColor purpleColor]; changeSizeView.target = self; changeSizeView.action = @selector(ChangeBounds:); [self.view addSubview:changeSizeView]; [changeColorView release]; [changePostionView release]; [changeSizeView release];}#warning 5.实现action方法//- (void)ChangeColor:(TouchView *)view//{// // 1.修改self.view的颜色为红色//// self.view.backgroundColor = [UIColor redColor];// // 2.修改当前点击视图的颜色为随机色// view.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];//}#pragma mark - 改变背景颜色- (void)ChangeColor:(TouchView *)view{ view.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];}#pragma mark - 改变视图位置- (void)ChangePostion:(TouchView *)view{ 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; view.center = CGPointMake(x, y);}#pragma mark - 改变视图大小- (void)ChangeBounds:(TouchView *)view{ 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; view.bounds = CGRectMake(0, 0, width, height);}
0 0
- UI target/action设计模式
- UI - Target-Action设计模式
- UI 04 target...action 设计模式
- UI阶段的 Target/Action设计模式
- UI-Target-action模式
- Target-Action 设计模式
- target/action设计模式
- Target-Action 设计模式
- target/action设计模式
- target/action 设计模式
- target - action 设计模式
- Target/Action 设计模式
- UIday05_zy01:target/action 设计模式
- 13.target/action设计模式
- UI开发----target/action设计模式和代理设计模式以及手势识别器
- target - action设计模式的思想
- iOS设计模式之Target-Action
- iOS开发 -- 耦合 ,target/action设计模式
- [ERROR:] [插入失败] code is 9998;desc is 得到唯一对象不唯一exception is null
- Android Studio开发入门-引用jar及so文件
- hpu 1713 参观城市 <bfs+邻接表>
- CSU 1329: 一行盒子(双向链表)经典 13年省赛题
- 基友记 SDUT 2189
- UI - Target-Action设计模式
- 在线接收短信验证
- 万年历cpp代码
- 【C++】万年历的实现
- StaffLoginValve登录异常
- Qt QpenGL 入门教程
- POJ 2065 SETI(高斯消元解模方程组)
- OpenStack点滴积累2--KeyStone的认证流程
- R语言-坐标轴画法要旨