开发者

Cocoa NSView animation

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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜