3

我不确定为什么当我移动视图时这不会平移?

CGPoint translation = [recognizer translationInView:self.view];


    if ((recognizer.view.frameY + translation.y >= self.view.frameHeight - recognizer.view.frameHeight)) {
        CGPoint newOrigin = CGPointMake(self.view.bounds.size.width, roundf(recognizer.view.frameY + translation.y));
        recognizer.view.frameOrigin = newOrigin;
        NSLog(@"FRAME ORIGIN %@", NSStringFromCGPoint(recognizer.view.frameOrigin));
    } 
4

3 回答 3

6

我不清楚你的一些自定义变量等,所以我没有调试你的代码,我想我会告诉你我如何使用UIPanGestureRecognizer.

因此,假设 (a) 手势已添加到被拖动的视图中;(b) 该视图包含在您想要约束视图移动的超级视图中,代码可能如下所示:

- (void)handlePan:(UIPanGestureRecognizer *)gesture
{
    static CGRect originalFrame;

    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        originalFrame = gesture.view.frame;
    }
    else if (gesture.state == UIGestureRecognizerStateChanged)
    {
        CGPoint translate = [gesture translationInView:gesture.view.superview];
        CGRect newFrame = CGRectMake(originalFrame.origin.x + translate.x,
                                     originalFrame.origin.y + translate.y,
                                     originalFrame.size.width,
                                     originalFrame.size.height);

        if (CGRectContainsRect(gesture.view.superview.bounds, newFrame))
            gesture.view.frame = newFrame;
    }
}

虽然上述内容具有一定的简单性,但我认为我们可以改善用户体验。值得注意的是,如果用户将视图拖到它的超级视图之外,上面的代码将不会做任何事情(例如,拖过左边框并冻结在那里,即使您上下移动手指;不是很优雅)。我认为以下是一个更优雅的用户界面(但阅读代码有点复杂),当用户拖动手指时,我们会将对象拖动到超级视图中与用户的手指是。

- (void)handlePan:(UIPanGestureRecognizer *)gesture
{
    static CGRect originalFrame;

    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        originalFrame = gesture.view.frame;
    }
    else if (gesture.state == UIGestureRecognizerStateChanged)
    {
        CGPoint translate = [gesture translationInView:gesture.view.superview];
        CGRect newFrame = CGRectMake(fmin(gesture.view.superview.frame.size.width - originalFrame.size.width, fmax(originalFrame.origin.x + translate.x, 0.0)),
                                     fmin(gesture.view.superview.frame.size.height - originalFrame.size.height, fmax(originalFrame.origin.y + translate.y, 0.0)),
                                     originalFrame.size.width,
                                     originalFrame.size.height);

        gesture.view.frame = newFrame;
    }
}
于 2012-12-23T18:36:36.673 回答
1

是否调用了平移手势方法?如果未设置启用用户交互。你可以使用下面的代码。

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


CGPoint translation = [recognizer translationInView:recognizer.view.superview];

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

if (recognizer.state == UIGestureRecognizerStateEnded) {

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

    float slideFactor = 0.1 * slideMult; // Increase for more of a slide
    CGPoint finalPoint = CGPointMake(recognizer.view.center.x + (velocity.x * slideFactor), 
                                     recognizer.view.center.y + (velocity.y * slideFactor));
    finalPoint.x = MIN(MAX(finalPoint.x, 0), recognizer.view.superview.bounds.size.width);
    finalPoint.y = MIN(MAX(finalPoint.y, 0), recognizer.view.superview.bounds.size.height);

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

} 



}
于 2012-12-21T05:19:55.203 回答
0

if你应该写<=而不是>=,因为原点在左上角

于 2012-12-20T21:32:54.807 回答