[英]Custom UIView doesn't get removed from superview when loaded again while animating
问题实际上解释了这一切。
我使用以下代码在当前视图之上加载了一个自定义UIView动画:
- (void)showView
{
self.blurView.alpha = 0.f;
[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^
{
self.blurView.alpha = 1.f;
} completion:^(BOOL finished) {
[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^
{
self.blurView.alpha = 0.f;
} completion:nil];
}];
}
它可以工作,但是当我仍在设置动画时再次执行-(void)showView 时 ,自定义视图不会从屏幕上删除。
它只是这样保留:
我发现了问题所在。
blurView
是一个公共变量,每次我执行-(void)showView
它都使用相同的变量,仅将alpha设置为0.f,因此永远不会更改第一个节目的alpha。
我在方法本身中声明了变量blurView
,现在它可以工作,因为每个blurView
都获得了自己的指针。
-(void)someMethod
{
//Create a live blur view
FXBlurView *blurView = [[FXBlurView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
blurView.center = CGPointMake(self.view.window.frame.size.width / 2, self.view.window.frame.size.height / 2);
blurView.layer.cornerRadius = 20.f;
[self.view.window addSubview:blurView];
[self showView:(blurView)];
}
- (void)showView:(FXBlurView *)blurView
{
//Make full transparent before showing
blurView.alpha = 0.f;
//Start animation
[FXBlurView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^
{
blurView.alpha = 1.f;
} completion:^(BOOL finished) {
if (finished)
{
//End animation
[FXBlurView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^
{
blurView.alpha = 0.f;
} completion:nil];
}
}];
}
您应该将此行添加到函数顶部:
[self.view.layer removeAllAnimations];
它将删除所有活动的动画。
它不起作用,因为您已实现了延迟,请使用具有延迟的动画块-它会创建可以通过上面的此功能删除的动画关键点。
[UIView animateWithDuration:0.3 delay:0.0
options:UIViewAnimationOptionAllowUserInteraction
animations:^
{
// ...
} completion:NULL];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.