0

I have work with animation same image

enter image description here

I have make a UILabel on UIView. And I make animation for UILabel move from right to left.

I want same here:

enter image description here

This is my code:

[UIView animateKeyframesWithDuration:6 delay:0 options:UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseOut animations:^{ _lbAnimation.frame=CGRectMake(0-(rect.size.width), _lbAnimation.frame.origin.y, rect.size.width, _lbAnimation.frame.size.height); } completion:^(BOOL finished) { _viewAnimation.hidden = YES; }]; 

And now, I want while frame of UILabel move to end is width of UIView: _viewAnimation follow UILabel.

Please help me.

This is my code

https://github.com/nhhthuanck/Alert-Animation

Please check again help me.

8
  • I didn't get your last comment Commented Dec 8, 2017 at 3:49
  • I want while frame of UILabel move to end is width of UIView: _viewAnimation follow UILabel. Commented Dec 8, 2017 at 3:51
  • So you want to move view also? Commented Dec 8, 2017 at 3:51
  • I want resize UIView Commented Dec 8, 2017 at 3:52
  • So you can use transform property to resize view. Commented Dec 8, 2017 at 3:53

1 Answer 1

3

You shouldn't put lbAnimation inside viewAnimation. And need some calculate duration of animation time.

- (IBAction)startAction:(id)sender { NSString * htmlString = @"<html>" " <head>" " <style type='text/css'>" " body { font: 16pt 'Gill Sans'; color: #1a004b; }" " i { color: #822; }" " </style>" " </head>" " <body>Here is some <i>formatting!</i> <a href='#'><font color='green'>Google</font></a><font color='red'> example text</font><a href='#'>YouTube</a><b> bold</b> and <i>italic</i></body>" "</html>"; NSError *err = nil; NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData: [htmlString dataUsingEncoding:NSUTF8StringEncoding] options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes: nil error: &err]; _viewAnimation.frame = CGRectMake(self.view.frame.size.width, _viewAnimation.frame.origin.y, _viewAnimation.frame.size.width, _viewAnimation.frame.size.height); _viewAnimation.hidden = NO; _lbAnimation.hidden = YES; CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(FLT_MAX, _lbAnimation.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin context:nil]; [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{ _viewAnimation.frame=CGRectMake(0, _viewAnimation.frame.origin.y, _viewAnimation.frame.size.width, _viewAnimation.frame.size.height); } completion:^(BOOL finished) { _lbAnimation.hidden = NO; _lbAnimation.attributedText = attrStr; _lbAnimation.frame = CGRectMake(self.view.frame.size.width, _lbAnimation.frame.origin.y, rect.size.width, _viewAnimation.frame.size.height); CGFloat duration = 6; CGFloat ratio = _viewAnimation.frame.size.width / (rect.size.width + _viewAnimation.frame.size.width); [UIView animateWithDuration:duration * ratio delay:duration * (1-ratio) + 0.1f options:UIViewAnimationOptionCurveLinear animations:^{ _viewAnimation.frame = CGRectMake(_viewAnimation.frame.origin.x, _viewAnimation.frame.origin.y, 0, _viewAnimation.frame.size.height); } completion:^(BOOL finished) { _viewAnimation.hidden = YES; }]; [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveLinear animations:^{ _lbAnimation.frame=CGRectMake(-rect.size.width, _lbAnimation.frame.origin.y, rect.size.width, _lbAnimation.frame.size.height); } completion:^(BOOL finished) { }]; }]; } 

You can check my demo.

Sign up to request clarification or add additional context in comments.

7 Comments

Now it's easier to help you. Wait a moment.
Can i use constraints instead of frame to resolve your problem?
Yes, Please make all you can.
@EasySoft i have updated my answer, you can replace your startAction with my method.
Thank you for your help. I replaced my method by your method, but it not run. How about for your source?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.