gpt4 book ai didi

iphone - UIScrollView View

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:21:11 24 4
gpt4 key购买 nike

我正在使用 iCarousel 作为老虎机,对于那些不知道 iCarousel 的人来说,它是一个带有(分页, ScrollView )。所以在我的应用程序中,我滚动它,然后当它停止时它显示 Image(Result) 3 秒,然后弹出一个按钮,如果你按下按钮,它将删除 View(Image Result) 然后再次旋转。

这是我删除轮播 View 的方法:

    NSInteger CurrentImage = carousel.currentItemIndex;
[carousel removeItemAtIndex:CurrentImage animated:YES];
[images removeObjectAtIndex:CurrentImage];

但是当 carousel.numberOfItems 还剩下 1 个项目时,它不会滚动,而是停留在那里。

所以我想出了一种方法让它滚动,即使它只剩下 1 个项目(索引)。

我用最后一个索引插入了它,所以我尝试了这个:

[self.carousel insertItemAtIndex:0 animated:NO];

(但不起作用)

然后我尝试了另一个:

int lastObject = [images objectAtIndex: ([images count]-1)]
[self.carousel insertItemAtIndex:lastObject animated:NO];

(还是不行)

还有这个:

int count = [images count];
[images objectAtIndex:count - 1];

(仍然没有运气)

我做错了吗?或其他方法是什么?或者我可以只复制最后一个 View 吗?感谢您的帮助。

编辑:方法

     - (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
if ([images count] == 1 || [self.carousel numberOfItems] == 1)
return 2;

return [images count];
}

- (NSUInteger)numberOfVisibleItemsInCarousel:(iCarousel *)carousel
{
//limit the number of items views loaded concurrently (for performance reasons)
return 7;
}


- (UIView *)carousel:(iCarousel *)_carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{

if (index >= [images count] || index >= [carousel numberOfItems]) {
index = 0;
}

NSDictionary *obj = [images objectAtIndex:index];
view = [[UIImageView alloc] initWithImage:[obj objectForKey:@"image"]];
view.tag = index;

return view;
}


- (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel
{
//note: placeholder views are only displayed on some carousels if wrapping is disabled
return 0;
}


- (CGFloat)carouselItemWidth:(iCarousel *)carousel
{
//usually this should be slightly wider than the item views
return 400;
}

- (BOOL)carousel:(iCarousel *)carousel shouldSelectItemAtIndex:(NSInteger)index{
return 5;
}
- (BOOL)carouselShouldWrap:(iCarousel *)carousel
{
//wrap all carousels
return wrap;
}

删除方法:

-(void) deleteItem{
//Removes the object chosen
if (carousel.numberOfItems >= 1)
{
NSInteger index = carousel.currentItemIndex;
[carousel removeItemAtIndex:index animated:YES];
//[images removeObjectAtIndex:index];
//[images replaceObjectAtIndex:index withObject:[NSNull null]];
}

}

最佳答案

你可以做的就是制作

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

总是返回一个值 >1(例如 2);然后确保

- (UIView*)carousel:(iCarousel*)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView*)view;

始终为您从 numberOfItemsInCarousel 返回的最少数量的元素返回正确的 View 。

例如。

- (NSUInteger)numberOfItemsInCarousel:(iCarousel*)carousel {
if (numberOfViews == 1)
return 2;
return numberOfViews;
}


- (UIView*)carousel:(iCarousel*)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView*)view {

if (index >= numberOfViews) {
index = 0;
}

NSDictionary *obj = [images objectAtIndex:index];
view = [[UIImageView alloc] initWithImage:[obj objectForKey:@"image"]];
view.tag = index;
return view;
}

你还应该确保最后一个元素永远不会被删除,或者在上面的代码中添加一些守卫来管理没有元素留下的情况。

关于iphone - UIScrollView View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11339185/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com