iphone - 随意混搭

标签 iphone ios xcode

所以在我的轮播中我实现了这个:

- (UIView *)carousel:(iCarousel *)_carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{   
   // int randomforIndex = arc4random()%[images count]+1;
        UIImageView *imageView  = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[images objectAtIndex:index]]];
        return imageView;


}

我用 NSString 分配我的图像。每当带有图像的轮播停在特定的 [images objectAtIndex:index] 时,它就会执行相应的操作。 但是,当我将 randomforIndex 部分用于我的 index 时,一切都变得困惑了。所以我的问题是如何在不随机化图像值的情况下随机化图像。

这是我使用 NSString 添加图像的方式:

for (int x=1; x<76;x++) {
        // add as NSString
        [images addObject:[NSString stringWithFormat:@"%d", x]];
            }

最佳答案

你可以试试这个:首先你用你的代码填充你的 images 数组。然后您洗牌这个有序数组。这里有一个很好的 NSArray 洗牌类别:Shuffling an NSArray .

请注意,您应该删除 NSArray+Helpers.h 中的空 #import 行

images = [[NSMutableArray alloc] init];
for (int x=1; x<76;x++)
{
    [images addObject:[NSString stringWithFormat:@"%d.png", x]];
}
images = (NSMutableArray *)[images shuffled];

这部分代码已经过测试,可以正常工作。

从现在开始,您应该可以使用没有任何随机化的简单 viewForItemAtIndex,因为文件名已经洗牌了。

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{   
    if (view == nil)
    {
        view = [[UIImageView alloc] init];
    }

    UIImage* nimage = [UIImage imageNamed:[images objectAtIndex:index]];
    view.image = nimage;
    view.frame = CGRectMake(0.0,0.0,nimage.size.width,nimage.size.height);  

    return view;
}

关于iphone - 随意混搭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10733108/

相关文章:

iphone - XCode 警告在后续 Build 调用后消失

iphone - 为什么我无法执行任何 CABasicAnimation 或关键帧动画内容?

ios - 如何在 ios 的 UIPICKER VIEW 中禁用滚动效果和阴影效果?

ios - iPhone、Xcode、有效架构的重要性

iphone - 需要经过多少秒才能保证出现 'loading bar'?

ios - 迪尔德 : Symbol not found: _OBJC_CLASS_$_NSTextTab

ios - 无法猜测 iOS 发生了什么崩溃

ios - Swift 3.0 xCode 8 迁移 pod

objective-c - 场景的 Sprite 套件快速边界

ios - 如何将 iPhone 应用程序从 Xcode 部署到真正的 iPhone 设备?