iOS触摸事件
2015-07-24 10:21
429 查看
{//开始触摸点
CGPoint _startPoint;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
//给testVIew加上颜色
self.backgroundColor=[UIColor redColor];
//阻断
//self.userInteractionEnabled=NO;
}
return self;
}
//有什么吩咐开始出发触摸事件的时候,执行touchBegan里面的预定的执行事件代码(开始触摸的时候,到这里来看看.)
//一次触摸事件发生时,该方法只执行一次
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"我要开始摸了.");
self.backgroundColor=[UIColor greenColor];//点击改变颜色
self.backgroundColor=[UIColor colorWithRed:kRandomColor green:kRandomColor blue:kRandomColor alpha:1];
//取出手指触控屏幕的坐标
// CGPoint sp=[[touches anyObject] locationInView:self];
// NSLog(@"%@",NSStringFromCGPoint(sp));
_startPoint=[[touches anyObject] locationInView:self];
NSLog(@"%@",NSStringFromCGPoint(_startPoint));
}
//一次触摸事件尚未结束,会一直调用该方法(没摸完,就一直摸)
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"触摸ing");
self.backgroundColor=[UIColor colorWithRed:kRandomColor green:kRandomColor blue:kRandomColor alpha:1];//点击移动一直改变颜色
//取移动中的点
CGPoint nowPoint=[[touches anyObject] locationInView:self];
NSLog(@"%@",NSStringFromCGPoint(nowPoint));
CGFloat x=nowPoint.x-_startPoint.x;
CGFloat y=nowPoint.y-_startPoint.y;
CGPoint centerPoint=CGPointMake(self.center.x+x,self.center.y+y);
self.center=centerPoint;
}
//一次触摸事件结束,执行该方法(摸完了)
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"结束了.");
}
//触摸事件被别的事件打断(有人打扰)
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
}
CGPoint _startPoint;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
//给testVIew加上颜色
self.backgroundColor=[UIColor redColor];
//阻断
//self.userInteractionEnabled=NO;
}
return self;
}
//有什么吩咐开始出发触摸事件的时候,执行touchBegan里面的预定的执行事件代码(开始触摸的时候,到这里来看看.)
//一次触摸事件发生时,该方法只执行一次
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"我要开始摸了.");
self.backgroundColor=[UIColor greenColor];//点击改变颜色
self.backgroundColor=[UIColor colorWithRed:kRandomColor green:kRandomColor blue:kRandomColor alpha:1];
//取出手指触控屏幕的坐标
// CGPoint sp=[[touches anyObject] locationInView:self];
// NSLog(@"%@",NSStringFromCGPoint(sp));
_startPoint=[[touches anyObject] locationInView:self];
NSLog(@"%@",NSStringFromCGPoint(_startPoint));
}
//一次触摸事件尚未结束,会一直调用该方法(没摸完,就一直摸)
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"触摸ing");
self.backgroundColor=[UIColor colorWithRed:kRandomColor green:kRandomColor blue:kRandomColor alpha:1];//点击移动一直改变颜色
//取移动中的点
CGPoint nowPoint=[[touches anyObject] locationInView:self];
NSLog(@"%@",NSStringFromCGPoint(nowPoint));
CGFloat x=nowPoint.x-_startPoint.x;
CGFloat y=nowPoint.y-_startPoint.y;
CGPoint centerPoint=CGPointMake(self.center.x+x,self.center.y+y);
self.center=centerPoint;
}
//一次触摸事件结束,执行该方法(摸完了)
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"结束了.");
}
//触摸事件被别的事件打断(有人打扰)
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
}
相关文章推荐
- iOS内存警告处理
- iOS 应用评分
- iOS 自定义一个视图作为根视图
- ios进制
- iOS 回收键盘
- iOS开发-清理缓存功能的实现
- iOS 开发之navigationController下子类视图发生位移的解决方法
- iOS 集成银联支付(绕过文档的坑,快速集成)
- 关于iOS7 上下黑条解决办法
- iOS开发之使用Runtime给Model类赋值
- iOS 集成银联支付(绕过文档的坑,快速集成)
- iOS的三维透视投影
- iOS 27个库
- 如何成为一名优秀的 iOS 开发工程师
- IOS研究成果——在autolayout 如何实现 Cell 的高度自适应
- iOS 用命令实现简单的打包过程
- iOS Sprite Kit教程之使用帮助文档以及调试程序
- iOS Sprite Kit教程之申请和下载证书
- 初涉iOS 通知机制
- iOS推送 (百度推送)