自动滚动问题

发布于 2024-10-17 03:57:37 字数 1045 浏览 3 评论 0原文

1.创建一个cocoa应用程序(不是基于文档的)

2.创建一个新类“StretchView”(NSView的子类)

3.打开界面构建器并将“滚动视图”拖到主窗口

4.选择“滚动视图”并设置类“StretchView”(在类标识窗口中)

contentview 的大小为 500*500,strechview 的大小也是 500*500 (水平滚动已启用)。

然后我开始一个接一个地水平画一些数字(1,2,3,4……)。 当数字超出范围时(x pos 大于 500)我增加宽度 StretchView 的。 (到目前为止一切正常)

然后我尝试让水平滚动条自动滚动到最后,这样 每次我增加 StretchView 的宽度时,最后一个数字可能是 看到了。

这是代码:

//The timer is called every sec
-(void)myTimerAction:(NSTimer *) timer
{  
    NSLog(@"myTimerAction");

       //......
    int i = _myArray.count;
    NSRect rect = [self frame];
    int width = rect.size.width;

       //The width between two number is 10
       //When the x pos of current num is bigger then the scroll's width
    if((i * 10) > width) {

           //reset the width
        width = i * 10;
        [self setFrameSize:CGSizeMake(width, rect.size.height)];
           //How to make it autoscroll???
           //...............................
    }
    //......
    [self setNeedsDisplay:YES];
} 

1.Create a cocoa application (not document-based)

2.Create a new class "StretchView"(subclass NSView)

3.Open the Interface builder and drag a "Scroll view" to the main window

4.Choose the "Scroll view" and set the class "StretchView" (in class identity window)

The size of the contentview is 500*500 and the size of the strechview is also 500*500
(horizontal Scroll is enabled).

Then I start to draw some numbers(1,2,3,4......) horizontally one after the other.
When the number is out of ranger(the x pos is larger than 500) I increase the width
of the StretchView. (Everything works fine up till this point)

Then I tried to make the horizontal scroller to automatically scroll to the end so
that everytime I increase the width of the StretchView the last number coulde be
seen.

Here's the code:

//The timer is called every sec
-(void)myTimerAction:(NSTimer *) timer
{  
    NSLog(@"myTimerAction");

       //......
    int i = _myArray.count;
    NSRect rect = [self frame];
    int width = rect.size.width;

       //The width between two number is 10
       //When the x pos of current num is bigger then the scroll's width
    if((i * 10) > width) {

           //reset the width
        width = i * 10;
        [self setFrameSize:CGSizeMake(width, rect.size.height)];
           //How to make it autoscroll???
           //...............................
    }
    //......
    [self setNeedsDisplay:YES];
} 

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

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

发布评论

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

评论(1

心房的律动 2024-10-24 03:57:37

试试这个:

NSView *contentView = [[self enclosingScrollView] contentView];
CGFloat newXPosition = width - NSWidth([contentView bounds]);
if (newXPosition > 0.0) [self scrollPoint:NSMakePoint(newXPosition, 0.0)];

contentView 是封闭滚动视图中的剪切视图。您希望将当前视图滚动到视图中的 x 点,以便该 x 点 + 剪切视图宽度给出视图的最后一个剪切视图宽度点。

Try this:

NSView *contentView = [[self enclosingScrollView] contentView];
CGFloat newXPosition = width - NSWidth([contentView bounds]);
if (newXPosition > 0.0) [self scrollPoint:NSMakePoint(newXPosition, 0.0)];

contentView is the clipping view in the enclosing scroll view. You want to scroll your current view to the x point in your view such that this x point + the clipping view width give the last clipping view width points of your view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文