自动滚动问题
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
contentView
是封闭滚动视图中的剪切视图。您希望将当前视图滚动到视图中的 x 点,以便该 x 点 + 剪切视图宽度给出视图的最后一个剪切视图宽度点。Try this:
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.