Autoscroll problen
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];
}
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.
精彩评论