一、touch
- (void)touchesBegan:(NSSet*)touches
withEvent:(UIEvent*)event //开始触摸,就只被执行一次
{
// NSLog(@"开始触摸.");
/**
* 2015-08-26 10:32:10.933 LessonUITouch[1650:58351]
<UITouch: 0x7fac30f8f230>
phase: Began tap count: 1
window: <UIWindow: 0x7fac30f905b0;
frame = (0 0; 375 667);
gestureRecognizers = <NSArray: 0x7fac30f91800>;
layer = <UIWindowLayer: 0x7fac30f90540>
>
view: <TouchView: 0x7fac30f948a0;
frame = (100 100; 100 100);
layer = <CALayer: 0x7fac30f8f040>
>
/**
* 2015-08-26 10:32:10.933 LessonUITouch[1650:58351]
<UITouch: 0x7fac30f8f230>
phase: Began tap count: 1
window: <UIWindow: 0x7fac30f905b0;
frame = (0 0; 375 667);
gestureRecognizers = <NSArray: 0x7fac30f91800>;
layer = <UIWindowLayer: 0x7fac30f90540>
>
view: <TouchView: 0x7fac30f948a0;
frame = (100 100; 100 100);
layer = <CALayer: 0x7fac30f8f040>
>
--<<<<<<<<<<------------------下面的信息相对重要-------->>>>>>>>>>>-
location in window: {121.5, 158.5} // 当前光标在window上的位置
previous location in window: {121.5, 158.5} //拖拽前上一个点的位置 相对于window
location in view: {21.5, 58.5} //当前光标在touchView上的位置
previous location in view: {21.5, 58.5} //拖拽前上一个点的位置 相对于touchView
*/
//保存了触摸时候手指的信息
UITouch*touch = [touchesanyObject];
// NSLog(@"%@", touch);
//取出一个点
CGPointp1 = [touchlocationInView:self];
self.startPoint= p1;
// CGPoint p2 = [touch previousLocationInView:self];
// NSLog(@"%@", NSStringFromCGPoint(p2));
// NSLog(@"%@", NSStringFromCGPoint(p1));
self.backgroundColor= [UIColorcolorWithRed:arc4random()%256/255.0green:arc4random()%256/255.0blue:arc4random()%256/255.0alpha:1.000];
}
- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event
{
NSLog(@"扫兴,抚摸中断了...");
}
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch*touch = [touchesanyObject];
CGPointending = [touchlocationInView:self];
CGFloatdx = ending.x-self.startPoint.x;
if(dx >10) {
NSLog(@"右滑");
}elseif(dx < -10) {
NSLog(@"左滑");
}
NSLog(@"%f", dx);
NSLog(@"摸够了...");
}
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
// NSLog(@"抚摸中...");
UITouch*touch = [touchesanyObject];
CGPointp1 = [touchlocationInView:self];
CGPointp2 = [touchpreviousLocationInView:self];
//计算偏移的距离
CGFloatdx = p1.x- p2.x;
CGFloatdy = p1.y- p2.y;
//原来中心点的坐标加上偏移量就得到了新的位置
// CGPoint p3 = {self.center.x + dx, self.center.y + dy};
// NSLog(@"%@", NSStringFromCGPoint(p2));
// NSLog(@"%@", NSStringFromCGPoint(p1));
//self.center = p3;
// self.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.000];
}
二、摇一摇
加载视图 加载的是自带的self.view
- (void)loadView
{
[superloadView];//如果要重写这个方法,就必须要写这个方法。如果没有这个方法我们就不能使用它因为它是一个空的视图
}
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor= [UIColorcolorWithRed:0.315green:0.867blue:1.000alpha:1.000];
// Do any additional setup after loading the view.
}
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent*)event
{
NSLog(@"开始摇");
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent*)event
{
// SecondViewController *sec = [[SecondViewController alloc]init];
WeChatLoginViewController*weChat = [[WeChatLoginViewControlleralloc]init];
[selfpresentViewController:weChatanimated:YEScompletion:nil];
[weChatrelease];
NSLog(@"摇完了");
}
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent*)event
{
NSLog(@"取消");
{
[superloadView];//如果要重写这个方法,就必须要写这个方法。如果没有这个方法我们就不能使用它因为它是一个空的视图
}
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor= [UIColorcolorWithRed:0.315green:0.867blue:1.000alpha:1.000];
// Do any additional setup after loading the view.
}
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent*)event
{
NSLog(@"开始摇");
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent*)event
{
// SecondViewController *sec = [[SecondViewController alloc]init];
WeChatLoginViewController*weChat = [[WeChatLoginViewControlleralloc]init];
[selfpresentViewController:weChatanimated:YEScompletion:nil];
[weChatrelease];
NSLog(@"摇完了");
}
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent*)event
{
NSLog(@"取消");
}
三、响应者链
/**
* 响应者链
响应者链分为两个过程(查找与响应)
1.查找过程
先找到是哪个应用程序->找是哪个window ->哪个控制器(页面) -> self.view ->所有的子视图 找到为止
2.响应过程
如何才能处理事件?
能捕捉到点击事件,即实现了touchBegan等方法
看所有子视图能不能处理 -> self.view ->所在控制器->所在window ->所属应用程序
如果没有视图处理这个事件,那么这个点击事件就被遗弃
*/
//阻断了查询的过程,不让进行查询的过程,都不会走到响应的步骤。
//直译:用户交互影响授权 系统默认是NO(阻断查询过程(把交互关闭))/ YES(不阻断查询过程(把交互打开))
self.window.userInteractionEnabled=YES;
WeChatLoginViewController*rootVC = [[WeChatLoginViewControlleralloc]init];
self.window.rootViewController= rootVC;
* 响应者链
响应者链分为两个过程(查找与响应)
1.查找过程
先找到是哪个应用程序->找是哪个window ->哪个控制器(页面) -> self.view ->所有的子视图 找到为止
2.响应过程
如何才能处理事件?
能捕捉到点击事件,即实现了touchBegan等方法
看所有子视图能不能处理 -> self.view ->所在控制器->所在window ->所属应用程序
如果没有视图处理这个事件,那么这个点击事件就被遗弃
*/
//阻断了查询的过程,不让进行查询的过程,都不会走到响应的步骤。
//直译:用户交互影响授权 系统默认是NO(阻断查询过程(把交互关闭))/ YES(不阻断查询过程(把交互打开))
self.window.userInteractionEnabled=YES;
WeChatLoginViewController*rootVC = [[WeChatLoginViewControlleralloc]init];
self.window.rootViewController= rootVC;
[rootVC
release];
1. 姚贝娜的照片。
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"123.jpg"
]];
imageView.frame = CGRectMake(0, 0, 375, 667);
[self.window addSubview:imageView];
[imageView release];
UIButton *button1 = [UIButton buttonWithType:(UIButtonTypeCustom)];
[button1 addTarget:self action:@selector(action:) forControlEvents:(UIControlEventTouchUpInside)];
[button1 setTitle:@"点我" forState:(UIControlStateNormal)];
[button1 setTitle:@"点我" forState:(UIControlStateHighlighted)];
[button1 setTitleColor:[UIColor redColor] forState:(UIControlStateHighlighted)];
button1.frame = CGRectMake(70, 180, 200, 200);
[imageView addSubview:button1];
imageView.userInteractionEnabled = YES;
imageView.frame = CGRectMake(0, 0, 375, 667);
[self.window addSubview:imageView];
[imageView release];
UIButton *button1 = [UIButton buttonWithType:(UIButtonTypeCustom)];
[button1 addTarget:self action:@selector(action:) forControlEvents:(UIControlEventTouchUpInside)];
[button1 setTitle:@"点我" forState:(UIControlStateNormal)];
[button1 setTitle:@"点我" forState:(UIControlStateHighlighted)];
[button1 setTitleColor:[UIColor redColor] forState:(UIControlStateHighlighted)];
button1.frame = CGRectMake(70, 180, 200, 200);
[imageView addSubview:button1];
imageView.userInteractionEnabled = YES;