可可 NSView 动画
我有一个视图正在更大的视图中显示,并且将始终保留在同一个位置。当提供一些用户输入时,第二个视图将出现在第一个视图的下方。希望这是一个稍微平滑的过渡,所以我所做的是将第二个视图直接放在第一个视图后面,然后启动一个计时器,检查视图是否使用它们的交集重叠,如果重叠,第二个视图会稍微降低,并再次检查重叠,直到视图不再重叠,并且计时器停止。
我希望过渡看起来像是第二个视图位于第一个视图的后面,并且现在正在从下面滑出。出于这个原因,我创建了一个“imaginaryRect”,它准确地表示第二个视图(称为progressBarView)的位置(如果它完全显示的话),并在我向下移动 imaginaryRect 时使用它来检查交集。但我随后将交集下方的矩形(即 imaginaryRect 减去交集高度)设置为实际显示第二个视图的矩形,该矩形称为 newFrame。
除了第二个视图的动画方式之外,这一切都很完美。我希望它看起来从顶部视图下方滑出,这意味着当视图开始出现在顶部视图下方时,用户将首先看到第二个视图的底部出现,因为它进一步向下滑动。看到中间部分,最后顶部落下来,它们都可见了。实际发生的情况是,当视图开始出现时,它是从上到下出现的。首先出现顶部部分,然后是中间部分,然后是底部部分。它不会给人以从顶部滑出的印象。事实上,它看起来并没有移动,而是给人一种上面覆盖着它的东西正在滑落的印象。
我从前天就开始关注这个问题,我认为问题与框架的起源有关。但仅此而已。
这是我正在使用的代码:
CGRect imaginaryRect = NSMakeRect(progressBarView.frame.origin.x,progressBarView.frame.origin.y, view.frame.size.width, view.frame.size.height);
CGRect rectIntersection = CGRectIntersection (view.frame,
imaginaryRect);
//if view and imaginary progress bar view frame intersect, the view is lowered
//the second condition prevents one drop too many at the end
if (!CGRectIsNull(rectIntersection) && (rectIntersection.size.height!=0)) {
CGRect offsetFrame = CGRectOffset (imaginaryRect, 0, -1);
imaginaryRect = offsetFrame;
//rectIntersection is recalculated after the offset, so it's height is subtracted for newFrame
rectIntersection = CGRectIntersection(view.frame, imaginaryRect);
//the rect below the area of intersection
CGRect newFrame = CGRectMake(imaginaryRect.origin.x, imaginaryRect.origin.y, view.frame.size.width, view.frame.size.height-rectIntersection.size.height);
//move the PBview to the new, slightly lowered frame (this is the frame that's located below the front view, but subtracts the intersection)
[progressBarView setFrame:newFrame];
感谢您的帮助!
I have a view that is being shown within a larger view and will always remain in the same place. When some user input is provided, a second view will appear below the first one. want this to be a somewhat smooth transition so what I've done is place the second view directly behind the first one, then started a timer that check if the views overlap using their intersection, if they do, the second view is slightly lowered, and overlapping is checked again, until the views no longer overlap, and the timer stops.
I want the transition to seem like the second view was behind the first one and is now being slid out from underneath. For that reason I created an "imaginaryRect" that represents exactly where the second view (called progressBarView) would be, if it was being displayed entirely, and use that to check the intersection as I move imaginaryRect down. But I then make the rectangle below the intersection (which would be imaginaryRect minus the intersection height) the one that actually displays the second view, and that one's called newFrame.
This all works perfectly, except for how the second view is being animated. I want it to appear to slide out from under the top one, which would mean that as the view begins to appear below the top one, the user would first see the bottom part of the second view appear, as it slid further down he'd see the middle part, and finally the top finishes coming down and they are all visible. What is actually happening is that when the view starts appearing, it appears from the top down. First the top part appears, then middle, then bottom. It doesn't give the impression that it's being slid out from under the top one. In fact, it looks like isn't moving, and instead gives the impression that something that was on top of it covering it is being slid down.
I've been at this since the day before yesterday and I think the problem has to do with the origin of the frame. But that's it.
This is the code I'm using:
CGRect imaginaryRect = NSMakeRect(progressBarView.frame.origin.x,progressBarView.frame.origin.y, view.frame.size.width, view.frame.size.height);
CGRect rectIntersection = CGRectIntersection (view.frame,
imaginaryRect);
//if view and imaginary progress bar view frame intersect, the view is lowered
//the second condition prevents one drop too many at the end
if (!CGRectIsNull(rectIntersection) && (rectIntersection.size.height!=0)) {
CGRect offsetFrame = CGRectOffset (imaginaryRect, 0, -1);
imaginaryRect = offsetFrame;
//rectIntersection is recalculated after the offset, so it's height is subtracted for newFrame
rectIntersection = CGRectIntersection(view.frame, imaginaryRect);
//the rect below the area of intersection
CGRect newFrame = CGRectMake(imaginaryRect.origin.x, imaginaryRect.origin.y, view.frame.size.width, view.frame.size.height-rectIntersection.size.height);
//move the PBview to the new, slightly lowered frame (this is the frame that's located below the front view, but subtracts the intersection)
[progressBarView setFrame:newFrame];
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论