Objective c MBProgressHUD与CustomView:为什么可以';我不能设置自定义视图的动画吗?
这不知怎的不起作用…为什么?如何在不制作gif动画的情况下实现旋转自定义螺旋桨Objective c MBProgressHUD与CustomView:为什么可以';我不能设置自定义视图的动画吗?,objective-c,uiview,cabasicanimation,mbprogresshud,Objective C,Uiview,Cabasicanimation,Mbprogresshud,这不知怎的不起作用…为什么?如何在不制作gif动画的情况下实现旋转自定义螺旋桨 -(UIView *)propTest { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 37, 37)]; UIImageView *movablePropeller = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 37 , 37)]; movable
-(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];
}
但是我的推进器保持静止…如果推进器没有旋转,如果您没有立即将此
视图添加到视图层次结构中,则可能会发生这种情况。通常,在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;
}
屈服:
一些不相关的观察结果:
>P>如果您想在视图B中集中查看A,请将视图A的<代码>中心>代码>坐标到B的“代码>界线< /代码>的中点,而不是B的<代码>中心< /代码>。例如,你永远不想做:
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的中心
是在B的坐标系中定义的,就像框架
一样。但B的中心
是在其superview的坐标系中定义的,这可能是完全不同的。有时你不会注意到差异(特别是如果B的原点是{0,0}
),但这表明对不同坐标系的误解,如果B不在{0,0}
,那么一切都会出错
- 您可以使用
NSNumber
文字,将[NSNumber numberwhithfloat: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;
}