Easy Soft
Easy Soft

Reputation: 110

Animation in UIView follow UILabel

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.

Upvotes: 0

Views: 117

Answers (1)

trungduc
trungduc

Reputation: 12154

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.

Upvotes: 3

Related Questions