带有CustomView的MBProgressHUD:为什么不能设置自定义视图的动画?

问题描述 投票:0回答:1

这不起作用...为什么?如何在不制作动画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个回答
0
投票

如果螺旋桨未旋转,则可能是由于您没有立即将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);

    我知道看起来应该是同一回事,但事实并非如此。在B的坐标系中定义了A的center,与frame类似。但是在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; }

© www.soinside.com 2019 - 2024. All rights reserved.