0

我遵循了有关使用手势识别器在屏幕上拖动多个图像的指南,但是我遇到了问题。我希望将图像拖到可能是 iPad 或 iPhone 的屏幕范围内。我正在使用的代码估计图像的中心,因此当到达边界时,一半的图像会离开屏幕。你能帮我解决这个问题吗?

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {

CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, 
                                     recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

if (recognizer.state == UIGestureRecognizerStateEnded) {

    CGPoint velocity = [recognizer velocityInView:self.view];
    CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
    CGFloat slideMult = magnitude / 200; //original divider 200
    NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult);

    float slideFactor = 0.1 * slideMult /4; // Increase for more of a slide (original doesn't have a divider)
    CGPoint finalPoint = CGPointMake(recognizer.view.center.x + (velocity.x * slideFactor), 
                                     recognizer.view.center.y + (velocity.y * slideFactor));
    finalPoint.x = MIN(MAX(finalPoint.x, 0), self.view.bounds.size.width);
    finalPoint.y = MIN(MAX(finalPoint.y, 0), self.view.bounds.size.height);

    [UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        recognizer.view.center = finalPoint;
    } completion:nil];
  }
}
4

1 回答 1

0

好吧,我设法找到了解决我的问题的方法。我将图像的宽度和高度除以二。在我的情况下,结果是 100 和 70。其余的可以在代码框中看到:

finalPoint.x = MIN(MAX(finalPoint.x, 70), self.view.bounds.size.width-70);
finalPoint.y = MIN(MAX(finalPoint.y, 100), self.view.bounds.size.height-100);
于 2012-05-16T09:27:44.680 回答