xcode iCarousel 线性类型从中心开始
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14453644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iCarousel Linear Type starts from center
提问by Zeeshan Jan
I would like iCarousel to start appearing from left i.e it should be left aligned. I have found that in case of Linear Carousel it starts from center of the screen.
我希望 iCarousel 从左侧开始出现,即它应该左对齐。我发现在 Linear Carousel 的情况下,它从屏幕中心开始。
How can I make the Linear Carousel to start from left?
我怎样才能让线性旋转木马从左边开始?
采纳答案by Pavel Rudkouski
In my case, I was helped by the use of the method [self.vwCoverFlow scrollToItemAtIndex:2 animated:YES];
in my viewDidLoad.
就我而言,[self.vwCoverFlow scrollToItemAtIndex:2 animated:YES];
在我的 viewDidLoad 中使用该方法对我有所帮助。
回答by Ankit
If you want a linear scrolling , you can use swipeView class , it is from the same developer as iCarousel but for linear scrolling only . I have used it and it do support left alignment . Its easy to add and used, same as iCarousel
如果您想要线性滚动,您可以使用 swipeView 类,它与 iCarousel 来自同一开发人员,但仅用于线性滚动。我用过它,它确实支持左对齐。它易于添加和使用,与 iCarousel 相同
here is the link for the code https://github.com/nicklockwood/SwipeView
回答by Rajarshi
Set the wrap property to YES and return the number of placeholderinCarousel as 2
将 wrap 属性设置为 YES 并将 placeholderinCarousel 的数量返回为 2
回答by zvjerka24
I had the same problem and do it with following hack ;)
我遇到了同样的问题,并通过以下 hack 来解决;)
Problem:I need to have view that display only 3 items that are always left aligned.
问题:我需要的视图只显示 3 个始终左对齐的项目。
Resolution:What I have done is to always make at least 3 items. If I have 0, 1 or 2 items I'm always creating 3, but those that do not need to be shown I'm creating as empty UIView. I always have 2 placeholders, but in some cases one or both are empty.
解决方案:我所做的是始终至少制作 3 个项目。如果我有 0、1 或 2 个项目,我总是创建 3 个,但那些不需要显示的项目我创建为空的 UIView。我总是有 2 个占位符,但在某些情况下,其中一个或两个都是空的。
e.g. If we have 2 items to display, I'm actually creating three, but third one is empty UIView. I'm creating two placeholders and one Item.
例如,如果我们有 2 个项目要显示,我实际上创建了三个,但第三个是空的 UIView。我正在创建两个占位符和一个项目。
- First item is first placeholderat index 0
- Second item is Item
- Third item is second placeholderbut with empty UIView
- 第一项是索引 0 处的第一个占位符
- 第二个项目是项目
- 第三项是第二个占位符,但 UIView 为空
If we have 1 items to display, again I'm creating three, but second and third one are empty UIView. Same as in previous example, I'm creating two placeholders and one Item.
如果我们有 1 个项目要显示,我再次创建三个,但第二个和第三个是空的 UIView。与前面的示例相同,我正在创建两个占位符和一个项目。
- First item is first placeholderat index 0
- Second item is Itembut with empty UIView
- Third item is second placeholderbut with empty UIView
- 第一项是索引 0 处的第一个占位符
- 第二项是Item但 UIView 为空
- 第三项是第二个占位符,但 UIView 为空
Because of this logic I'm always cleaning view when reusing ([v removeFromSuperview]), to be sure it is clean and if new one needs to be displayed I'm adding it ([view addSubview...). Same logic for Itemand Placeholder
由于这个逻辑,我在重用时总是清理视图 ([v removeFromSuperview]),以确保它是干净的,如果需要显示新视图,我正在添加它 ([view addSubview...)。Item和Placeholder 的逻辑相同
If you need to have more than 3 displayed items you can use same logic, but change value 3 to something else. If I'm wrong update me ;)
如果您需要显示 3 个以上的项目,您可以使用相同的逻辑,但将值 3 更改为其他内容。如果我错了,请更新我;)
Here is a part of mine code that works for me ;)
这是我的代码的一部分,对我有用;)
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
return [[self getRecordings] count] > 3? [[self getRecordings] count] - 2: 1;
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
if (view == nil)
{
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
}
else
{
// If reusing remove content from holder view for fake items
for (UIView *v in view.subviews)
{
[v removeFromSuperview];
}
}
if ([[self getRecordings] count] >= 2)
{
[view addSubview:[(RecordingItemViewController*)[_recordingItemViewControllers objectAtIndex:index + 1] view]];
}
return view;
}
- (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel
{
return 2;
}
- (UIView *)carousel:(iCarousel *)carousel placeholderViewAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
if (view == nil)
{
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
}
else
{
// If reusing remove content from holder view for fake items
for (UIView *v in view.subviews)
{
[v removeFromSuperview];
}
}
if (([[self getRecordings] count] > 0 && [[self getRecordings] count] < 3 && index == 0) || [[self getRecordings]count] >= 3)
{
[view addSubview:[(RecordingItemViewController*)(index == 0? [_recordingItemViewControllers objectAtIndex:0]: [_recordingItemViewControllers lastObject]) view]];
}
return view;
}
回答by user2713033
This works for me
这对我有用
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel{
[carousel scrollToItemAtIndex:photosArray.count/2 animated:YES];
return [photosArray count];
}
回答by nahung89
in your case, i would like to suggest using 2 placeholder.
在您的情况下,我建议使用 2 个占位符。
Ex: you have n photos, then you will have (n-2) items and 2 placeholders. - Index of items is from 1 to (n-2). - Index of placeholders is 0 and (n-1). Now we will have some code like this:
例如:您有 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);
}