Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView transitionWithView animating property change: slide in from right?

I have a readonly UITextView, and I'm updating the text. When the new text appears, I want it to animate as follows: New text slides in from the right, as the old text slides offscreen toward the left side.

Is it possible to do this with transitionWithView? If not, what's the best way to do it? I can make it do a CrossDissolve, but that's not the animation I'm looking for:

    [UIView transitionWithView:self.storyTextView
                  duration:0.5
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:^{
                    [self.storyTextView setText:withStory.storyText];
                }
                completion:nil];

The reason for insisting on the right to left animation is because eventually I want the user to be able to trigger it by swiping toward the left on the UITextView.

like image 518
stone Avatar asked May 18 '14 01:05

stone


People also ask

Does UIView animate need weak self?

No, it is not needed in this case. animations and completion are not retained by self so there is no risk of strong retain cycle.

Does UIView animate run on the main thread?

The contents of your block are performed on the main thread regardless of where you call [UIView animateWithDuration:animations:] .

How do you animate a view in Swift?

To be exact, whenever you want to animate the view, you actually call layoutIfNeeded on the superview of that view. Try this instead: UIView. animate(withDuration: 0.1, delay: 0.1, options: UIViewAnimationOptions.

What is transition in Swift?

Transitions are predefined animations you can apply to views.


1 Answers

CATransition will allow you to do this, with a transition type of 'push' and a subtype of 'from right'. It's very straightforward to use:

CATransition *transition = [CATransition new];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;

// Make any view changes
self.textView.text = newText;

// Add the transition
[self.textView.layer addAnimation:transition forKey:@"transition"];

Note that this, while it's what you've asked for, won't fit nicely with a gesture without some extra tinkering - to tie it to a gesture you'd need to do something like set the layer speed to 0 and the manually update the progress of the animation to match your gesture.

like image 133
jrturton Avatar answered Oct 19 '22 15:10

jrturton