我正在尝试围绕锚点旋转UIImageView以模拟“雷达扫描”样式动画。我对锚点的理解使我相信(1,1)应该是radar image的右下角。但是,将ImageView的锚点设置为此并不能提供我预期的行为。这可以查看here。
可以看出,锚点是图像右侧的一个方向,尽管它正好位于中心。我想要的是围绕中心点旋转的图像,就像雷达扫描一样。
以下是相关代码:
self.radarView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, barDimension, barDimension)];
self.radarView.contentMode = UIViewContentModeScaleAspectFill;
self.radarView.image = [UIImage imageNamed:@"radarSweep.png"];
self.radarView.layer.anchorPoint = CGPointMake(1, 1);
self.radarView.layer.position = CGPointMake(screenWidth / 2.0, screenHeight / 2.0);
[self.mapView addSubview:self.radarView];
CABasicAnimation *opacityInAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
[opacityInAnim setFromValue:[NSNumber numberWithFloat:0.0]];
[opacityInAnim setToValue:[NSNumber numberWithFloat:1.0]];
opacityInAnim.duration = 0.25;
opacityInAnim.fillMode = kCAFillModeForwards;
opacityInAnim.removedOnCompletion = NO;
opacityInAnim.beginTime = 0.0;
opacityInAnim.delegate = self;
[opacityInAnim setValue:@"radarSweep" forKey:@"animName"];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.fromValue = [NSNumber numberWithFloat:0.0];
animation.toValue = [NSNumber numberWithFloat:2 * M_PI];
animation.duration = 1.0f;
animation.fillMode = kCAFillModeForwards;
animation.repeatCount = 2;
animation.removedOnCompletion = NO;
[animation setBeginTime:0.0];
animation.delegate = self;
[animation setValue:@"radarSweep" forKey:@"animName"];
animation.removedOnCompletion = YES;
CABasicAnimation *opacityOutAnimBar = [CABasicAnimation animationWithKeyPath:@"opacity"];
[opacityOutAnimBar setFromValue:[NSNumber numberWithFloat:1.0f]];
[opacityOutAnimBar setToValue:[NSNumber numberWithFloat:0.0f]];
opacityOutAnimBar.delegate = self;
opacityOutAnimBar.fillMode = kCAFillModeForwards;
opacityOutAnimBar.removedOnCompletion = NO;
[opacityOutAnimBar setDuration:0.25];
[opacityOutAnimBar setValue:@"radarSweep" forKey:@"animName"];
[opacityOutAnimBar setBeginTime:1.35];
CAAnimationGroup *group = [CAAnimationGroup animation];
[group setDuration:2.0];
group.delegate = self;
group.fillMode = kCAFillModeForwards;
group.removedOnCompletion = NO;
[group setAnimations:@[opacityInAnim, animation, opacityOutAnimBar]];
[group setValue:@"radarSweep" forKey:@"animName"];
[self.radarView.layer addAnimation:group forKey:@"group"];
任何建议都将不胜感激。提前谢谢。
答案 0 :(得分:0)
您的雷达图像不是正方形,而是图像视图,因此图像在图像视图的范围内流血。您可以self.radarView.layer.clipsToBounds = YES
或将内容模式更改为ScaleToFit。或者,您可以调整图像视图的大小以匹配雷达图像的比例。