Yian
Yian

Reputation: 199

ScrollView for images Objective C (iOS)

I am trying to create a welcome page for my app (with screenshots).

I have taken some samples and made it into a text scroll. I have tried for weeks without any success. Thanks

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

Here is my failed attempt to do with images:

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

I also might need to remove the word "Parse" unless the images above sit on top of it? I cannot locate the word.

enter image description here

Upvotes: 1

Views: 2214

Answers (2)

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

try this

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

the sample OutPut is

enter image description here

here I attached the sample Project also, please use this

Upvotes: 1

Imran
Imran

Reputation: 2941

Please change your code to something like

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

Add scroll view Delegate methods

- (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);



}

Upvotes: 1

Related Questions