【问题标题】:Why is user interface blocked after animation?为什么动画后用户界面被阻塞?
【发布时间】:2011-02-22 09:05:55
【问题描述】:

我遇到了一个简单动画的问题。我有一个 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];        
}];

}

【问题讨论】:

    标签: iphone objective-c animation block


    【解决方案1】:

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

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

    【讨论】:

    • 就是这样!我不知道我怎么没有发现这一点。非常感谢,我已经被这个问题困扰了很长时间......有时你看不到你面前的东西......我希望我能给你的不仅仅是一票:)
    【解决方案2】:

    你最好使用

    • (void)animateWithDuration:(NSTimeInterval)duration 延迟:(NSTimeInterval)延迟 选项:(UIViewAnimationOptions)选项 动画:(无效(^)(无效))动画 完成:(无效(^)(布尔 完成))完成

    UIViewAnimationOptions 设置为UIViewAnimationOptionAllowUserInteraction

    这不会阻止您的用户界面。

    【讨论】:

    • 很抱歉,谢谢您的建议,但这不起作用。一定是别的东西……
    • 啊……应该已经彻底阅读了代码。同意 Sana 的回答。
    猜你喜欢
    • 1970-01-01
    • 2019-02-18
    • 1970-01-01
    • 2017-01-26
    • 2014-08-11
    • 1970-01-01
    • 2016-05-24
    • 2017-04-26
    • 1970-01-01
    相关资源
    最近更新 更多