为什么动画后用户界面会被阻塞?
我对简单的动画有疑问。我有一个 UIImageView 和它上面的一个不可见的按钮。当按下此按钮时,图像将全屏显示,当用户按下全屏时,图像将返回。效果很好。问题是,当图像大小调整回来时,界面会被阻止(它不会崩溃),它只会阻止所有用户交互。尽管我有与视图层次结构相关的理论,但我看不出问题出在哪里......
这是有问题的动画的完整代码。
- (IBAction) imageButtonPressed {
NSLog(@"Entered imageButtonPressed method");
imageFullscreenView = [[UIImageView alloc]
initWithFrame:CGRectMake(8, 72, 72, 72)];
[imageFullscreenView setImage:[self.coolView image]];
[imageFullscreenView setContentMode:UIViewContentModeScaleAspectFit];
UIWindow *mainWindow = [[UIApplication sharedApplication] keyWindow];
[mainWindow addSubview:imageFullscreenView];
// With Concurrent Block Programming:
[UIView animateWithDuration:0.5 animations:^{
[imageFullscreenView setFrame:CGRectMake(0, 0, 320, 480)];
imageFullscreenView.transform = CGAffineTransformMakeScale(1, 1);
imageButton = [[[UIButton alloc]
initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
[[[UIApplication sharedApplication] keyWindow] addSubview:imageButton];
[[[UIApplication sharedApplication] keyWindow]
addSubview:imageFullscreenView];
} completion: ^(BOOL finished) {
NSLog(@"entered First animation");
[self animationDidStop:@"Expand" finished:YES context:nil];
}];
}
- (void) animationDidStop:(NSString *) animationID finished:(BOOL)
finished context:(void *)context {
NSLog(@"Entered animationDidStop");
NSLog(@"animationID: %@", animationID);
if ([animationID isEqualToString:@"Expand"]) {
NSLog(@"Entered First if");
NSLog(@"imageButton enabled: %d", self.imageButton.enabled);
NSLog(@"coolButton enabled: %d", coolButton.enabled);
NSLog(@"uncoolButton enabled: %d", uncoolButton.enabled);
NSLog(@"reportButton enabled: %d", self.reportButton.enabled);
imageButton.enabled = YES;
imageButton = [[[UIButton alloc]
initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
[imageButton addTarget:self
action:@selector(didViewFullscreen:)
forControlEvents:UIControlEventTouchDown];
[[[UIApplication sharedApplication] keyWindow] addSubview:imageButton];
imageButtonPressed = NO;
} else {
}
}
- (void) didViewFullscreen: (id) selector {
NSLog(@"Entered didViewFullscreen");
[imageButton removeFromSuperview];
[UIView animateWithDuration:0.5 animations:^{
[imageFullscreenView setFrame:CGRectMake(8, 72, 72, 72)];
} completion: ^(BOOL finished){
NSLog(@"FINISHED");
//NSLog(@"imageButton enabled: %d", self.imageButton.enabled);
NSLog(@"coolButton enabled: %d", coolButton.enabled);
NSLog(@"uncoolButton enabled: %d", uncoolButton.enabled);
NSLog(@"reportButton enabled: %d", self.reportButton.enabled);
//[imageFullscreenView setFrame:CGRectMake(20, 72, 280, 192)];
imageFullscreenView.transform = CGAffineTransformMakeScale(1, 1);
[imageFullscreenView removeFromSuperview];
imageButton = [[[UIButton alloc]
initWithFrame:CGRectMake(20, 72, 280, 192)] autorelease];
[imageButton addTarget:self
action:@selector(imageButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
[imageButton setImage:nil forState:UIControlStateNormal];
[[[UIApplication sharedApplication] keyWindow] addSubview:self.imageButton];
}];
}
I am having issues with a simple animation. I have a UIImageView and an invisible button on top of it. When this button is pressed the image goes full screen, when the user presses the full screen it goes back. That works fine. The problem is that when the image is resized back the interface gets blocked (it does not crash) it just blocks all user interaction. I can't see where the problem lies though I have the theory that is something related to the view hierarchy...
Here is the entire code for the animation in question.
- (IBAction) imageButtonPressed {
NSLog(@"Entered imageButtonPressed method");
imageFullscreenView = [[UIImageView alloc]
initWithFrame:CGRectMake(8, 72, 72, 72)];
[imageFullscreenView setImage:[self.coolView image]];
[imageFullscreenView setContentMode:UIViewContentModeScaleAspectFit];
UIWindow *mainWindow = [[UIApplication sharedApplication] keyWindow];
[mainWindow addSubview:imageFullscreenView];
// With Concurrent Block Programming:
[UIView animateWithDuration:0.5 animations:^{
[imageFullscreenView setFrame:CGRectMake(0, 0, 320, 480)];
imageFullscreenView.transform = CGAffineTransformMakeScale(1, 1);
imageButton = [[[UIButton alloc]
initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
[[[UIApplication sharedApplication] keyWindow] addSubview:imageButton];
[[[UIApplication sharedApplication] keyWindow]
addSubview:imageFullscreenView];
} completion: ^(BOOL finished) {
NSLog(@"entered First animation");
[self animationDidStop:@"Expand" finished:YES context:nil];
}];
}
- (void) animationDidStop:(NSString *) animationID finished:(BOOL)
finished context:(void *)context {
NSLog(@"Entered animationDidStop");
NSLog(@"animationID: %@", animationID);
if ([animationID isEqualToString:@"Expand"]) {
NSLog(@"Entered First if");
NSLog(@"imageButton enabled: %d", self.imageButton.enabled);
NSLog(@"coolButton enabled: %d", coolButton.enabled);
NSLog(@"uncoolButton enabled: %d", uncoolButton.enabled);
NSLog(@"reportButton enabled: %d", self.reportButton.enabled);
imageButton.enabled = YES;
imageButton = [[[UIButton alloc]
initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
[imageButton addTarget:self
action:@selector(didViewFullscreen:)
forControlEvents:UIControlEventTouchDown];
[[[UIApplication sharedApplication] keyWindow] addSubview:imageButton];
imageButtonPressed = NO;
} else {
}
}
- (void) didViewFullscreen: (id) selector {
NSLog(@"Entered didViewFullscreen");
[imageButton removeFromSuperview];
[UIView animateWithDuration:0.5 animations:^{
[imageFullscreenView setFrame:CGRectMake(8, 72, 72, 72)];
} completion: ^(BOOL finished){
NSLog(@"FINISHED");
//NSLog(@"imageButton enabled: %d", self.imageButton.enabled);
NSLog(@"coolButton enabled: %d", coolButton.enabled);
NSLog(@"uncoolButton enabled: %d", uncoolButton.enabled);
NSLog(@"reportButton enabled: %d", self.reportButton.enabled);
//[imageFullscreenView setFrame:CGRectMake(20, 72, 280, 192)];
imageFullscreenView.transform = CGAffineTransformMakeScale(1, 1);
[imageFullscreenView removeFromSuperview];
imageButton = [[[UIButton alloc]
initWithFrame:CGRectMake(20, 72, 280, 192)] autorelease];
[imageButton addTarget:self
action:@selector(imageButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
[imageButton setImage:nil forState:UIControlStateNormal];
[[[UIApplication sharedApplication] keyWindow] addSubview:self.imageButton];
}];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的方法
animationDidStop:finished:context:
中,您再次创建了一个imageButton
,而没有删除在方法imageButtonPressed
中创建的按钮。[imageButton removeFromSuperview]
在分配另一个之前将解决问题。In your method
animationDidStop:finished:context:
you are again creating animageButton
without removing the one created in the methodimageButtonPressed
.[imageButton removeFromSuperview]
before allocating another one will solve the problem.你最好使用
将
UIViewAnimationOptions
设置为UIViewAnimationOptionAllowUserInteraction
完成,这不会阻止您的 UI。
You're better off using
with the
UIViewAnimationOptions
set toUIViewAnimationOptionAllowUserInteraction
This would not block your UI.