简体   繁体   English

UIPanGestureRecognizer-拖动后视图消失

[英]UIPanGestureRecognizer - view dissappears on drag

I have a strange problem with using UIPanGestureRecognizer to move a view from the top of the screen. 使用UIPanGestureRecognizer从屏幕顶部移动视图时遇到一个奇怪的问题。 Everything works perfect in simulator but on the actual device the view dissappears when I touch it. 一切都可以在模拟器中完美运行,但是当我触摸它时,在实际设备上的视图便消失了。

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

There is one thing that comes to my mind that there might be problem with calculating the position on the device and the view is moved outside the screen. 我想到的一件事是,计算设备上的位置可能会出现问题,并且视图会移动到屏幕之外。 Is that possible? 那可能吗?

If nothing else, you should make start a static or an ivar. 如果没有其他要求,则应将start设为static或ivar。 Otherwise, when you hit UIGestureRecognizerStateChanged , start will not have preserved the value it had during UIGestureRecognizerStateBegan . 否则,当您按下UIGestureRecognizerStateChangedstart将不会保留其在UIGestureRecognizerStateBegan期间的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 自定义视图上的UIPanGestureRecognizer从未调用 - UIPanGestureRecognizer on a Custom View never called 如何在父视图中限制UIPanGestureRecognizer - how to limit UIPanGestureRecognizer with in parent view 将UIPanGestureRecognizer添加到视图后,将其删除 - Remove a UIPanGestureRecognizer after add it to view 使用UIPanGestureRecognizer滑动到新的视图控制器 - Using UIPanGestureRecognizer to slide to a new view controller 使用UIPanGestureRecognizer滑动视图时如何限制区域? - How to limit area when sliding a view with UIPanGestureRecognizer? 如何让UISwipeGestureRecognizer和UIPanGestureRecognizer在同一视图上工作 - How to have a UISwipeGestureRecognizer AND UIPanGestureRecognizer work on the same view UIPanGestureRecognizer可以很好地移动视图,但有时会跳动 - moving view well by UIPanGestureRecognizer but sometimes goes jumpy 绑定到添加到UIPanGestureRecognizer的视图的运动,在其父视图中 - Bound the movement of a view which is added to UIPanGestureRecognizer, within its superview 将UIPanGestureRecognizer添加到新创建的UIView中,并用幻灯片替换当前视图 - Adding UIPanGestureRecognizer to newly created UIView, and replace current view with slide 使用UIPanGestureRecognizer在视图周围移动子图层是不错的选择? - Is it good choice to move a Sublayer around a view using UIPanGestureRecognizer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM