Re: Scroll Views
Re: Scroll Views
- Subject: Re: Scroll Views
- From: Dave Hersey <email@hidden>
- Date: Sat, 22 Sep 2007 01:03:34 -0400
- Resent-date: Sat, 22 Sep 2007 01:05:27 -0400
- Resent-from: Dave Hersey <email@hidden>
- Resent-message-id: <email@hidden>
- Resent-to: Cocoa Dev <email@hidden>
If you use a matrix and make that a subview of a scrollview, the
scrolling comes for free. You can test this in Interface Builder by
making a window with a big matrix of buttons and then going up to the
Layout menu with that selected, and selecting Make subviews of
>Scroll View. If you shrink the scroll view and then do a command-
R to test run the interface, you'll see that the scrolling to your
buttons is handled for you.
Now, having said all that, whenever I've done this, for a real
application, you'll want to create an NSMatrix subclass and make that
a subview of an NSScrollView. So, same thing, but you'd make the
matrix programatically. Making a subclass of NSMatrix lets you
respond to view sizing changes, as well as allowing the user to
change the size of the thumbnails themselves if you want that. You
also need to create a subclass to set up your items, unless they're
going to be exactly the same as some standard button that already
exists. I've always used an NSImageCell subclass for that so that I
had more control over the layout.
You don't need to do any calculations besides how many of your
buttons fit in the given width and how many rows of them you need to
display them all. There should be a lot of information in the
archives about this. This works really well for thumbnail type views,
which it sounds like you're working on. You really shouldn't have to
worry about the scrolling at all.
Basically, the matrix is like one huge sheet of your buttons. You
make that programattically, and then the scroll view controls how
much of that is visible. If the user grows the view, then you change
the number of columns in your matrix to make it fit nicely. If they
scroll, you do nothing. The scroll view handles all that for you.
It's easier than the code you're writing below. :)
- Dave
On Sep 21, 2007, at 11:46 PM, Development wrote:
I don't think a matrix would work would it? It wouldn't allow me to
scroll through...
However... It took some time but between my books and the developer
site i did finally get the scrollview to work the way I wanted. I
have included the working code on the chance that some one with the
same problem searches the list archive. This is just code I used to
figure out the scroll view. The actual program will get images from
a folder.
-(void)awakeFromNib
{
[scroller setHasVerticalScroller:YES];
NSString * imagePath =@"/Users/fns/Pictures/DSCF0019.JPG";
int n = 0;
float top = (10.0*140.0);
NSRect scf = [scrollClip frame];
[scrollClip setFrame:NSMakeRect(scf.origin.x,scf.origin.y,
150.0,top)];
while(n < 10){
ThumbNail * thisThumb =[[ThumbNail alloc]initWithFile:imagePath
originx:5.0 originy:(top -140)];
[scrollClip addSubview:[thisThumb thumbNailImage]];
top -=140;
n++;
}
[scroller setDocumentView:scrollClip];
NSPoint newOrigin = NSMakePoint(0.0,NSMaxY([[scroller
documentView] frame])-NSHeight([[scroller contentView]bounds]));
float height = NSHeight([[scroller contentView]bounds]);
float width = NSWidth([[scroller contentView]bounds]);
[[scroller contentView] scrollRectToVisible:NSMakeRect
(newOrigin.x,newOrigin.y,width,height)];
NSPoint csp=[[scroller contentView] bounds].origin;
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden