2013-02-20 98 views
1

我有一個UIView子類,包含幾個子視圖,我想拖動&拖放到UICollectionView中包含的其他UIViews之一。當拖動開始時,我想將拖動的視圖從其當前的大小縮放到拖動持續時間內的較小值(原始大小過大,以至於無法首先縮放它時方便地選擇放置目標)到目前爲止,我有這樣的:IOS拖放與縮放

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [self.superview bringSubviewToFront:self]; 
    self.transform = CGAffineTransformMakeScale(0.3f, 0.3f); 
    startLocation = ([[touches anyObject] locationInView:self]); 
    startLocation = CGPointApplyAffineTransform(startLocation, self.transform); 
} 

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    CGPoint pt = [[touches anyObject] locationInView:self]; 
    float dx = pt.x - startLocation.x; 
    float dy = pt.y - startLocation.y; 
    CGPoint newCenter = CGPointApplyAffineTransform (CGPointMake(self.center.x + dx, self.center.y + dy), self.transform); 
    self.center = newCenter; 
} 

這是一個開始,因爲它縮放拖動的UIView,我想它,並讓我將它拖到;但是,拖動的UIView不會直接用鼠標指針移動(我在模擬器上運行)。當圖像靠近模擬器屏幕的左上角時,鼠標指針&拖動視圖在一起;但是當我離開屏幕的右上角時,視圖不再直接用鼠標指針移動;鼠標指針似乎以大約2:1的比例移動到拖動的UIView的移動。

第二個問題是,當拖動結束時,如果該項目沒有被刪除,我需要將UIView返回到它的原始比例,然後再將其重新附加到它的超級視圖,並且還沒有完全想到如何做到這一點。

感謝任何幫助,包括有關更好方法的建議,如果我完全偏離此處。 (我知道還有其他的東西需要在隔離放置目標和放棄時完成,但我想我知道那裏需要做什麼)。

感謝您的任何指導。

正則表達式

回答

0

似乎touchesmoved仍在使用舊的大小。作爲一個建議,我將首先開始調整大小,然後實施拖放操作。

當拖放工作正在執行調整大小。首先開始拖動然後拖放。這樣做可以讓你更好地感受你想要達到的目標。

關於2:1的比例,我有一種感覺,它是關於你沒有調整視圖總體的變換,但我可能是錯的。但請確保您使拖動視圖更小,中間指向拖動點。

一些參考資料,可以幫助有:

http://www.edumobile.org/iphone/iphone-programming-tutorials/simple-drag-and-drop-on-iphone/ http://bynomial.com/blog/?p=77

+0

好的,我明白了,我想我會發布代碼,以防萬一別人需要它。我在這裏發現了另一篇文章,其中有類似的問題: – RegularExpression 2013-02-20 17:50:06

2

好,我知道了答案&想我應該張貼在這裏的情況下,任何人都需要它。感謝以前的答覆。我碰到下面的文章:

How to move a UIImageView after applying CGAffineTransformRotate to it?

,這給我帶來了以下解決方案。在我的原始代碼中,我將變換應用到開始位置,但我沒有將它應用到觸摸點。所以,這裏是我結束了與解決的問題:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [self.superview bringSubviewToFront:self]; 
    self.transform = CGAffineTransformMakeScale(0.2f, 0.2f); 
    startLocation = ([[touches anyObject] locationInView:self]); 
    startLocation = CGPointApplyAffineTransform(startLocation, self.transform); 
} 

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    CGPoint pt = [[touches anyObject] locationInView:self]; 
    if (!CGAffineTransformIsIdentity(self.transform)) 
     pt = CGPointApplyAffineTransform(pt, self.transform); 
    float dx = pt.x - startLocation.x; 
    float dy = pt.y - startLocation.y; 
    CGPoint newCenter = CGPointMake(self.center.x + dx, self.center.y + dy); 
    self.center = newCenter; 
} 

因此,需要變換當且僅當變換已應用到UIView的被應用到觸摸點。我一度將轉換應用於接觸點,但沒有條件 - 在這種情況下,整個拖動操作的起點很差,因爲轉換已應用於觸點,可能在點之前已被移動。無論如何,上面的代碼似乎已經解決了這個問題。

+0

此外,新中心點不需要進行轉換。 – RegularExpression 2013-02-20 18:05:26