2014-04-01 62 views
-1

我有一張圖片可以在屏幕上拖動,但無論我觸摸屏幕的哪個位置,圖片都會出現在我的手指上,並開始拖動,並觀看我觀看的所有視頻以及所有博客和文章所有人都展示的是如何做到這一點...它不是我想要的..我想要的是開始拖動圖像只有當我觸摸圖像..繼承人我的代碼只有當圖片被觸摸時纔會拖動圖片

- (void)touchesMoved :(NSSet中*)觸摸withEvent:方法(的UIEvent *)事件

{

UITouch *Drag = [ [ event allTouches ] anyObject ]; 
Dot.center = [ Drag locationInView: self.view ]; 

[self checkCollison]; 

}

謝謝

+0

代替此使用UIPangesture –

回答

0

在您的.h

IBOutlet UIImageView * YOURIMAGE; 

在您的m

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [[event allTouches] anyObject]; 
    if ([touch view] == YOURIMAGE) { 
     YOURIMAGE.center = [touch locationInView:self.view]; 
    } 
} 
+0

的我喜歡它是多麼的簡單,但它不是工作..如果我把你的代碼在點不動... – user3481455

+0

試試這個http://stackoverflow.com/a/14858148/3078845 – user3078845

0

而不是使用-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event方法使用UIPanGestureRecognizer

創建並添加一個平移手勢您UIImageView像:

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleDrag:)]; 
[yourImageView addGestureRecognizer:panGesture]; 

並寫出選擇方法,如:

- (void)handleDrag:(UIPanGestureRecognizer*)recognizer 
{ 

    CGPoint translation = [recognizer translationInView:recognizer.view]; 
    CGPoint velocity = [recognizer velocityInView:recognizer.view]; 
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, 
            recognizer.view.center.y + translation.y); 

    if (recognizer.state == UIGestureRecognizerStateBegan) 
    { 
     // start tracking movement 
    } 
    else if (recognizer.state == UIGestureRecognizerStateEnded) 
    { 
     // You can change position here, with animation 
    } 
}