{//开始触摸点
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
{
}