在MyView.h中定义两个属性, 用来接收移动前的点坐标, 和移动后的点坐标.
@property(nonatomic, assign) CGPoint startPoint;
@property(nonatomic, assign) CGPoint endPoint;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//用self.startPoint存放点击开始时的点坐标.
UITouch *touch = [touches anyObject];
self.startPoint = [touch locationInView:self];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//用self.endPoint存放移动后的点坐标.
UITouch *touch = [touches anyObject];
self.endPoint = [touch locationInView:self];
//思想: 鼠标移动多少, 图片的中心(center)也移动多少
//鼠标移动的水平方向距离: 移动后的点坐标的横坐标(_endPoint.x) - 开始触摸时的点坐标的横坐标(_startPoint.x)
//鼠标移动的竖直方向距离: 移动后的点坐标的横坐标(_endPoint.y) - 开始触摸时的点坐标的横坐标(_startPoint.y)
CGFloat x = self.center.x + _endPoint.x - _startPoint.x;
CGFloat y = self.center.y + _endPoint.y - _startPoint.y;
self.center = CGPointMake(x, y);
}