2013-10-18 61 views
0

這是我的隨機移動圖像和觸摸代碼的代碼。如何在移動圖像時保持並拖動圖像?

-(void)viewDidAppear:(BOOL)animated { 

timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(randomizeXandY) userInfo:nil repeats:YES]; 
} 

-(void)randomizeXandY { 

CGFloat x = (CGFloat) (arc4random() % (int) self.view.bounds.size.width); 
CGFloat y = (CGFloat) (arc4random() % (int) self.view.bounds.size.height); 

[self moveObjectsAtX:x Y:y]; 

} 

-(void)moveObjectsAtX:(CGFloat)x Y:(CGFloat)y { 

[UIView animateWithDuration:5 animations:^{ 
    imgView.center = CGPointMake(x, y); 
}]; 

} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

UITouch *touch = [[event allTouches]anyObject]; 
CGPoint touchLocation = [touch locationInView:self.view]; 

    if ([touch view] == imgView) { 

     imgView.center = touchLocation; 
    } 

} 

回答

1

嘗試在touchesBegin代碼和touchesEnded

-(void)touchesBegin:(NSSet *)touches withEvent:(UIEvent *)event { 

// stop timers here 

} 

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

// start timers here again 

} 
+0

我將如何阻止它?我試圖在觸摸開始時使計時器無效,但它仍然無效 – user2838188

+0

使用標誌來檢查是否可以在觸摸處理代碼中移動圖像視圖和更改標誌值。 –

+0

嘗試使計時器無效或使其無效..它應該工作 –

0

你必須使用以下方法:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint touchLocation = [touch locationInView:self.view]; 

    //... 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint touchLocation = [touch locationInView:self.view]; 
    //.. 

} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint touchLocation = [touch locationInView:self.view]; 

    //... 
} 
中的touchesBegan方法

,檢查觸摸是你的形象:

if (CGRectContainsPoint(yourImage.frame, touchLocation)){ 
    dragging = YES; 
} 

如果是的話設置一個全局變量,例如bool拖動到YES。

在touchesMoved:

檢查

if (dragging){ 
    yourImage.frame = CGRectMake(touchLocation.x, touchLocation.y,yourImage.frame.size.widht,yourImage.frame.size.height); 
} 
在touchesEnded

: 組拖到NO

拖動= NO;

所以:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint touchLocation = [touch locationInView:self.view]; 

    if (CGRectContainsPoint(yourImage.frame, touchLocation)){ 
     dragging = YES; 
     [timer invalidate]; 
     timer = nil; 
    } 
    //... 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint touchLocation = [touch locationInView:self.view]; 

    if (dragging){ 
     yourImage.frame = CGRectMake(touchLocation.x, touchLocation.y,yourImage.frame.size.widht,yourImage.frame.size.height); 
    } 
    //.. 

} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint touchLocation = [touch locationInView:self.view]; 

    dragging = NO; 
    //... 
} 
+0

仍然不適合我。這裏似乎是什麼問題? – user2838188

0

我寧願建議使用UIPanGestureRecognizer。在它的處理方法中,您可以檢查觸摸的可能狀態。

-(void)handlePan:(UIPanGestureRecognizer *)recognizer { 
     switch (recognizer.state) { 
      case UIGestureRecognizerStateBegan: { 
       CGPoint touchLocation = [recognizer locationInView:self.view]; 

       ... 

       break; 
      } 

      case UIGestureRecognizerStateChanged: 

       ... 

       break; 

      case UIGestureRecognizerStateEnded: 

       ... 

       break; 

      default: 
       break; 
} 
+0

你能給我那個確切的代碼嗎? – user2838188

+0

也許這[教程](http://www.raywenderlich.com/6567/)可以提供幫助。 –