Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 伊卡鲁塞尔线型从中心开始_Ios_Xcode_Icarousel - Fatal编程技术网

Ios 伊卡鲁塞尔线型从中心开始

Ios 伊卡鲁塞尔线型从中心开始,ios,xcode,icarousel,Ios,Xcode,Icarousel,我希望iCarousel从左边开始显示,即它应该是左对齐的。我发现,在线性旋转木马的情况下,它从屏幕的中心开始 如何使线性旋转木马从左侧开始?如果需要线性滚动,可以使用swipeView类,它与iCarousel来自同一个开发人员,但仅用于线性滚动。我用过它,它确实支持左对齐。 它易于添加和使用,与iCarousel相同 这是代码的链接在您的情况下,我建议使用2占位符 例:你有n张照片,然后你会有(n-2)个项目和2个占位符。 -项目索引从1到(n-2)。 -占位符的索引为0和(n-1)。 现在

我希望iCarousel从左边开始显示,即它应该是左对齐的。我发现,在线性旋转木马的情况下,它从屏幕的中心开始


如何使线性旋转木马从左侧开始?

如果需要线性滚动,可以使用swipeView类,它与iCarousel来自同一个开发人员,但仅用于线性滚动。我用过它,它确实支持左对齐。 它易于添加和使用,与iCarousel相同


这是代码的链接

在您的情况下,我建议使用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);
}

将wrap属性设置为YES,并将占位符的编号返回为2(我遇到了相同的问题,并使用以下hack;)

问题: 我需要有一个视图,显示只有3个项目总是左对齐

分辨率: 我所做的是总是做至少3件物品。如果我有0、1或2个项目,我总是创建3个,但那些不需要显示的项目,我创建为空UIView。我总是有两个占位符,但在某些情况下,一个或两个占位符都是空的

e、 g。 如果我们有两个项目要显示,我实际上创建了三个,但第三个是空的UIView。我正在创建两个占位符和一个项目

  • 第一项是索引0处的第一个占位符
  • 第二项是第三项
  • 第三项是第二个占位符,但视图为空
如果我们有1个项目要显示,我再次创建了3个,但是第二个和第三个项目是空的。和前一个例子一样,我创建了两个占位符和一个项目

  • 第一项是索引0处的第一个占位符
  • 第二项是项,但UIView为空
  • 第三项是第二个占位符,但视图为空
由于这种逻辑,我在重用([v removeFromSuperview])时总是清理视图,以确保它是干净的,如果需要显示新视图,我将添加它([view addSubview…)。项和占位符的逻辑相同

如果需要显示3个以上的项目,可以使用相同的逻辑,但将值3更改为其他值。如果我错了,请更新我;)

下面是我的代码中适合我的一部分;)

-(NSUInteger)项目编号转盘:(iCarousel*)转盘
{
返回[[self-getRecordings]计数]>3?[[self-getRecordings]计数]-2:1;
}
-(UIView*)旋转木马:(iCarousel*)旋转木马视图for ItemAtIndex:(nsInteger)索引重用视图:(UIView*)视图
{
如果(视图==nil)
{
view=[[UIView alloc]initWithFrame:CGRectMake(0,0,50,30)];
}
其他的
{
//如果重复使用,请从持有者视图中删除假冒项目的内容
用于(视图中的UIView*v.子视图)
{
[v从SuperView移除];
}
}
如果([[self-getRecordings]计数]>=2)
{
[视图添加子视图:[(RecordingItemViewController*)[[u RecordingItemViewController对象索引:索引+1]视图];
}
返回视图;
}
-(整数)占位符编号转盘:(iCarousel*)转盘
{
返回2;
}
-(UIView*)旋转木马:(iCarousel*)旋转木马占位符视图索引:(NSUInteger)索引重用视图:(UIView*)视图
{
如果(视图==nil)
{
view=[[UIView alloc]initWithFrame:CGRectMake(0,0,50,30)];
}
其他的
{
//如果重复使用,请从持有者视图中删除假冒项目的内容
用于(视图中的UIView*v.子视图)
{
[v从SuperView移除];
}
}
如果(([[self-getRecordings]计数]>0&&[[self-getRecordings]计数]<3&&index==0)|[self-getRecordings]计数]>=3)
{
[查看添加子视图:[(RecordingItemViewController*)(索引==0?[[U RecordingItemViewController对象索引:0]:[[U RecordingItemViewController lastObject])查看]];
}
返回视图;
}

在我的案例中,我使用了方法
[self.vwCoverFlow scrollToItemAtIndex:2动画:是];
在我的viewDidLoad中。

这对我很有效

    - (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel{

       [carousel scrollToItemAtIndex:photosArray.count/2 animated:YES];

        return [photosArray count];
       }

SWIFT 5:

对我有效的解决方案是:使旋转木马宽度加倍屏幕宽度,并将旋转木马约束到屏幕的x轴,然后添加0.5倍旋转木马宽度的
contentOffset
。在一些
containerView:UIView
中显示旋转木马也很重要,而不是直接在
viewController
中显示因为旋转木马的第一项居中于旋转木马的中心。如果将旋转木马设置为视图大小的两倍,并将
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
设置转盘内容偏移:

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

但是这个卷轴在第二个索引处的旋转木马没有显示从最左边开始的第一个项目?
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