UITouch

UITouch

触摸事件 


#pragma mark == 手指移动触发的方法

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {

//1.获取触摸对象touch

UITouch *touch = [touches anyObject];

//2.通过触摸对象来获取触摸的点参照谁的坐标系:self.view

lastPoint= [touch locationInView:self.view];

}

#pragma mark =====手指按下移动触发的方法

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {

//1.touch

UITouch *touch = [touches anyObject];

//point

CGPoint currentPoint = [touch locationInView:self.view];

//移动的距离

floatx = currentPoint.x-lastPoint.x;
floaty = currentPoint.y-lastPoint.y;

//改变view的位置
//view.frame = CGRectMake(view.frame.origin.x + x, view.frame.origin.y, CGRectGetWidth(view.frame), CGRectGetHeight(view.frame));

//如果上一个点不在对象区域范围内就不让对象移动
//点在图上
if(CGRectContainsPoint(_Myview.frame,lastPoint)) {

//偏移
_Myview.center=CGPointMake(_Myview.center.x+x,_Myview.center.y+y);

//赋值给上一个点,更新点坐标
lastPoint= currentPoint;

}
}

你可能感兴趣的:(UITouch)