0

我是 Objective C 学习编程的初学者,也是在这个网站上提问的初学者,请多多包涵。

我目前正在尝试在屏幕上绘制一列框(UIControls),并且能够无限向上或向下滚动它们。因此,当一个人离开屏幕底部时,它会移动到底部并重复使用。

我知道代码中一定有很多错误。但我想做的事情的要点是:这些盒子都在一个数组(imArray)中。当一个框从屏幕底部滚动时,它会从数组的末尾取出,并插入到开头。然后框以图形方式将自身插入列的顶部。

第一个 if 语句处理从屏幕底部滚动,它工作正常。但是第二个 if 语句,我尝试用类似的代码做相反的事情,只有当我缓慢滚动时,当我快速滚动时,框之间的间距变得不均匀,有时一个框只是锁定在屏幕上并停止移动。

任何帮助表示赞赏,我将尝试提供可能需要的更多清晰度。

-(BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{

CGPoint pt = [touch locationInView:self];
int yTouchEnd = pt.y;
int yTouchChange = yTouchEnd - yTouchStart;

//iterate through all boxes in imArray
for(int i = 0; i < self.numberOfSections; i++)
{
    //1. get box
    STTimeMarker *label = self.imArray[i];
    //2. calculate new label transform
    label.transform = CGAffineTransformTranslate(label.startTransform, 0, yTouchChange);
    CGRect frame = label.frame;
    //3. if the box goes out of the screen on the bottom 
    if (frame.origin.y > [[UIScreen mainScreen]bounds].size.height)
    {

        //1. move box that left the screen to to beginning of array
        [self.imArray removeObjectAtIndex:i];
        [self.imArray insertObject:label atIndex:0];
        //2. get y value of box closest to top of screen. 
        STTimeMarker *labelTwo = self.imArray[1];
        CGRect frameTwo =labelTwo.frame;
        //3. put box that just left the screen in front of the box I just got y value of. 
        frame.origin.y = frameTwo.origin.y - self.container.bounds.size.height/self.numberOfSections;
        label.frame=frame;
     }

    //1. if the box goes out of the frame on the top
    // (box is 40 pixels tall)
    if (frame.origin.y < -40)
    {  
        [self.imArray removeObjectAtIndex:i];
        [self.imArray addObject:label];
        STTimeMarker *labelTwo = self.imArray[self.numberOfSections-1];
        CGRect frameTwo =labelTwo.frame;
        frame.origin.y = frameTwo.origin.y + self.container.bounds.size.height/self.numberOfSections;

        label.frame=frame;

    }
}

return YES;
}
4

1 回答 1

0

如果我理解您要正确执行的操作,我认为您想以不同的方式来解决这个问题。您的数据模型(数组)不需要更改。滚动时发生的所有变化都是视图,即屏幕上显示的内容。实现无限滚动外观的最简单方法是使用 aUITableView并为其提供大量单元格。然后,您的cellForRowAtIndexPath:方法将使用 mod 运算符 ( %) 返回单元格的正确位置。未经测试的代码:

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    return 99999;
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
    NSInteger moddedRow = indexPath.row % [self.imArray count];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kSomeIdentifierConst forIndexPath:[NSIndexPath indexPathForRow:moddedRow inSection:indexPath.section]];
    return [self configureCellWithData:self.imArray[moddedRow]];
}

如果您需要真正的无限滚动,这可能不足以满足您的目的,但应该适用于大多数目的。

于 2014-09-08T12:46:50.060 回答