我创建了一个透明的模态视图,它工作正常。我想要的是在模态视图出现时使过渡发生渐变。下面是代码。

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];

最佳答案

使用以下代码淡入。

// 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];
}


淡出后删除modalView。

// Remove modalView after it faded out

- (void)removeModalView {

    [modalView removeFromSuperview];
}

关于iphone - 模态视图中的淡出过渡,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7121141/

10-09 15:34