0

我有滚动视图,其中子视图是从底部添加的,例如在墙上添加砖块(在我的情况下只有一块砖块),然后在其上添加下一块砖块,依此类推。与 default 一样UIScrollView ContentSize,滚动将发生在顶部。我怎样才能滚动到底部。如果我向下滚动,我应该能够看到其他的积木,它们一个接一个地添加。

正如您在下面的屏幕截图中看到的那样,开始按钮应该向下而不是向上。

非常感谢任何形式的帮助。

谢谢

示例代码:

CGFloat initialYPoint = 30;
for (OUSTLevel* currentLevel in self.course.sortedLevelsList) {

    NSArray* nibList = [[UINib nibWithNibName:@"OUSTLearningMapPlayLevelCell" bundle:nil] instantiateWithOwner:self options:nil];
    OUSTLearningMapPlayLevelCell* playLevelCell = nil;

    for (UIView* childView in nibList) {
        if ([childView isKindOfClass:[OUSTLearningMapPlayLevelCell class]]) {
            playLevelCell = (OUSTLearningMapPlayLevelCell*)childView;
            break;
        }
    }

    CGFloat cellHeight = CGRectGetHeight(playLevelCell.frame);
    CGFloat yPoint = CGRectGetHeight(self.scrollview.frame) - (cellHeight + initialYPoint);
    CGRect cellFrame = CGRectMake(0, yPoint, viewWidth, cellHeight);

    [playLevelCell setFrame:cellFrame];      
    [self.scrollview addSubview:playLevelCell];

    initialYPoint += cellHeight;

    if (!landingNodeLevelData.locked) {
        [playLevelCell.unlockedView setHidden:NO];
        [playLevelCell.lockedView setHidden:YES];

        // Frame calculation for placing pin
        CGFloat centerPoint = playLevelCell.unlockedView.frame.size.width / 4;
        pinXPoint = centerPoint + 5;
        pinYPoint = yPoint - 30;
    }
}

CGRect pinFrame = CGRectMake(pinXPoint, pinYPoint, 80, 80);

[self.pinImageView setFrame:pinFrame];
[self.scrollview addSubview:self.pinImageView];
[self.view bringSubviewToFront:self.pinImageView];
self.scrollview.contentSize = CGSizeMake(self.view.frame.size.width, initialYPoint);
self.scrollview.contentInset = UIEdgeInsetsMake(30.0, 0.0, 30.0, 0.0);
CGPoint bottomOffset = CGPointMake(0, self.scrollview.contentSize.height - self.scrollview.bounds.size.height+ 30);

编辑:我没有使用约束布局,我正在计算和设置框架。

第一个屏幕

第二屏

第三屏

在此处输入图像描述

4

1 回答 1

0

It looks like you could make things a bit easier on yourself.

If you Reverse the order of your "brick" stacking, you can be filling the view from the Top-Down instead of Bottom-Up ... which is much more logical and easier to think about.

This is a modification to your pastebin code. It should work fine... I didn't have your data / classes / xib views / etc, but I'm pretty sure I simulated it so this will run without error:

// start with 30-pt "padding" at the top
CGFloat initialYPoint = 30;

// reverse the array of OUSTLevel objects so we're going from top-down instead of bottom-up
NSArray *reversed = [[self.course.sortedLevelsList reverseObjectEnumerator] allObjects];

for (OUSTLevel* currentLevel in reversed) {

    NSArray* nibList = [[UINib nibWithNibName:@"OUSTLearningMapPlayLevelCell" bundle:nil] instantiateWithOwner:self options:nil];
    OUSTLearningMapPlayLevelCell* playLevelCell = nil;

    for (UIView* childView in nibList) {
        if ([childView isKindOfClass:[OUSTLearningMapPlayLevelCell class]]) {
            playLevelCell = (OUSTLearningMapPlayLevelCell*)childView;
            break;
        }
    }

    CGFloat cellHeight = CGRectGetHeight(playLevelCell.frame);

    // we're going top-down now, so Y coordinate will be a simple increment
    CGFloat yPoint = initialYPoint;

    CGRect cellFrame = CGRectMake(0, yPoint, viewWidth, cellHeight);

    [playLevelCell setFrame:cellFrame];

    [self.scrollview addSubview:playLevelCell];

    initialYPoint += cellHeight;

    if (!landingNodeLevelData.locked) {
        [playLevelCell.unlockedView setHidden:NO];
        [playLevelCell.lockedView setHidden:YES];

        // Frame calculation for placing pin
        CGFloat centerPoint = playLevelCell.unlockedView.frame.size.width / 4;
        pinXPoint = centerPoint + 5;
        pinYPoint = yPoint - 30;
    }
}

CGRect pinFrame = CGRectMake(pinXPoint, pinYPoint, 80, 80);

[self.pinImageView setFrame:pinFrame];
[self.scrollview addSubview:self.pinImageView];
[self.view bringSubviewToFront:self.pinImageView];

// add 30-pt "padding" at the bottom
initialYPoint += 30;

// set contentSize
self.scrollview.contentSize = CGSizeMake(cellWidth, initialYPoint);

// we want to initially see the "Start Rocket" visible at the bottom of the scroll view
CGFloat yOffset = scrollview.contentSize.height - scrollview.bounds.size.height;
scrollview.contentOffset = CGPointMake(0, yOffset);

Hope it helps - if anything's not clear, ask away :)

于 2017-06-21T14:05:48.683 回答