ios - 在 ScrollView 中显示图像

标签 ios objective-c

我想在 ScrollView 中显示一些图像,但我遇到了一些问题。

这是我所拥有的:

- (void)viewDidLoad
{
    [super viewDidLoad];

    myObject = [[NSMutableArray alloc] init];

    [myObject addObject:@"tut_5.jpg"];
    [myObject addObject:@"tut_2.jpg"];
    [myObject addObject:@"tut_3.jpg"];
    [myObject addObject:@"tut_4.jpg"];

    pageControlBeingUsed = NO;


    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    CGFloat screenHight = screenRect.size.height;

    for (int i = 0; i < [myObject count]; i++) {
        CGRect frame;
        frame.origin.x = self.takeTourScrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.takeTourScrollView.frame.size;

        NSString *val = [myObject objectAtIndex:i];


        UIImage* theImage = [UIImage imageNamed:val];


        UIImageView *img=[[UIImageView alloc] initWithFrame:CGRectMake(screenWidth*i,0,185 ,284)];


        img.image = theImage;

        [self.takeTourScrollView addSubview:img];

    }

第一张图片似乎还可以。

enter image description here

然后当我向左滑动时,我得到一个空白屏幕

enter image description here

我再次向左滑动,然后我看到了我的第二张照片。所有 4 张图像都是这样。

任何想法我做错了什么?

最佳答案

有些系统组件可以用更少的工作和更精致的用户体验为您提供您想要的东西。使用 UICollectionView正如 Vive 在她的回答中提到的那样,是一种可能性。我的建议是 UIPageViewController .有一个名为 PhotoScroller 的示例应用程序来自 Apple,它展示了如何实现与您描述的非常相似的 UI。在 Xcode 文档中搜索“PhotoScroller”以找到它。

关于ios - 在 ScrollView 中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33566213/

相关文章:

ios - 我如何更改 NSMutableArray 中所有 Sprite 的图像

ios - Xcode - [UITableView _contentOffsetForScrollingToRowAtIndexPath :atScrollPosition:]: row (7) beyond bounds (0) for section (0).'

ios - ld : library not found for -lCocoaLumberjack

objective-c - 我们可以制定实现协议(protocol)吗?

iphone - 如何在 Monotouch 中对 UIImageView 进行运动模糊效果?

iOS: "recognizer.view.center = CGPointMake(..."的错误效果

ios - CAKeyframeAnimation 从最后一点开始重复旋转

ios - xCode 删除的目标仍在项目中

ios - 如何使用_几乎_相同的 UIActionSheet 委托(delegate)代码来干燥三个 Controller

objective-c - 在多个文件上设置 viewController 子类的代码是个好主意吗?