【问题标题】:how to show Container View ViewController and hide Container view ViewController如何显示容器视图 ViewController 和隐藏容器视图 ViewController
【发布时间】:2016-11-29 19:01:53
【问题描述】:

I want to show ContainerView's view controller as like this

我使用以下代码,它显示为我想要的

  [UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 0.8];
if (_addLinkQuestionView.isHidden == YES)
{
    _addLinkQuestionView.hidden = NO;
    _addLinkQuestionView.alpha = 1.0;
}
else
{
    _addLinkQuestionView.alpha = 0.0;
    _addLinkQuestionView.hidden = YES;
}
[UIView commitAnimations];

但是单击模糊区域后,我想隐藏容器视图。该区域是 UIButton。我使用以下代码,但它什么也没做。

 [UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 0.8];
    _addLinkQuestionView.alpha = 0.0;
    _addLinkQuestionView.hidden = YES;
[UIView commitAnimations];

任何帮助。提前谢谢。

【问题讨论】:

  • 你有什么容器? UIViewController 实例或带有 .h 和 .m 的 xib?
  • 请确保第二个代码部分正在执行,同时通过放置断点或NSLogNSLog 来点击模糊区域
  • 基本上,你想要的是在点击背景时用动画关闭弹出窗口吗?
  • 在需要创建新的视图控制器之前考虑使用自定义 ViewController Transition。这是很好的教程如何处理这个raywenderlich.com/110536/custom-uiviewcontroller-transitions
  • 容器有一个 UIViewController 实例

标签: ios objective-c xcode uicontainerview


【解决方案1】:

基本上,您似乎需要显示一个 alertview 行为,其中应用程序中的所有 ui 都被禁用,而只有对话框中的内容被启用。

  • 向您的应用委托添加一个公共方法,如 showOverlayView:(UIView*)v
  • 在此方法上创建一个视图,设置 alpha 并将其添加到 keywindow。
  • 现在将传递的视图添加到 keywindow 并计算和设置 其中心属性。

或者,您可以使用像 MJPopupViewControllerSLPopupViewController 这样的库来为您完成这项工作。

【讨论】:

  • 这张图片有你给camo.githubusercontent.com/…MJPopupViewController 是要使用的库的要求。或者在你的容器视图上添加 tapGestureRecognizer 来实现同样的效果。
  • 是的。正如你所展示的那样,但没有第 3 方集成。
  • 好的,不要使用第三方。假设您将容器存储在变量容器中。在你的 VC 加载函数的某处写 UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap)]; [self.tableView addGestureRecognizer:gestureRecognizer];然后声明这样的函数 -(void)handleTap { [container removeFromSuperView]; }
【解决方案2】:

正确的做法:

1- 新建文件 -> UIView -> 重命名为 addLinkQuestionView

2-新建文件->obj c类->重命名为addLinkQuestionView

现在你有一个 xib、一个 .h 和一个 .m

3- 转到 xib 并在文件所有者中选择您在步骤 2 中创建的 addLinkQuestionView

4- 将 xib 设计为您发布的图片链接,并将适当的网点链接到 addLinkQuestionView.h

5- 要在 .h 中初始化 uiview,请执行以下操作: #import "addLinkQuestionView.h"

@implementation addLinkQuestionView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during     animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
    // Initialization code.
    [[NSBundle mainBundle] loadNibNamed:@"addLinkQuestionView" owner:self options:nil];


    self.vuComplete.frame = CGRectMake(self.vuComplete.frame.origin.x, self.vuComplete.frame.origin.y, self.frame.size.width, self.frame.size.height);

    [self addSubview:self.vuComplete];

    self.vuContainer.layer.cornerRadius = 5.0;
    self.vuContainer.layer.borderWidth = 1.0/[UIScreen mainScreen].scale;
    self.vuContainer.layer.borderColor = [[UIColor clearColor]CGColor];
    self.vuContainer.alpha = 0.0;


    [self layoutIfNeeded];

}
return self;
}
 -(void)awakeFromNib
{


}



- (IBAction)onBackgroundTapDismissView:(id)sender {
[UIView animateWithDuration:0.5
                 animations:^{self.vuContainer.alpha = 0.0;}
                 completion:^(BOOL finished){  }];

[self removeFromSuperview];

}

注意:- (IBAction)onBackgroundTapDismissView 可以通过在 addLinkQuestionView 的灰色背景 uiview 上放置一个 uitapgesturerecognizer 来完成,以便点击它可以关闭整个 uiview (vuComplete)

6- 然后将其添加到您的主视图控制器中,该控制器显示此弹出窗口,如下所示: A-先导入addLinkQuestionView.h B-将此代码添加到您单击以呈现 addLinkQuestionView 的按钮操作中: addLinkQuestionView *popup = [[addLinkQuestionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

        [UIView animateWithDuration:0.25
                         animations:^{popup. addLinkQuestionView.alpha = 1.0;}
                         completion:^(BOOL finished){  }];
        [self.view addSubview:popup];

玩得开心!

【讨论】:

  • 如何使用 ViewController 而不是 xib。我已经为图中所示的容器设计了视图控制器。
  • 在动画中使用 presentViewController 函数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-05
  • 1970-01-01
  • 2016-03-22
  • 1970-01-01
  • 2017-10-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多