Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImageView Fading based on ScrollView Position

I have a UIPageControl that flips through different pages. I'd like to have a UIImageView begin to fade as the user scrolls from page 1 to page 2. I'm struggling trying to figure out the proper way to add a layer based on the position. Any help would be most appreciated...

[UIView beginAnimations:@"fade out" context:nil];
    [UIView setAnimationDuration:1.0]; // some how based on position?? 
    imageView.alpha = 1.0; // here to?
    [UIView commitAnimations];

Edit:

Set the delegate:

 self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

    self.pageController.dataSource = self;
like image 452
Bob Yousuk Avatar asked Aug 15 '13 02:08

Bob Yousuk


1 Answers

You need the delegate to override the following method:

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

Then you should check against the scrollView.contentOffset.x value to determine how much the scroll view has scrolled horizontally, and use it to determine your imageView's alpha as follows:

CGFloat newAlpha = scrollView.contentOffset.x / scrollView.frame.size.width; // This will need to be adapted depending on the content size of your scroll view -- do not just copy and paste this expecting it to work :)
imageView.alpha = newAlpha;
like image 90
bachonk Avatar answered Oct 19 '22 19:10

bachonk