2012-11-13 23 views
0

我有這樣的代碼來拖動ImageView的:IOS:拖動ImageView的不是從中心

在touchbegan

CGPoint point = [touch locationInView:self]; 
[imageView setCenter:point]; 
在touchmoved

CGPoint point = [touch locationInView:self]; 
[imageView setCenter:point]; 

這個代碼允許有ImageView的過在我的手指下移動並形成其中心。 但我想要的東西,不僅可以從imageview的中心,而且也可以從imageview的另一個點拖動imageview,並且不拖動它,如果我觸摸出這個imageView ...你能幫助我嗎?

+0

http://stackoverflow.com/questions/4982277/uiview-drag-image-and-text/8332581#8332581 –

回答

0

您不需要將中心點設置爲觸摸點,而需要計算觸摸的移動方式。

將觸摸點存儲在變量中。

在的touchesBegan ...

storedPoint = [touchLocationInView:self]; 
在touchesMoved

則...

CGPoint currentPoint = [touch locationInView:self]; 

CGPoint vector = CGPointMake(currentPoint.x - storedPoint.x, currentPoint.y storedPoint.y); 

[imageView setCenter:CGPointMake(imageView.center.x + vector.x, imageView.center.y + vecotr.y)]; 

storedPoint = currentPoint; 

像這樣的事情呢。

要忽略觸摸,你可以在說的touchesBegan ...

if (!CGRectContainsPoint(imageView.frame, [touch locationInView:self]) 
{ 
    //ignore touch 
    trackTouches = NO; 
} 

trackTouches = YES; 

(trackTouches是一類變種)

然後在touchesMoved只能做的東西,如果trackTouches == YES。

0

您需要: A)測試觸摸是否在圖像中B)計算觸摸和imageView中心之間的差異。一旦你知道了x/y的差異,你可以做一些簡單的數學運算,將中心移動到用戶觸摸的imageView上。

這個網站上有很多關於A的例子,B應該是直截了當的。