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;
  }

}
4

3 回答 3

1

尝试 touchesBegin 和 touchesEnded 中的代码

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

 // stop timers here

}   

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

 // start timers here again

}   
于 2013-10-18T08:58:34.780 回答
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

拖动=否;

所以 :

- (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;
    //...
}
于 2013-10-18T09:04:06.733 回答
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;
}
于 2013-10-18T09:13:35.713 回答