我遇到一个简单的动画问题。我有一个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];
}];
}
答案 0 :(得分:3)
在您的方法animationDidStop:finished:context:
中,您再次创建imageButton
而不删除方法imageButtonPressed
中创建的那个。
[imageButton removeFromSuperview]
在分配另一个之前将解决问题。
答案 1 :(得分:1)
你最好使用
- (无效)animateWithDuration:(NSTimeInterval)持续时间 延迟:(NSTimeInterval)延迟 选项:(UIViewAnimationOptions)选项 动画:( void(^)(void))动画 完成:(void(^)(BOOL 完成))完成
将UIViewAnimationOptions
设置为UIViewAnimationOptionAllowUserInteraction
这不会阻止你的用户界面。