【问题标题】:How can I make my view shrink and disappear?如何让我的视野缩小并消失?
【发布时间】:2011-08-28 05:45:40
【问题描述】:

当我的用户按下按钮时,我希望可见视图缩小并消失,从而显示下面的父视图。目前我有这个,几乎可以工作:

-(void)deleteNoteTransition
{
    self.view.userInteractionEnabled=NO;
    [UIView beginAnimations:@"deleteNote" context:nil];
    [UIView setAnimationDuration:0.6];
    self.view.transform=CGAffineTransformMakeScale(0.01,0.01);
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:)];
    [UIView commitAnimations];
}

- (void)animationDidStop:(NSString *)anim finished:(BOOL)flag
{
    self.view.userInteractionEnabled=YES;
    if ([anim isEqualToString:@"deleteNote"]) {
    [self.navigationController popViewControllerAnimated:YES];
    }
}

但是,这样做有两个问题:

  • 进行缩小的视图不是“整体”视图 - 它不包括标题栏和工具栏。
  • 视图缩小以显示纯白色背景,然后才触发第二种方法,因此白色背景被毫不客气地替换为父视图。

有没有办法包含整个屏幕并平滑地显示背后的父视图控制器,而无需进入对于我目前的编程水平来说过于复杂的 OpenGL 内容?

【问题讨论】:

    标签: iphone cocoa-touch uiview uiviewcontroller core-animation


    【解决方案1】:

    这应该可以捕捉到你的整个视图:

    #import <QuartzCore/QuartzCore.h>
    UIGraphicsBeginImageContext(viewToCapture.bounds.size);
    [viewToCapture.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

    一旦您将视图捕捉到图像,请使用此方法缩放和淡化图像:

    UIImageView *firstView = nil;
    
    -(void)animateOut:(UIImage*)image {
        firstView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 320, 480)];
        firstView.image = image
        [window addSubview:firstView];
        [window bringSubviewToFront:firstView];
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1.0];
        [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:window cache:YES];
        [UIView setAnimationDelegate:self]; 
        [UIView setAnimationDidStopSelector:@selector(startupAnimationDone:finished:context:)];
        firstView.alpha = 0.0;
        // change this for a different scale
        firstView.frame = CGRectMake(-60, -85, 440, 635);  
        [UIView commitAnimations];
    }
    
    - (void)startupAnimationDone:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
        [firstView removeFromSuperview];
        [firstView release];
    }
    

    您可以从[UIApplication sharedApplication] 获取窗口,或者只是将其添加为当前视图控制器的视图的子视图。

    【讨论】:

    • 为了其他遇到此问题的人的利益,我还发现可以通过使用setAnimationStopSelector 的菊花链动画获得更复杂的动画。如果第一个动画的曲线为 UIViewAnimationCurveEaseIn 而最后一个动画的曲线为 UIViewAnimationCurveEaseInOut,这通常效果最佳。中间的任何一个都可以是UIViewAnimationCurveLinear
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多