// Created By 郭仔 2015年04月15日18:01:00
今天北京7级大风,伴随沙尘暴,让哥骑自行车怎么搞~~~~~~~~~~~~~~~~~
=================================================
实现触摸:
UIView?支持触摸事件(因为继承于UIResponder),?而且?支持多 点触摸。
需要定义UIView?子类,实现触摸相关的?方法。
touches..began、touches..moved、touches...ended、 touches..canceled。
手势:
?手势:有规律的触摸。
UITouch代表触摸在屏幕上的?一根?手指。可以获取触摸时间和触摸 位置。
如何获取touch对象。touches集合中包含了视图上的所有?手势。
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
// 打开多点触摸,UIView默认是单点触摸
self.multipleTouchEnabled = YES;
self.backgroundColor = [UIColor cyanColor];
UIView * redView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 150, 150)];
redView.backgroundColor = [UIColor redColor];
[self addSubview:redView];
// 给属性赋值
self.redView = redView;
[redView release];
// ==================
UIView * label = [[UIView alloc]initWithFrame:CGRectMake(0, 100, 100, 100)];
label.backgroundColor = [UIColor orangeColor];
// 打断事件的交互
label.userInteractionEnabled = NO;
[self addSubview:label];
[label release];
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
btn.backgroundColor = [UIColor greenColor];
btn.frame = CGRectMake(20, 40, 50, 50);
[btn setTitle:@"按钮" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(xiangying:) forControlEvents:UIControlEventTouchUpInside];
[label addSubview:btn];
}
return self;
}
- (void)xiangying:(UIButton *)sender
{
NSLog(@"事件相应");
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"TouchesMoved!");
// 触摸点的个数
NSLog(@"%d",[touches count]);
// 1.获取移动先前的一个位置
UITouch * touch = [touches anyObject];
CGPoint prePoint = [touch previousLocationInView:self];
// 获取touch发生在哪个视图
UIView * touchView = [touch view];
if (touchView == self.redView) {
// 2.获取当前位置
CGPoint newPoint = [touch locationInView:self];
// 3.计算偏移量
// CGPoint offPoint = CGPointMake(newPoint.x - prePoint.x, newPoint.y-prePoint.y);
float offsetX = newPoint.x - prePoint.x;
float offsetY = newPoint.y - prePoint.y;
// 4.根据偏移量改变redView的位置
CGPoint center = self.redView.center;
center.x += offsetX;
center.y += offsetY;
//NSLog(@"%f",(self.redView.center.x);
// 这句话肯定报错,因为左边的第二个点是set方法
// self.redView.center.x = self.redView.center.x + offsetX;
//写开来就是这种表达: [self.redView setCenter:].x,肯定错误
self.redView.center = center;
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// 记录末点
CGPoint endPoint = [[touches anyObject]locationInView:self];
// 判断清扫方向
float dx = endPoint.x - self.startPoint.x;
float dy = endPoint.y - self.startPoint.y;
if(dx > 50 && fabs(dy) <= 50)
{
NSLog(@"向右");
}
else if (dx <= -50 && fabs(dy) < 50)
{
NSLog(@"向左");
}
else if(fabs(dx)< 50 && dy <= -50)
{
NSLog(@"向上");
}
else if(fabs(dx) < 50 && dy >= 50)
{
NSLog(@"向下");
}
else
{
NSLog(@"滑动太短");
}
NSLog(@"TouchesEnded!");
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// =====================
// 记录起点
CGPoint startPoint = [[touches anyObject]locationInView:self];
self.startPoint = startPoint;
//=================
NSLog(@"TouchesBegan!");
// 打印点击次数
UITouch * touch = [touches anyObject];
NSLog(@"点击次数:%d",touch.tapCount);
// 获取随机色
// 改变redView颜色 ,取0到1之间的值
float red = (arc4random()%256)/255.0;
float green = (arc4random()%256)/255.0;
float blue = (arc4random()%256)/255.0;
UIColor * randomColor = [UIColor colorWithRed:red green:green blue:blue alpha:1];
self.redView.backgroundColor = randomColor;
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"TouchesCancelled!");
}
- (void)dealloc
{
[_redView release];
[super dealloc];
}
======================
响应者链:
硬件检测到触摸操作,会将信息交给UIApplication,开始检测。
UIApplication -> window -> viewController -> view -> 检测所有?子 视图
最终确认触碰位置,完成响应者链的查询过程。
处理触碰事件:
检测到响应者后,实现touchesBegan:withEvent:等?方法,即处理事 件。
如果响应者没有处理事件,事件会向下传递。如果没有响应者处理,
则丢弃触摸事件。
事件处理的顺序与触摸检测查询相反。
触摸的?子视图 -> view -> viewController -> window -> UIApplication
阻断响应者链:
响应者链可以被打断。?无法完成检测查询过程。
视图类的属性 : userInteractionEnabled。关闭后能阻断查询过 程。
======================
另外一个容易忽略的小错误:
// 这句话肯定报错,因为左边的第二个点是set方法
// self.redView.center.x = self.redView.center.x + offsetX;
//写开来就是这种表达: [self.redView setCenter:].x,肯定错误另外注意控制器中容易忽略的一句赋值语句
- (void)loadView
{
LoginView * logV = [[LoginView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
self.view = logV; // 就是这里
[logV release];
}=======================
来吧,沙尘暴~~~~~~~~~
原文:http://blog.csdn.net/guoxianzhuang/article/details/45062879