iphone - 模态视图中的淡出过渡

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

最佳答案

使用以下代码淡入。

// 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/

相关文章:

ios - 对 UITableView 使用 didSelectRowAtIndexPath 或 prepareForSegue 方法?

objective-c - Swift 在 Objective-C block 中尝试

ios - 我将如何制作这种类型的 iOS 动画?

objective-c - 纯 C 语言的安全范围书签

ios - 尝试查看记录时,从结果类型为 'UITableViewCell *__strong' 的函数返回 'UITableView *' 的不兼容指针类型

iphone - 自动布局无法按预期工作

iphone - 如何调整 UIToolbar 实例包含的 UIBarButtonItem 的框架或垂直对齐方式?

ios - App Center 上的 Flutter iOS 构建失败并出现错误 : "Invalid Podfile file: Generated. xcconfig must exist

iphone - 更改 UIView 动画转换背后的背景

ios - 在应用颜色时对图像应用阴影