2013-01-19 56 views
3

林運動圖像圍繞iPhone屏幕。有些圖像很小,當您將它們移動到手指上時,會遮擋您對圖像本身的看法。我想在移動圖像的同時設置圖像的中心,以便圖像中心實際上是觸摸位置前的10個點,而不是將其設置到觸摸位置。移動圖像使用UIPanGesture偏離中心在IOS

我測試了以下,但很快意識到這反覆減去10從Y使圖像得到越走越從觸摸位置離開,並最終關閉屏幕,而不是保持恆定的10點偏移。

我應該怎樣做呢?

- (void) TestGestureMethod:(UIPanGestureRecognizer *) panGesture { 
    CGPoint translation = [panGesture translationInView:self.view]; 
    switch (panGesture.state) { 
    case UIGestureRecognizerStateBegan: 
     [self.view bringSubviewToFront:testObject]; 
     break; 
    case UIGestureRecognizerStateChanged: 
     testObject.center = CGPointMake(testObject.center.x + translation.x, 
             testObject.center.y + translation.y); 
     testObject.center = CGPointMake(testObject.center.x, testObject.center.y - 10); 
     break; 
    case UIGestureRecognizerStateEnded: 
     break; 
    } 
    [panGesture setTranslation:CGPointZero inView:self.view]; 
} 

回答

4

既然你不翻譯的形象,但手動設置它的中心,你有沒有考慮使用UIGestureRecognizer locationInView:不是translationInView:?

你可以做這樣的事情......

- (void)TestGestureMethod:(UIPanGestureRecognizer *)panGesture 
{ 
    CGPoint location = [panGesture locationInView:self.view]; 

    switch (panGesture.state) { 
     ... 
     case UIGestureRecognizerStateChanged: 
      testObject.center = CGPointMake(location.x, location.y - 10); 
     break; 
     ... 
    } 
} 

這應當引起圖像中心永遠是在觸摸下方僅有10分。

+0

完美,非常感謝! – DelightedD0D

+0

也適合我! –

+0

是有辦法動畫突如其來的舉動第一拖曳當對象進行?也許調整它,或者增加一個彈簧動作? –

0

試試這個:

- (void) TestGestureMethod:(UIPanGestureRecognizer *) panGesture { 
    CGPoint translation = [panGesture translationInView:self.view]; 
    switch (panGesture.state) { 
    case UIGestureRecognizerStateBegan: 
     [self.view bringSubviewToFront:testObject]; 
     testObject.center = CGPointMake(testObject.center.x, testObject.center.y - 10); 
     break; 
    case UIGestureRecognizerStateChanged: 
     testObject.center = CGPointMake(testObject.center.x + translation.x, 
             testObject.center.y + translation.y); 

     break; 
    case UIGestureRecognizerStateEnded: 
     break; 
    } 
    [panGesture setTranslation:CGPointZero inView:self.view]; 
} 
0

試試這個,這能解決問題烏爾:

- (void)handlePanGesture:(UIPanGestureRecognizer *)gestureRecognizer 
{ 
    UIView *piece = [gestureRecognizer view]; 

    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) { 
     CGPoint translation = [gestureRecognizer translationInView:[piece superview]]; 

     [piece setCenter:CGPointMake([piece center].x + translation.x, [piece center].y + translation.y)]; 
     [gestureRecognizer setTranslation:CGPointZero inView:[piece superview]]; 
    } 
} 

我希望這將幫助你。