2013-03-18 74 views
3

我在IB中设置了4个UIImageViews。我还有一个描述状态的UILabel(如下面的代码所述)。 我使用的touchesBegan,touchesMoved和touchesEnd方法来捕捉对象移动如下:iOS >>拖放问题>> UIView返回原位

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

    UITouch* aTouch = [touches anyObject]; 
    UIView* aView = [aTouch view]; 
    if (aView != self.view) { 
     [self.view bringSubviewToFront:aView]; 
     self.statusLabel.text = @"You're Dragging..."; 
    } 
} 

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

    UITouch* aTouch = [touches anyObject]; 
    UIView* aView = [aTouch view]; 
    if (aView != self.view) { 
     aView.center = [aTouch locationInView:self.view]; 
    } 
} 

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

    self.statusLabel.text = @"At Ease"; 
} 

的问题是,当我移动(在模拟器),他们“跳”回原来的位置的UIImageViews的一个如其在IB中的设置,而不是留在我放弃它们的位置。

为什么发生? 如何在“touchesEnd”中“捕捉”UIImageView的新位置并避免此“跳跃”?

P.S.我注意到,如果我不更新“touchesEnd”的标签,UIImageView仍然处于最后位置,直到我点击另一个UIImageView;这里发生了什么事?

+0

代码看起来并且在跑得很快的样机中运行良好(尽管我可能会跟踪哪个视图正在移动并根据“touchesEnded”位置设置其最终位置)。你确定你没有在你的代码的其他地方设置移动视图的中心,或者从笔尖重新加载整个视图? – bobnoble 2013-03-18 15:48:18

回答

2

你的代码似乎correct..you只是尝试取消选中自动布局后... 后来我发现作者已经发布了自己的答案......所以,对不起,这个职位..

+0

你已经试过这个..对于最近的答复... – Shivaay 2013-03-19 06:43:35

+0

这并不是提供了一个问题的答案。要批评或要求作者澄清,请在其帖子下方留言。 – slfan 2013-03-19 07:00:16

+1

我已经问过对不起。 – Shivaay 2013-03-19 07:20:11

0

我取消选中IB中的“自动布局”复选框,它工作正常。 这不能成为这样做的“最佳实践”......但它有效。

相关问题