图片的滚动视图 Objective C (iOS)

ScrollView for images Objective C (iOS)

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

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

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”这个词,除非上面的图片位于它上面?我找不到这个词。

试试这个

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];

示例 OutPut 是

这里我也附上了sample Project,请用这个

请将您的代码更改为

int numberOfPages = 5;
    UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    someScrollView.pagingEnabled = YES;
    //set the scroll view delegate to self so that we can listen for changes
    someScrollView.delegate = self;
    someScrollView.contentSize = CGSizeMake(numberOfPages * someScrollView.frame.size.width, someScrollView.frame.size.height);
    [self.view addSubview:someScrollView];

    for (int i = 1; i <= numberOfPages; i++) {
        //set the origin of the sub view
        CGFloat myOrigin = i * self.view.frame.size.width;

        //get the sub view and set the frame
        UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(myOrigin, 0, self.view.frame.size.width, someScrollView.frame.size.height)];
       imageView1.image = [UIImage imageNamed: [NSString stringWithFormat:@"openingScreen%d.png",i ]];


        //add the subview to the scroll view
         [someScrollView addSubview:imageView1];
    }

添加滚动视图委托方法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{

}


//dragging ends, please switch off paging to listen for this event
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *) targetContentOffset NS_AVAILABLE_IOS(5_0)
{

    //find the page number you are on

        CGFloat pageWidth = scrollView.frame.size.width;
        int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
        NSLog(@"Dragging - You are now on page %i",page);



}