【问题标题】:MBProgressHUD with CustomView: Why can't I animate my custom view?带有 CustomView 的 MBProgressHUD:为什么我不能为我的自定义视图设置动画?
【发布时间】:2020-01-06 11:22:01
【问题描述】:

这不知何故不起作用...为什么?如何在不从动画中制作 gif 的情况下实现旋转的自定义螺旋桨?

-(UIView *)propTest
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 37, 37)];

    UIImageView *movablePropeller = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 37 , 37)];

    movablePropeller.image = [UIImage imageNamed:@"MovablePropeller"];


    [view addSubview:movablePropeller];
    movablePropeller.center = view.center;

    CABasicAnimation *rotation;
    rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    rotation.fromValue = [NSNumber numberWithFloat:0.0f];
    rotation.toValue = [NSNumber numberWithFloat:(2 * M_PI)];
    rotation.cumulative = true;
    rotation.duration = 1.2f; // Speed
    rotation.repeatCount = INFINITY; // Repeat forever. Can be a finite number.

    [movablePropeller.layer addAnimation:rotation forKey:@"Spin"];

    return view;
}

-(void)presentMyHud
{
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:hud];
    hud.mode = MBProgressHUDModeCustomView;
    hud.customView = [self propTest];
    hud.detailsLabelText = @"Getting data";
    [hud show:YES];
}

但我的螺旋桨保持静止......

【问题讨论】:

    标签: objective-c uiview cabasicanimation mbprogresshud


    【解决方案1】:

    如果螺旋桨没有旋转,如果您没有立即将此 view 添加到视图层次结构中,可能会发生这种情况。通常,将视图添加到视图层次结构中是谨慎的addAnimation

    - (UIView *)addPropellerToView:(UIView *)view {
        UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 37, 37)];
    
        // make sure to add it to the view hierarchy
    
        [view addSubview:containerView];
    
        // if you want to center it, set its `center` to be in the middle of the `bounds` ... don't use `center`
    
        containerView.center = CGPointMake(view.bounds.origin.x + view.bounds.size.width / 2, view.bounds.origin.y + view.bounds.size.height / 2);
    
        // I'd just use the `bounds` of the container view; reducing the risk that we get its coordinates wrong
    
        UIImageView *movablePropeller = [[UIImageView alloc] initWithFrame:containerView.bounds];
    
        movablePropeller.image = [UIImage imageNamed:@"MovablePropeller"];
        movablePropeller.contentMode = UIViewContentModeScaleAspectFill;
    
        [containerView addSubview:movablePropeller];
    
        // never set the center of a view to the center of its super view ... those are two different coordinate systems.
        // in this case, this line isn't needed at all, so I'll remove it.
        //
        //     movablePropeller.center = containerView.center;
    
        CABasicAnimation *rotation;
        rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
        rotation.fromValue = @(0);
        rotation.toValue = @(2 * M_PI);
        rotation.cumulative = true;
        rotation.duration = 1.2f;
        rotation.repeatCount = INFINITY;
    
        [movablePropeller.layer addAnimation:rotation forKey:@"Spin"];
    
        return containerView;
    }
    

    产量:


    一些不相关的观察:

    • 如果要将视图 A 置于视图 B 的中间,请将视图 A 的 center 设置为 B 的 bounds 的中点,而不是 B 的 center。例如,你永远不想做:

      A.center = B.center;
      

      你想要的是:

      A.center = CGPointMake(B.bounds.origin.x + B.bounds.size.width / 2, B.bounds.origin.y + B.bounds.size.height / 2);
      

      我知道它看起来应该是同一件事,但事实并非如此。 A 的centerframe 一样是在B 的坐标系中定义的。但是B 的center 是在其父视图的坐标系中定义的,可能完全不同。有时你不会注意到差异(特别是如果 B 的 origin{0, 0}),但这表明对不同坐标系的误解,如果 B 不在 {0, 0},那么一切都会出错。

    • 您可以使用NSNumber 文字,将[NSNumber numberWithFloat:0.0f] 替换为@(0)

    • 你真的不需要那个容器视图,所以你可以简化例程,如下所示。

      - (UIView *)addPropellerTo:(UIView *)view {
          UIImageView *movablePropeller = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 37, 37)];
          movablePropeller.image = [UIImage imageNamed:@"MovablePropeller"];
          movablePropeller.contentMode = UIViewContentModeScaleAspectFill;
          [view addSubview:movablePropeller];
          movablePropeller.center = CGPointMake(view.bounds.origin.x + view.bounds.size.width / 2, view.bounds.origin.y + view.bounds.size.height / 2);
      
          CABasicAnimation *rotation;
          rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
          rotation.fromValue = @(0);
          rotation.toValue = @(2 * M_PI);
          rotation.cumulative = true;
          rotation.duration = 1.2f;
          rotation.repeatCount = INFINITY;
      
          [movablePropeller.layer addAnimation:rotation forKey:@"Spin"];
      
          return movablePropeller;
      }
      

    【讨论】:

      猜你喜欢
      • 2019-07-02
      • 2011-02-11
      • 2015-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-29
      相关资源
      最近更新 更多