1

我在使用 UIPanGestureRecognizer 从屏幕顶部移动视图时遇到了一个奇怪的问题。一切都在模拟器中完美运行,但在实际设备上,当我触摸它时,视图消失了。

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

    CGPoint start;

    if (recognizer.state == UIGestureRecognizerStateBegan) {
        start = [recognizer translationInView:self.view];
        originCenter = recognizer.view.center.y;
    }

    if (recognizer.state == UIGestureRecognizerStateChanged) {
        CGPoint translation = [recognizer translationInView:self.view];
        if (down) {
            if (originCenter + translation.y < self.view.center.y && self.view.center.y + translation.y < self.view.center.y) {
                CGPoint move = CGPointMake(self.view.frame.size.width/2, originCenter-(start.y-translation.y));
                [recognizer.view setCenter:move];
            }
        }else{
            if (originCenter + translation.y < self.view.center.y && originCenter + translation.y > originCenter) {
                CGPoint move = CGPointMake(self.view.frame.size.width/2, originCenter-(start.y-translation.y));
                [recognizer.view setCenter:move];
            }
        }
    }

    if([recognizer state] == UIGestureRecognizerStateEnded  || [recognizer state] == UIGestureRecognizerStateCancelled ){
        if(recognizer.view.center.y > -40){

            [recognizer.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

            down = TRUE;
        }else{

            [recognizer.view setFrame:CGRectMake(0, 80-self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)];

            down = FALSE;
        }
    }
}

我想到一件事,计算设备上的位置可能存在问题,并且视图移动到屏幕外。那可能吗?

4

1 回答 1

1

如果没有别的,你应该做start一个static或一个ivar。否则,当您点击 时UIGestureRecognizerStateChangedstart将不会保留它在 期间的值UIGestureRecognizerStateBegan

于 2013-08-02T19:55:44.457 回答