几天前想通了,以为我会回来为其他有相同问题的人回答。
我应该使用什么类?我假设NSView,但就像我说的我以前从未这样做过,所以我不确定。
NSView其实就是我用来绘制每一页的类。
我是否需要为每个对象指定像素坐标,或者我可以让每个对象以某种方式相对于另一个对象?
我最终为网格上的每个图像(加上其标题)指定了像素坐标,但是一旦我了解了 8.50 x 11 英寸页面的大小(以磅为单位),就很容易计算出它们应该放置的位置。下一个挑战是将它们绘制在for 循环中,而不是必须显式声明每个可能的NSRect。这是我在drawRect 中的代码:
// Declared elsewhere: constants for horizontal/vertical spacing,
// the width/height for an image, and a value for what row the image
// should be drawn on
for (int i = 0; i < [_people count]; i++) {
float horizontalPoint = 0.0; // What column should the image be in?
if (i % 2 != 0) { // Is i odd? (i.e. Should the image be in the right column?)
horizontalPoint += (imageWidth + horizontalSpace); // Push it to the right
}
NSRect imageRect = NSMakeRect(horizontalSpace + horizontalPoint, verticalSpace + verticalPoint,
imageWidth, imageHeight);
// Draw the image with imageRect
if (i % 2 != 0) { // Is i odd? (i.e. Is the current row drawn?)
verticalPoint = (imageRect.origin.y + imageRect.size.height); // Push the row down
}
}
我确实意识到我本可以更有效地编写代码(例如,为 i % 2 != 0 制作一个 BOOL),但我急于完成整个项目,因为我需要它的朋友在截止日期前。
如何为视图创建单独的页面?
通过谷歌搜索,我找到了this SO answer。但是,除非我有一个将所有页面连接在一起的大视图,否则这是行不通的。我想出了一个方法来做到这一点:
// Get an array of arrays containing 1-6 JANPerson objects each using data from a parsed in .csv
NSArray *paginatedPeople = [JANGridView paginatedPeople:people];
int pages = [JANGridView numberOfPages:people];
// Create a custom JANFlippedView (just an NSView subclass overriding isFlipped to YES)
// This will hold all of our pages, so the height should be the # of pages * the size of one page
JANFlippedView *view = [[JANFlippedView alloc] initWithFrame:NSMakeRect(0, 0, 612, 792 * pages)];
for (int i = 0; i < [paginatedPeople count]; i++) { // Iterate through each page
// Create a custom JANGridView with an array of people to draw on a grid
JANGridView *gridView = [[JANGridView alloc] initWithFrame:NSMakeRect(0, 0, 612, 792) people:paginatedPeople[i]];
// Push the view's frame down by 792 points for each page drawn already
// and add it to the main view
gridView.frame = NSMakeRect(0, 792 * i, gridView.frame.size.width, gridView.frame.size.height);
[view addSubview:gridView];
}
如果这对任何人来说难以理解,我深表歉意;我更擅长谈论我的过程而不是写作!如果有不清楚的地方,我欢迎任何人寻求帮助,或者如果他们可以做得更好,请编辑。