0

私はオブジェクティブ C のプログラミング学習の初心者であり、このサイトでの質問の初心者でもありますが、ご容赦ください。

現在、画面上にボックス (UIControls) の列を描画し、それらを上下に無限にスクロールできるようにしようとしています。そのため、画面の下部から外れると、下部に移動して再利用されます。

コードに多くの間違いがあるに違いないことはわかっています。しかし、私がやろうとしていることの要点は次のとおりです。ボックスはすべて配列(imArray)にあります。ボックスが画面の下部からスクロールすると、配列の最後から取り除かれ、最初に挿入されます。次に、ボックスがグラフィカルに列の上部に挿入されます。

最初の if ステートメントは、画面の下部からのスクロールを処理し、正常に動作します。しかし、同様のコードで反対のことをしようとする 2 番目の 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

あなたが正しくやろうとしていることを私が理解しているなら、あなたはこれを別の方法でやりたいと思っていると思います. データ モデル (配列) を変更する必要はありません。スクロールすると変化するのは、画面に表示されているビューだけです。無限スクロールの外観を実現する最も簡単な方法は、 a を使用して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]];
}

真の無限スクロールが必要な場合、これでは十分ではないかもしれませんが、ほとんどの目的では機能するはずです。

于 2014-09-08T12:46:50.060 に答える