我在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