UIView touchesbegan doesn't respond during animation

前端 未结 1 1111
青春惊慌失措
青春惊慌失措 2020-12-18 00:53

I have a draggable class that inherits UIImageView. The drag works fine when the view is not animating. But when animating it won\'t respond to touches. Once the animation i

相关标签:
1条回答
  • 2020-12-18 01:34

    That's because ios places your animating view to the target position, when the animation starts, but draws it on the path. So if you tap the view while moving, you actually tap somewhere out of its frame.

    In your animating view's init, set userInteractionEnabled to NO. So the touch events are handled by the superview.

    self.userInteractionEnabled = NO;
    

    In your superview's touchesBegan method, check your animating view's presentationLayer position. If they match with the touch position, redirect the touchesBegan message to that view.

    - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        CGPoint point = [[touches anyObject] locationInView:self.view];
        CGPoint presentationPosition = [[animatingView.layer presentationLayer] position];
    
        if (point.x > presentationPosition.x - 10 && point.x < presentationPosition.x + 10
            && point.y > presentationPosition.y - 10 && point.y < presentationPosition.y + 10) {
            [animatingView touchesBegan:touches withEvent:event];
        }
    }
    
    0 讨论(0)
提交回复
热议问题