触摸事件
//创建label类
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.backgroundColor = [UIColor greenColor];
// label的交互 默认是关闭 我们可以手动给他打开
self.userInteractionEnabled = YES;
// self addSubview:
self.text = @"我是label";
}
return self;
}
/*
#pragma mark ---触摸开始---
// 一开始触摸就会调用方法 (相当于手指按上去的时候)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// NSLog(@"触摸开始");
// 获取手指在屏幕上所触摸的点
UITouch *touch = [touches anyObject];
// 将触摸的位置 转化成点
CGPoint point = [touch locationInView:self];
NSLog(@"x === %f y === %f", point.x, point.y);
}
*/
// 此方法 提供给controller 调用
- (void)addTarget:(id)target action:(SEL)action
{
_target = target;
_action = action;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"label 的began");
// 代理
if ([_delegate respondsToSelector:@selector(changeViewColor)])
{
[self.delegate changeViewColor];
}
}
#pragma mark --- 触摸移动---
// 触摸移动会调用的方法 (相当于手指在屏幕上面移动)
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// NSLog(@"触摸移动");
UITouch *touch = [touches anyObject];
// 获取结束点
CGPoint endpoint = [touch locationInView:self];
NSLog(@"x === %f y === %f", endpoint.x, endpoint.y);
// 获取先前点
CGPoint startPoint = [touch previousLocationInView:self];
// 计算连点之间的移动
CGFloat spaceX = endpoint.x - startPoint.x;
CGFloat spaceY = endpoint.y - startPoint.y;
// 给视图重新赋值center 改变center 相当于改变 frame
self.center = CGPointMake(self.center.x + spaceX, self.center.y + spaceY);
}
#pragma mark ---触摸结束---
// 触摸结束会调用的方法 (相当于把手指抬起来的时候)
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// NSLog(@"触摸结束");
}
#pragma mark --- 触摸取消---
// 触摸取消: 一切被动的行为阻止了触摸事件 会调用的方法 比如:来短信 或者来电话 或者(通知栏和控制栏)
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
// NSLog(@"触摸取消");
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
/*
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
// 将触摸的位置 转化成点
CGPoint point = [touch locationInView:self];
NSLog(@"x === %f y === %f", point.x, point.y);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGpoint endPoint =
}
*/
@end
#pragma mark ---触摸开始---
// 一开始触摸就会调用方法 (相当于手指按上去的时候)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// NSLog(@"触摸开始");
// 获取手指在屏幕上所触摸的点
UITouch *touch = [touches anyObject];
// 将触摸的位置 转化成点
CGPoint point = [touch locationInView:self];
NSLog(@"x === %f y === %f", point.x, point.y);
}
#pragma mark --- 触摸移动---
// 触摸移动会调用的方法 (相当于手指在屏幕上面移动)
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// NSLog(@"触摸移动");
UITouch *touch = [touches anyObject];
// 判断触摸的视图
// 如果不是红色的视图 那么就是label
/*
if (touch.view != self)
{
// 第一种方式 直接在本类中创建label 触摸红视图 红视图动 触摸黄视图 黄视图动
// 获取结束点
CGPoint endpoint = [touch locationInView:self];
NSLog(@"x === %f y === %f", endpoint.x, endpoint.y);
// 获取先前点
CGPoint startPoint = [touch previousLocationInView:self];
// 计算连点之间的移动
CGFloat spaceX = endpoint.x - startPoint.x;
CGFloat spaceY = endpoint.y - startPoint.y;
// 给视图重新赋值center 改变center 相当于改变 frame
self.label.center = CGPointMake(self.label.center.x + spaceX, self.label.center.y + spaceY);
return;
}
*/
if(touch.view != self)
{
return;
}
// 获取结束点
CGPoint endpoint = [touch locationInView:self];
NSLog(@"x === %f y === %f", endpoint.x, endpoint.y);
// 获取先前点
CGPoint startPoint = [touch previousLocationInView:self];
// 计算连点之间的移动
CGFloat spaceX = endpoint.x - startPoint.x;
CGFloat spaceY = endpoint.y - startPoint.y;
// 给视图重新赋值center 改变center 相当于改变 frame
self.center = CGPointMake(self.center.x + spaceX, self.center.y + spaceY);
}
#pragma mark ---触摸结束---
// 触摸结束会调用的方法 (相当于把手指抬起来的时候)
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// NSLog(@"触摸结束");
}
#pragma mark --- 触摸取消---
// 触摸取消: 一切被动的行为阻止了触摸事件 会调用的方法 比如:来短信 或者来电话 或者(通知栏和控制栏)
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
// NSLog(@"触摸取消");
}

浙公网安备 33010602011771号