Stylo
Stylo

Reputation: 250

UIScrollView paging from center to left and right

I made a horizontal UIScrollView, with paging enabled. This allow me to "swipe" a certain view away. The problem I have is that the scrollview is positioned to the left. I can only scroll (read: swipe) my certain view to the left. I also want to let it swipe to the right, so both sides. I think it's a problem with centering the UIScrollView. I tried a lot of things but it all didn't work out.

Here's my code (vertScroll is the horizontal scrollView):

    - (void)viewDidLoad {
    [super viewDidLoad];


     vertScroll.contentSize = CGSizeMake(3*375, 232);
    vertScroll.frame = CGRectMake(0, 0, 375, 232);
    vertScroll.pagingEnabled = YES;

    vertScroll.scrollEnabled = YES;
    vertScroll.showsHorizontalScrollIndicator = YES;

    vertScroll.contentOffset = CGPointMake(375, 0);


    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];


    [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:255.0 green:125.0 blue:71.0 alpha:1.0]];
    self.navigationController.navigationBar.topItem.title = @"blabla";

    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSForegroundColorAttributeName:[UIColor whiteColor],
       NSFontAttributeName:[UIFont fontWithName:@"StoneSansBold" size:21]}];

    self.scrollView.showsVerticalScrollIndicator=YES;
    self.scrollView.scrollEnabled=YES;
    self.scrollView.userInteractionEnabled=YES;

    self.scrollView.contentSize = CGSizeMake(320, 800);

    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];

    // add effect to an effect view
    self.effectView = [[UIVisualEffectView alloc]initWithEffect:blur];
    self.effectView.alpha = 0.9;
    self.effectView.frame = CGRectMake(self.coverImage.frame.origin.x, self.coverImage.frame.origin.y+108, self.coverImage.frame.size.width, 34);

    // add the effect view to the image view
[self.coverImage addSubview:self.effectView];

   [self.cellView addSubview:self.seismo];


    self.seismo = [[UIImageView alloc]initWithFrame:CGRectMake(2, self.coverImage.frame.size.height/2, 40, 40)];
    self.seismo.animationImages = [NSArray arrayWithObjects:
                                         [UIImage imageNamed:@"tmp-0.gif"],
                                         [UIImage imageNamed:@"tmp-1.gif"],
                                         [UIImage imageNamed:@"tmp-2.gif"],
                                         [UIImage imageNamed:@"tmp-3.gif"],[UIImage imageNamed:@"tmp-4.gif"], nil];
    self.seismo.animationDuration = 1.0f;
    self.seismo.animationRepeatCount = 0;
    [self.seismo startAnimating];
    [self.cellView addSubview:self.seismo];
    self.seismo.alpha = 0.0;

    [self.cellView setFrame:CGRectMake(375, 0, 375, 232)];




           // Do any additional setup after loading the view, typically from a nib.
}

Explanation with an illustration: enter image description here

Upvotes: 1

Views: 917

Answers (1)

Erik Terwan
Erik Terwan

Reputation: 2780

You should make 3 pages, so the contentSize should be 3 times the size of one page, as far as i can see, that is happening.

Now make sure the user always starts on page 2 (the center page), do this by setting the contentOffset to 1x the width of the scrollView for the x coordinate and 0 for the y coordinate.

After that, the user should be able to scroll left and right.

PS: by looking at your code, it seems like you are assuming everybody uses an iPhone 6 (width of 375pt), you might want to use the calculated width (by using self.view.frame.size.width or something relative)

Upvotes: 3

Related Questions