ios - UIView 中的动画遵循 UILabel

标签 ios objective-c

我有处理同一张图片的动画

enter image description here

我在 UIView 上创建了一个 UILabel。我为 UILabel 制作了从右向左移动的动画。

我想要同样的:

enter image description here

这是我的代码:

[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;
          }];

现在,我希望当 UILabel 的框架移动到末尾时是 UIView 的宽度:_viewAnimation 跟随 UILabel。

请帮帮我。

这是我的代码

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

请再次检查帮助我。

最佳答案

您不应该将 lbAnimation 放在 viewAnimation 中。并且需要一些计算动画时间的持续时间。

- (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)
        {

        }];

     }];
}

可以查看my demo .

关于ios - UIView 中的动画遵循 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47707301/

相关文章:

iphone - UILabel 未更新与 IBOutlet 连接

iphone - 从右/左动画 presentModalViewController

ios - 从后台线程调用时在 iPad 上延迟

objective-c - NSTimer with block——我做的对吗?

objective-c - 以编程方式手动链接 XIB 中声明的 UIButton 和 VIewController 中的属性

ios - UITableViewCell - 发送到实例的无法识别的选择器

ios - 如何在用户登录后自动更新 ViewController

iphone - AVURLAsset 视频缓冲

ios - 使用 iPhone 检测频率值

ios - 如何在关闭 UISearchDisplayController 时将文本保留在搜索栏中