Question

I'm having an annoying problem with my UIView animation. Basically I'm doing a UIView rotation animation by following the finger position. Everything is working fine when the view is fully loaded but if the user drags his finger while new data (view content) is incoming from my web service the view is jumping off the screen. When the users scrolls a bit the view jumps back into it's position and everything is fine. So what can I do to prevent this jumping.

I think the incoming data triggers something like layoutIfNeeded and this effects the animation position. Another idea is that it clashes with my autoLayout constraints.

The animation is done like this:

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

  UIView *currentView = self.viewToAnimate;

  CGPoint translation = [gesture translationInView:gesture.view.superview];
  CGFloat percent = translation.x / gesture.view.bounds.size.width;

  CATransform3D transform = CATransform3DIdentity;
  transform.m34 = 1.0 / -800;
  currentView.layer.transform =
      CATransform3DRotate(transform, M_PI * percent, 0.0, 1.0, 0.0);

      //Snap back if the users lifts his finger
  if (gesture.state == UIGestureRecognizerStateEnded ||
      gesture.state == UIGestureRecognizerStateCancelled ||
      gesture.state == UIGestureRecognizerStateFailed) {

    [UIView animateWithDuration:0.45
        delay:0.0
        options:UIViewAnimationOptionCurveEaseInOut
        animations:^{ currentView.transform = CGAffineTransformIdentity; }
        completion:nil];
  }
}
Was it helpful?

Solution 2

I managed to get it working with [self.viewToAnimate setTranslatesAutoresizingMaskIntoConstraints:YES];.

I think my constraints repositioned the view during the loading because the view was growing. After reducing the constraint priority in IB and setting translatesAutoresizingMaskIntoConstraints to YES everything is working as expected.

OTHER TIPS

There is way too little information on what and how you are doing to solve this directly. What type are these views of, are you generating views programatically...

Most likely what is going on is one of 2 scenarios.

First the view itself is calling self to refresh in which case it sets its transform ti identity. If this is the case all you need to do is add this view to another superview and apply transform to that superview rather then the view itself.

Second possibility is the superview is calling the view to reposition. In this case you might want to break such operations by possibly setting some parameters on one of the views for this to stop doing automatically or try to analyse where this call comes from.

To analyse what is calling this view to reposition I suggest you to subclass your view and if needed even the layer and override the methods such as setFrame, setTransform and add breakpoints there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top