iOS - 关于一个view的拖拽
2015-08-18 本文已影响2411人
Mitchell
作者:Mitchell
- 自定义view
- 重写touch方法
- 只要不断的摸当前的view就会调用
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// 拿到UITouch就能获取当前点
UITouch *touch = [touches anyObject];
// 获取当前点
CGPoint curP = [touch locationInView:self];
// 获取上一个点
CGPoint preP = [touch previousLocationInView:self];
// 获取手指x轴偏移量
CGFloat offsetX = curP.x - preP.x;
// 获取手指y轴偏移量
CGFloat offsetY = curP.y - preP.y;
// 移动当前view
self.transform = CGAffineTransformTranslate(self.transform, offsetX, offsetY);
}
- 触摸事件被打断
// 触摸事件被迫打断(电话打来)
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- 手抬起的时候调用
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"%s",__func__);
}