ios - 用于图像 Objective C (iOS) 的 ScrollView

标签 ios objective-c uiscrollview

我正在尝试为我的应用程序创建一个欢迎页面(带有屏幕截图)。

我已经取了一些样本并将其制作成文字卷轴。我已经尝试了数周但没有成功。谢谢

int numberOfPages = 5;
UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
someScrollView.pagingEnabled = YES;
someScrollView.contentSize = CGSizeMake(numberOfPages * someScrollView.frame.size.width, someScrollView.frame.size.height);
[self.view addSubview:someScrollView];
   
for (int i = 0; i < numberOfPages; i++) {
    UILabel *tmpLabel = [[UILabel alloc] initWithFrame:CGRectMake(i * someScrollView.frame.size.width + 20,
                                                                  20,
                                                                  someScrollView.frame.size.width - 40,
                                                                  20)];
    tmpLabel.textAlignment = UITextAlignmentCenter;
    tmpLabel.text = [NSString stringWithFormat:@"THIS ACTUALLY WORKS!! %d", i];
    [someScrollView addSubview:tmpLabel];
}

这是我对图像的失败尝试:

int numberOfPages = 5;
UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
someScrollView.pagingEnabled = YES;
someScrollView.contentSize = CGSizeMake(numberOfPages * someScrollView.frame.size.width, someScrollView.frame.size.height);
[self.view addSubview:someScrollView];

for (int i = 0; i < numberOfPages; i++) {
    UIImageView *tmpLabel = [[UIImageView alloc] initWithFrame:CGRectMake(i * someScrollView.frame.size.width + 20,
                                                                  20,
                                                                  someScrollView.frame.size.width - 40,
                                                                  20)];
    tmpLabel.imageAlignment = UIImageAlignmentCenter;
    tmpLabel.text = [NSString stringWithFormat:@"THIS ACTUALLY WORKS!! %d", i];
    [someScrollView addSubview:tmpLabel];
}
    UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:subview1.frame];
    
    if (i == 0) {
        imageView1.image = [UIImage imageNamed:@"openingScreen1.png"];
    }else if (i == 1){
        imageView1.image = [UIImage imageNamed:@"openingScreen2.png"];
    }else if (i == 2){
        imageView1.image = [UIImage imageNamed:@"openingScreen3.png"];
    }else {
        imageView1.image = [UIImage imageNamed:@"openingScreen4.png"];
    }

我可能还需要删除“Parse”这个词,除非上面的图片位于它上面?我找不到这个词。

enter image description here

最佳答案

试试这个

UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,
                                                                              self.view.frame.size.width,
                                                                              self.view.frame.size.height)];
someScrollView.pagingEnabled = YES;
[someScrollView setAlwaysBounceVertical:NO];

//setup internal views
NSInteger numberOfPages = 5;


for (int i = 0; i < numberOfPages; i++)
{
     CGFloat xOrigin = i * self.view.frame.size.width+10;
    UILabel *tmpLabel = [[UILabel alloc] initWithFrame:CGRectMake(xOrigin,70,self.view.frame.size.width,25)];
    tmpLabel.textAlignment = NSTextAlignmentCenter;
    tmpLabel.text = [NSString stringWithFormat:@"THIS ACTUALLY WORKS!! %d", i+1];
    [someScrollView addSubview:tmpLabel];



    // calculate the width


    UIImageView *image = [[UIImageView alloc] initWithFrame:
                          CGRectMake(xOrigin, tmpLabel.frame.size.height + tmpLabel.frame.origin.y + 10,
                                     self.view.frame.size.width,
                                     self.view.frame.size.height)]; // customize your width, height and y co ordinates

    image.image = [UIImage imageNamed:[NSString stringWithFormat:
                                       @"openingScreen%d.jpg", i+1]];
    image.contentMode = UIViewContentModeScaleAspectFit;
    [someScrollView addSubview:image];


}
//set the scroll view content size
someScrollView.backgroundColor = [UIColor blueColor];
someScrollView.contentSize = CGSizeMake(self.view.frame.size.width *
                                        numberOfPages, 
                                        self.view.frame.size.height);
//add the scrollview to this view
[self.view addSubview:someScrollView];

样本输出是

enter image description here

我在这里附上了 sample Project另外,请使用这个

关于ios - 用于图像 Objective C (iOS) 的 ScrollView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32883548/

相关文章:

ios - 如果已打开通知,如何直接转到特定的 View Controller

ios - 将日历格鲁吉亚语更改为波斯语时,无法将类型 'NSLocale' 的值强制转换为类型 'Locale'

iphone - 如何使用 IDA/pro 给 iPhone 应用打补丁?

iphone - 位置追踪

iphone - 区分applicationDidEnterBackground和applicationWillTerminate

ios - 将 UITableView 滑动到屏幕上,继续滑动?

ios - 设置 UIScrollView 的 contentInset 为 KeyboardWillShowNotification 无法正常工作

iphone - 回收单元格时的 UITableView 单元格布局

ios - 有人可以向我解释 initWithNibName 在 iOS 中的用法吗?

ios - UITableView 限制滚动部分