【发布时间】:2013-03-18 14:56:13
【问题描述】:
我在 IB 中设置了 4 个 UIImageView。我还有一个描述状态的 UILabel(如下面的代码所述)。 我使用 touchesBegan、touchesMoved 和 touchesEnd 方法来捕捉对象的移动,如下所示:
-(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";
}
问题是,在我移动(在模拟器中)其中一个 UIImageView 后,它们“跳”回其在 IB 中设置的原始位置,而不是留在我放置它们的位置。
为什么会这样? 如何在“touchesEnd”“捕获” UIImageView 的新位置并避免这种“跳跃”?
附:我注意到,如果我不更新“touchesEnd”处的标签,UIImageView 将保持在最后一个位置,直到我点击另一个 UIImageView;这是怎么回事?
【问题讨论】:
-
代码在我运行的快速模型中看起来和运行良好(尽管我可能会跟踪正在移动的视图并根据
touchesEnded位置设置其最终位置)。你确定你没有在代码中的其他地方设置移动视图的中心,或者从笔尖重新加载整个视图?
标签: ios animation uiview drag-and-drop