模态视图中的淡出过渡

时间:2011-08-19 11:53:29

标签: iphone objective-c ios xcode

我创建了一个透明的模态视图,它工作正常。我想要的是在模态视图出现时进行淡入淡出过渡。下面是代码..

UIView *modalView = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
modalView.opaque = NO;
modalView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.9];

UILabel *label1 = [[[UILabel alloc] init] autorelease];
label1.text = @"Modal View";
label1.textColor = [UIColor whiteColor];
label1.backgroundColor = [UIColor clearColor];
label1.opaque = NO;
[label1 sizeToFit];
[modalView addSubview:label1];

[self.view addSubview:modalView];

1 个答案:

答案 0 :(得分:3)

使用以下代码淡入。

// Fade In

- (void)fadeInModalView {

    [self.view addSubview:modalView];
    modalView.alpha = 0;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    modalView.alpha = 0.9;
    [UIView commitAnimations];
}

淡出。

// Fade Out

- (void)fadeOutModalView {

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(removeModalView)];
    modalView.alpha = 0.0;
    [UIView commitAnimations];
}

在淡出幻灯片后删除它。

// Remove modalView after it faded out

- (void)removeModalView {

    [modalView removeFromSuperview];
}