iCarousel が左から表示されるようにします。つまり、左揃えにする必要があります。Linear Carousel の場合、画面の中央から始まることがわかりました。
線形カルーセルを左から開始するにはどうすればよいですか?
線形スクロールが必要な場合は、swipeView クラスを使用できます。これは、iCarousel と同じ開発者によるものですが、線形スクロール専用です。私はそれを使用しましたが、左揃えをサポートしています。iCarousel と同じように、簡単に追加して使用できます
ここにコードのリンクがありますhttps://github.com/nicklockwood/SwipeView
wrap プロパティを YES に設定し、placeholderinCarousel の数を 2 として返します。
これは私のために働く
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel{
[carousel scrollToItemAtIndex:photosArray.count/2 animated:YES];
return [photosArray count];
}
あなたの場合、2 プレースホルダーを使用することをお勧めします。
例: n 個の写真がある場合、(n-2) 個のアイテムと 2 個のプレースホルダーがあります。- アイテムのインデックスは 1 から (n-2) までです。- プレースホルダーのインデックスは 0 および (n-1) です。これで、次のようなコードができます。
#pragma mark iCarousel DataSource
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
//if we have n photos, then number of items is (n-2)
int count = _movieGalleries.count;
return count >= 3 ? count - 2 : count;
}
- (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel
{
//2 placeholder
return _movieGalleries.count >= 3 ? 2 : 0;
}
#pragma mark iCarousel Delegate
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
//reuse your view
UIView *yourview = ...
//get your data
//index of item is 1 -> n-2, index of placeholder is 0 and n-1
Item *item = [_listItems objectAtIndex:index + 1];
//config your view
...
return yourView;
}
- (UIView*)carousel:(iCarousel *)carousel placeholderViewAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
//index of placeholder is 0 and n-1
//Trick: we use viewForItemAtIndex for getting placeholder view
index = index == 0 ? - 1 : carousel.numberOfItems;
return [self carousel:carousel viewForItemAtIndex:index reusingView:view];
}
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
//in carousel with placeholder, index of selected item is -1, 0, 1, ..., (n-2). which -1 and (n-2) is index of placeholders. So that we need to increase index by 1
NSLog("current index: %i", index + 1);
}
スイフト 5:
私にとってうまくいった解決策:カルーセルの幅を画面の幅の2倍にし、カルーセルを画面のXAnchorに制限し、後でcontentOffset
カルーセルの幅の0.5 xサイズを追加しました。カルーセルの最初のアイテムがカルーセルの中心に配置されるため、カルーセルcontainerView: UIView
を直接ではなく一部に表示することも重要です。viewController
カルーセルをビューの 2 倍のサイズにcontentOffset
して左に設定すると (- 0.5 x カルーセルのサイズ)、中央はビューの左側になります。コード例を次に示します。
カルーセルサイズの設定:
carousel.leadingAnchor.constraint(equalTo: containerView.leadingAnchor).isActive = true
carousel.trailingAnchor.constraint(equalTo: containerView.trailingAnchor).isActive = true
carousel.translatesAutoresizingMaskIntoConstraints = false
carousel.heightAnchor.constraint(equalToConstant: 200).isActive = true
carousel.widthAnchor.constraint(equalTo: self.containerView.widthAnchor, multiplier: 2).isActive = true
carousel.centerXAnchor.constraint(equalTo: containerView.centerXAnchor).isActive = true
carousel.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
カルーセル contentOffset の設定:
carousel.contentOffset = CGSize(width: - containerView.frame.size.width, height: 0)
プロパティscrollToItemBoundry
を設定して、右に大きくスクロールしたときにカルーセル アイテムが手の届かないところに移動しないようにします。
carousel.scrollToItemBoundary = true
後であなたをあなたに追加containerView
しますviewController.view
:
viewController.view.addSubView(containerView)
containerView.translatesAutoresizingMaskIntoConstraints = false
containerView.centerXAnchor.constraint(equalTo: viewController.view.centerXAnchor).isActive = true
containerView.centerYAnchor.constraint(equalTo: viewController.view.centerYAnchor).isActive = true