iOS - 背景视图正在消耗触摸事件

时间:2015-07-18 14:28:49

标签: ios objective-c swift uiview uikit

我首先介绍屏幕的外观。它是function show_posts() { $html = ""; $extra_css = ""; $latestPosts = new WP_Query('cat=5&posts_per_page=1'); if($latestPosts->have_posts()): while($latestPosts->have_posts()): $latestPosts->the_post(); if (has_post_thumbnail) $extra_css = "has-thumbnail"; $html = "<article class='post {$extra_css}'>"; $html .= "<div class='post-thumbnail'>"; $html .= "<a href='" . get_the_permalink() . "'>" . get_the_post_thumbnail('thumbnail') . "</a>"; $html .= "<div class='post-thumbnail-date'></h4>" . get_the_time('Y-m-d') . "<h4></div>"; $html .= "</div><!-- end post-thumbnail -->"; $html .= "<h3><a href='" . get_the_permalink() . "'>" . get_the_title() . "</a></h3>"; $html .= "<p class='post-info'>" . get_the_time('Y-m-d') . " | Av " . get_the_author() . "</p>"; $html .= "<p class='post-text'>" . get_the_excerpt() . "<a href='" . get_the_permalink() . "'> Läs mer...</a>"; $html .= "</p>"; $html .= "</article>"; endwhile; else: // Error message endif; wp_reset_postdata(); return $html; }覆盖100%的屏幕,UIPageViewController (在页面顶部vc)覆盖20%的屏幕。容易,对吗?基本上UIView已超过UIView

所以,我正以编程方式将UIPageViewController添加到我的视图控制器UIPageViewController。但self.view.addSubview(pageVC.view)界面构建器

中定义

出于这个原因,由于最后添加了UIView,因此UIPageViewController覆盖UIView,迫使我致电myUIView.layer.zPosition = 1,以便UIView可见。

问题是UIView上的任何触摸都由UIPageViewController消耗。我怎么解决这个问题?

1 个答案:

答案 0 :(得分:2)

特定视图的图层属性仅用于呈现该特定视图。这就是为什么更改图层不会影响与特定视图交互的任何内容的原因。

但是,UIView上的-sendSubviewToBack:-bringSubviewToFront:方法旨在实现您正在寻找的内容。在这里,您可以阅读docs

我编写了这段代码,以便您可以测试此方法的用途。尝试评论/取消注释最后一行或更改子视图的添加顺序,以便更好地了解其工作原理。

UIView *firstView = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
UIView *secondView = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 150, 150)];

firstView.backgroundColor = [UIColor greenColor];
secondView.backgroundColor = [UIColor redColor];

[self.view addSubview:firstView];
[self.view addSubview:secondView];

UITapGestureRecognizer *firstTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(firstTap)];
UITapGestureRecognizer *secondTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(secondTap)];
[firstView addGestureRecognizer:firstTap];
[secondView addGestureRecognizer:secondTap];

[self.view sendSubviewToBack:secondView];