#import "RootViewController.h"
#import "RootV.h"
@interface RootViewController ()
@property (nonatomic, retain)RootV *rootV;
@property (nonatomic, retain)RootV *roooo;
@end
@implementation RootViewController
// 知识点 1:
// 响应者类
// UIResponder
// 响应者链 从底层往上层传递 , 上层优先响应
// 当用户触摸屏幕,产生传递给应用,应用传递给 window ,window检测它上面所有的子视图,如果有子视图,事件传递给子视图,直到当前没有子视图,才开始判断自己能不能响应事件.自己响应不了就将事件抛给父视图响应.以此类推
// 事件检测从底层视图开始往子视图检测
// 事件响应是从子视图往下响应
// 当到达window 时都无法响应, 事件将会被抛弃
// 有两个控件的用户交互 默认是关闭的 UILabel UIImageView
// self.view.userInteractionEnabled = YES; // 通过这个手势打开
// 当子视图的所在位置超出了父视图的范围时,点击超出的部分无法响应
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor grayColor];
RootV *rootV = [[RootV alloc]initWithFrame:CGRectMake(40, 100, 100, 100)];
rootV.backgroundColor = [UIColor redColor];
[self.view addSubview:rootV];
[rootV release];
// 知识点 2:
// target - Action
// 3 .执行
[rootV addMyTarget:self action:@selector(changecolor:)];
RootV *roooo = [[RootV alloc]initWithFrame:CGRectMake(40, 250, 100, 100)];
roooo.backgroundColor = [UIColor greenColor];
[self.view addSubview:roooo];
[roooo addMyTarget:self action:@selector(changePoin:)];
[roooo release];
}
// 4. 步: 动作方法
- (void)changecolor:(RootV *)rootV {
rootV.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
}
- (void)changePoin:(RootV *)ro {
ro.center = CGPointMake(arc4random() % 300, arc4random() % 600);
}
// 手指触碰时触发
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// 打印当前方法名
// NSLog(@"%s", __FUNCTION__);
NSLog(@"手指触碰时触发");
// self.view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
}
// 手指 移动时触发
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
// NSLog(@"%s", __FUNCTION__);
// self.view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
}
// 手指离开时触发
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// NSLog(@"%s", __FUNCTION__);
// self.view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
}
// 触摸取消 (当来电话时,被动断开触发)
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
// NSLog(@"%s", __FUNCTION__);
// self.view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
}
// 摇一摇 开始 摇一摇快捷键 conm + crtl + z
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
self.view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
}
// 摇一摇 结束
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
}
// 摇一摇 中断
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
}
触摸 , 响应者链 , 摇一摇
最新推荐文章于 2019-10-24 19:48:16 发布