为什么动画后用户界面会被阻塞?

发布于 2024-10-18 16:09:13 字数 3488 浏览 3 评论 0原文

我对简单的动画有疑问。我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半世蒼涼 2024-10-25 16:09:13

在您的方法 animationDidStop:finished:context: 中,您再次创建了一个 imageButton,而没有删除在方法 imageButtonPressed 中创建的按钮。

[imageButton removeFromSuperview] 在分配另一个之前将解决问题。

In your method animationDidStop:finished:context: you are again creating an imageButton without removing the one created in the method imageButtonPressed.

[imageButton removeFromSuperview] before allocating another one will solve the problem.

梦断已成空 2024-10-25 16:09:13

你最好使用

  • (void)animateWithDuration:(NSTimeInterval)持续时间
    延迟:(NSTimeInterval)延迟
    选项:(UIViewAnimationOptions)选项
    动画:(void (^)(void))动画
    完成:(无效 (^)(BOOL
    完成))完成

UIViewAnimationOptions 设置为 UIViewAnimationOptionAllowUserInteraction

完成,这不会阻止您的 UI。

You're better off using

  • (void)animateWithDuration:(NSTimeInterval)duration
    delay:(NSTimeInterval)delay
    options:(UIViewAnimationOptions)options
    animations:(void (^)(void))animations
    completion:(void (^)(BOOL
    finished))completion

with the UIViewAnimationOptions set to UIViewAnimationOptionAllowUserInteraction

This would not block your UI.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文