【发布时间】: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