简体   繁体   English

AnimateWithDuration禁用UIView上的userInteraction

[英]AnimateWithDuration disables userInteraction on UIView

I'm adding a child view on top of one of my views, and animating it so that it starts at the bottom of the screen and ends up at the top of the screen by using animateWithDuration. 我在其中一个视图的顶部添加了一个子视图,并对其进行了动画处理,从而使其通过使用animateWithDuration在屏幕的底部开始并在屏幕的顶部结束。 And that all works fine except that after animateWithDuration is complete the view on top has no user interaction and the view below still has user interaction. 一切正常,除了animateWithDuration完成后,上方的视图没有用户交互,而下方的视图仍然具有用户交互。 If I remove animateWithDuration , and just start the child view at it's normal position the user interaction works how I would expect it too, which is why I think animateWithDuration is the problem. 如果我删除animateWithDuration ,而只是在其正常位置启动子视图,则用户交互也会按我期望的方式工作,这就是为什么我认为animateWithDuration是问题所在。

Here's my code: 这是我的代码:

UIViewController *viewController = [[CCouponDetailViewController alloc] init];
[[viewController view] setBounds:CGRectMake(0, ([UIScreen mainScreen].bounds.size.height * -1), viewController.view.bounds.size.width, viewController.view.bounds.size.height)];
[self addChildViewController:viewController];
[self.view addSubview:viewController.view];

CGRect newFrame = viewController.view.frame;
newFrame.origin.x = 0;
newFrame.origin.y = ([UIScreen mainScreen].bounds.size.height * -1);

[UIView animateWithDuration:1.0
     animations:^{
         viewController.view.frame = newFrame;
     }
     completion:^(BOOL finished){
         [viewController didMoveToParentViewController:self];
     }
];

Another question I have (Not really important just curious) is that in newFrame I'm setting the y to the same thing as it is when I initially set the bounds, but yet it moves. 我有另一个问题(只是好奇而已,并不是很重要),就是在newFrame中,我将y设置为与最初设置边界时相同的东西,但是它会移动。 I would have expected newFrame to require ay value of "0" but when I did that nothing happened. 我本来希望newFrame要求ay值为“ 0”,但是当我这样做时,什么也没发生。 Just wondering why that is. 只是想知道为什么。

I'll actually go backwards on this one... 我实际上会退后一步...

"Another question I have (Not really important just curious) is that in newFrame I'm setting the y to the same thing as it is when I initially set the bounds, but yet it moves. I would have expected newFrame to require ay value of "0" but when I did that nothing happened. Just wondering why that is." “我还有一个问题(不是很重要,只是好奇)是,在newFrame中,我将y设置为与最初设置边界时相同的东西,但是它仍然可以移动。我希望newFrame要求ay值表示“ 0”,但是当我这样做时,什么也没发生。只是想知道为什么会这样。”

This is actually a very important question for you to have answered, because it has a lot to do with why your code isn't working. 对于您来说,这实际上是一个非常重要的问题,因为它与您的代码为何无法正常工作有很大关系。 You're misunderstanding some very important concepts. 您误解了一些非常重要的概念。

First off, the bounds of a UIView determine its position entirely in relation to itself. 首先, UIViewbounds完全确定其相对于自身的位置。 The frame determines the UIView 's position within its superview. frame确定UIView在其父视图中的位置。 In your code, you're originally setting the view's bounds , not the frame -- 在您的代码中,您最初是在设置视图的bounds ,而不是frame -

[[viewController view] setBounds:CGRectMake(0, ([UIScreen mainScreen].bounds.size.height * -1), viewController.view.bounds.size.width, viewController.view.bounds.size.height)];

-- so you're not at all originally determining the view's place within its superview, only in relation to itself. -因此,您根本不是在确定视图在其父视图中的位置,而只是确定与它本身的关系。

Essentially, you're ending up with a CCouponDetailViewController view with a 0x0 frame, but since you haven't specified that you want your UIView to clip its subviews, the portions of your CCouponDetailViewController view are not actually on top of their view, but visible and hanging over the UIView 's bounds. 本质上,您最终得到一个带有0x0框架的CCouponDetailViewController视图,但是由于您未指定要让UIView裁剪其子视图,因此CCouponDetailViewController视图的部分实际上不在其视图顶部,而是可见的并悬在UIView的边界上。 The reason they aren't selectable is because they're not actually within the UIView . 之所以不能选择它们,是因为它们实际上不在UIView

So to fix this, set your UIView 's initial frame instead of setting a bounds (I've editing the initial frame to start at the bottom of the screen like you're trying to do. I don't understand the height x -1 thing you're trying to do, by the way...) : 因此,要解决此问题,请设置UIView的初始框架,而不要设置边界 (我正在尝试按照您的UIView将初始框架编辑为从屏幕底部开始。我不知道x的高度-顺便说一句,您正在尝试做的事情...):

[[viewController view] setFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height, viewController.view.bounds.size.width, viewController.view.bounds.size.height)];

Secondly, you're setting your new frame incorrectly. 其次,您错误地设置了新框架。 You can't set the CGRect this way: 您不能通过以下方式设置CGRect:

CGRect newFrame = viewController.view.frame;
newFrame.origin.x = 0;
newFrame.origin.y = ([UIScreen mainScreen].bounds.size.height * -1);

Instead, set it using CGRectMake (And, again, I edited the y value so that the view ends up at the top of the screen like you're trying to do): 而是使用CGRectMake设置(同样,我编辑了y值,以使视图最终像您尝试的那样显示在屏幕顶部):

CGRect newFrame = CGRectMake(0, 0, viewController.view.frame.width, viewController.view.frame.height);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM