私は、一連のロゴを宣伝する「イメージ ティッカー」を作成するために UICollectionView を使用するプロジェクトに取り組んでいます。collectionView は高さが 1 項目、長さが 12 項目で、一度に 2 ~ 3 項目を表示します (表示されるロゴのサイズによって異なります)。
最初の項目から最後の項目までゆっくりと自動スクロールするアニメーションを作成し、それを繰り返したいと思います。
誰かこれを動作させることができましたか?スクロールは次のようにして動作させることができます
[myCollection scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:(myImages.count -1) inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
しかし、これはあまりにも速すぎます!
[UIView animateWithDuration:10 delay:2 options:(UIViewAnimationOptionAutoreverse + UIViewAnimationOptionRepeat) animations:^{
[myCollection scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:(myImages.count -1) inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:NO];
} completion:nil];
これにより、望ましいスクロール速度が得られますが、一連のセルのうち最後の数個のみが表示されます。これらのセル (および最初に表示されるセル) は、すぐにキューから削除されると思われます。
何かご意見は?
ベストアンサー1
次のアプローチを試すことができます:
@property (nonatomic, assign) CGPoint scrollingPoint, endPoint;
@property (nonatomic, strong) NSTimer *scrollingTimer;
@synthesize scrollingPoint, endPoint;
@synthesize scrollingTimer;
- (void)scrollSlowly {
// Set the point where the scrolling stops.
self.endPoint = CGPointMake(0, 300);
// Assuming that you are starting at {0, 0} and scrolling along the x-axis.
self.scrollingPoint = CGPointMake(0, 0);
// Change the timer interval for speed regulation.
self.scrollingTimer = [NSTimer scheduledTimerWithTimeInterval:0.015 target:self selector:@selector(scrollSlowlyToPoint) userInfo:nil repeats:YES];
}
- (void)scrollSlowlyToPoint {
self.collectionView.contentOffset = self.scrollingPoint;
// Here you have to respond to user interactions or else the scrolling will not stop until it reaches the endPoint.
if (CGPointEqualToPoint(self.scrollingPoint, self.endPoint)) {
[self.scrollingTimer invalidate];
}
// Going one pixel to the right.
self.scrollingPoint = CGPointMake(self.scrollingPoint.x, self.scrollingPoint.y+1);
}