以编程方式调整 NSView 的大小

发布于 2024-11-24 16:34:53 字数 1183 浏览 3 评论 0原文

我有一个主视图,应该显示几个子视图。这些子视图位于彼此的正上方和正下方(在 z 轴上),并且将向下(在 y 轴上)向下移动并使用以下代码向上移动:

if (!CGRectIsNull(rectIntersection)) {
    CGRect newFrame = CGRectOffset (rectIntersection, 0, -2);
    [backgroundView setFrame:newFrame];
} else{
    [viewsUpdater invalidate];
    viewsUpdater = nil;
}

rectIntersection 用于告知视图何时完全向下移动并且不再位于视图的后面前面的一个(当它们不再重叠 rectIntersection 为 null 时),它一次向下移动 2 个像素,因为这全部都在重复计时器内。我希望我的主视图(包含这两个其他视图的主视图)向下调整大小,以便它在背景视图降低时扩展。这是我正在尝试的代码:

CGRect mainViewFrame = [mainView frame];
if (!CGRectContainsRect(mainViewFrame, backgroundFrame)) {
    CGRect newMainViewFrame = CGRectMake(0,
                                         0,
                                         mainViewFrame.size.width,
                                         (mainViewFrame.size.height + 2));
    [mainView setFrame:newMainViewFrame];
}

这个想法是检查 mainView 是否包含此背景视图。当backgroundView降低时,主视图不再包含它,并且它(应该)向下扩展2个像素。这种情况会发生,直到背景视图停止移动并且主视图最终包含背景视图。

问题是 mainView 根本没有调整大小。背景视图正在降低,我可以看到它,直到它从主视图的底部消失。 mainView 应该调整大小,但在任何方向上都没有改变。我尝试使用 setFrame 和 setBounds (带和不带 setNeedsDisplay),但没有任何效果。

我真的只是在寻找一种以编程方式更改主视图大小的方法。

I have a main view that is supposed to display several subviews. Those subviews are directly above and below one another (in the z axis) and will drop down (in the y axis) and move up using this code:

if (!CGRectIsNull(rectIntersection)) {
    CGRect newFrame = CGRectOffset (rectIntersection, 0, -2);
    [backgroundView setFrame:newFrame];
} else{
    [viewsUpdater invalidate];
    viewsUpdater = nil;
}

rectIntersection is used to tell when the view has entirely moved down and is no longer behind the front one (when they no longer overlap rectIntersection is null), it moves down by 2 pixels at a time because this is all inside a repeating timer. I want my main view, the one that contains these two other views, to resize downward so that it expands just as the view in the background is being lowered. This is the code I'm trying for that:

CGRect mainViewFrame = [mainView frame];
if (!CGRectContainsRect(mainViewFrame, backgroundFrame)) {
    CGRect newMainViewFrame = CGRectMake(0,
                                         0,
                                         mainViewFrame.size.width,
                                         (mainViewFrame.size.height + 2));
    [mainView setFrame:newMainViewFrame];
}

The idea is to check if the mainView contains this background view. When the backgroundView is lowered, the main view no longer contains it, and it (should) expand downward by 2 pixels. This would happen until the background view stopped moving and the mainView finally contains backgroundView.

The problem is that the mainView is not resizing at all. the background view is being lowered, and I can see it until it disappears off the bottom of the mainView. mainView should have resized but it does not change in any direction. I tried using setFrame and setBounds (with and without setNeedsDisplay) but nothing worked.

I'm really just looking for a way to programmatically change the size of the main view.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天旳寂寞 2024-12-01 16:34:54

我想我明白了,问题是什么。我仔细阅读了代码。

if (!CGRectIsNull(rectIntersection)) {
    // here you set the wrong frame
    //CGRect newFrame = CGRectOffset (rectIntersection, 0, -2);
    CGRect newFrame = CGRectOffset (backgroundView.frame, 0, -2);
    [backgroundView setFrame:newFrame];
} else{
    [viewsUpdater invalidate];
    viewsUpdater = nil;
}

rectIntersection 实际上是两个重叠视图的交集,并且当 backgroundView 向下移动时,该矩形的高度会减小。
这样 mainView 只调整一次大小。

除此之外,这里有一个使用块语法的简单解决方案,为您的视图设置动画,此代码通常会在您的自定义视图控制器中执行。

// eventually a control action method, pass nil for direct call
-(void)performBackgroundViewAnimation:(id)sender {
    // first, double the mainView's frame height
    CGFrame newFrame = CGRectMake(mainView.frame.origin.x,
                                  mainView.frame.origin.y,
                                  mainView.frame.size.width,
                                  mainView.frame.size.height*2);
    // then get the backgroundView's destination rect
    CGFrame newBVFrame = CGRectOffset(backgroundView.frame,
                                      0,
                                      -(backgroundView.frame.size.height));
    // run the animation
    [UIView animateWithDuration:1.0
                     animations:^{
                                     mainView.frame = newFrame;
                                     backgroundView.frame = newBVFrame;
                                 }
    ];
}

I think I understood, what the problem is. I read carefuly the code.

if (!CGRectIsNull(rectIntersection)) {
    // here you set the wrong frame
    //CGRect newFrame = CGRectOffset (rectIntersection, 0, -2);
    CGRect newFrame = CGRectOffset (backgroundView.frame, 0, -2);
    [backgroundView setFrame:newFrame];
} else{
    [viewsUpdater invalidate];
    viewsUpdater = nil;
}

rectIntersection is actually the intersection of the two views, that overlap, and as the backgroundView is moved downward, that rect's height decreases.
That way the mainView gets resized only one time.

To add on this, here is a simple solution using block syntax, to animate your views, this code would typically take place in your custom view controller.

// eventually a control action method, pass nil for direct call
-(void)performBackgroundViewAnimation:(id)sender {
    // first, double the mainView's frame height
    CGFrame newFrame = CGRectMake(mainView.frame.origin.x,
                                  mainView.frame.origin.y,
                                  mainView.frame.size.width,
                                  mainView.frame.size.height*2);
    // then get the backgroundView's destination rect
    CGFrame newBVFrame = CGRectOffset(backgroundView.frame,
                                      0,
                                      -(backgroundView.frame.size.height));
    // run the animation
    [UIView animateWithDuration:1.0
                     animations:^{
                                     mainView.frame = newFrame;
                                     backgroundView.frame = newBVFrame;
                                 }
    ];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文