【问题标题】:positioning uicontrol to create infinite scroll定位uicontrol创建无限滚动
【发布时间】:2014-09-08 12:12:10
【问题描述】:

我是 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;
}

【问题讨论】:

  • 这可能不符合您的确切需求,但 Apple 有一个 pretty good tutorial video here 涵盖了使滚动视图无限化。它在 Advanced ScrollView Techniques 下。

标签: ios objective-c scroll uicontrol uicontrolview


【解决方案1】:

如果我理解您正在尝试正确执行的操作,我认为您想以不同的方式来解决这个问题。您的数据模型(数组)不需要更改。滚动时发生的所有变化都是视图,即屏幕上显示的内容。实现无限滚动外观的最简单方法是使用UITableView 并为其提供大量单元格。然后您的 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]];
}

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

【讨论】:

    猜你喜欢
    • 2019-07-04
    • 1970-01-01
    • 1970-01-01
    • 2022-12-24
    • 1970-01-01
    • 2021-04-30
    • 1970-01-01
    • 2017-05-11
    • 1970-01-01
    相关资源
    最近更新 更多