This somehow doesn't work...why? How can I achieve a spinning custom propeller without making a gif out of the animation?
-(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];
}
But my propeller stays static...
If the propeller is not spinning, that can happen if you didn’t immediately add this
viewto the view hierarchy. Generally, it’s prudent to add the view to the view hierarchy before youaddAnimation.Yielding:
Some unrelated observations:
If you want to center view A in the middle of view B, set view A’s
centerto coordinates to the midpoint of B’sbounds, not to B’scenter. E.g., you never want to do:What you want is:
I know it looks like it should be the same thing, but it’s not. A’s
centeris defined, likeframe, in the coordinate system of B. But B’scenteris defined in the coordinate system of its superview, which might be completely different. Sometimes you won’t notice the difference (specifically if B’soriginis{0, 0}), but it suggests a misunderstanding of the different coordinate systems, and if B isn’t at{0, 0}, then everything will be wrong.You can use
NSNumberliterals, replacing[NSNumber numberWithFloat:0.0f]with@(0).You really don’t need that container view, so you could simplify the routine, like below.