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