Friday, July 6, 2012

Adding fade Animation to the scrollview


Here is the simple usage of mapping the values to get fade effect while scrolling, I am not going to write whole code. Here is the logic...


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    mPreviousTouchPoint = scrollView.contentOffset;
}


- (void)scrollViewDidScroll:(UIScrollView *)sender
{
if(animation == fadeIn)
{
        pageWidth = self.pageSize.size.width;
        page = floor((self.scrollView.contentOffset.x - pageWidth) / pageWidth) + 1;
     
                 //Mapping values from 0-1024(subview frame) to 0-1(alpha range)
        float NewMax = 1;
        float NewMin = 0;
        float OldMax = pageWidth*(page+1);
        float OldMin = pageWidth*page;
     
        int OldValue = mScrollView.contentOffset.x;//doubth
        float newValue = (((OldValue - OldMin) * (NewMax - NewMin)) / (OldMax - OldMin)) + NewMin;
     
               //If it is scrolling to right applying animation to next page
        page = page+1;
     
                 //If it is scrolling to left applying animation to previous page
        if(mPreviousTouchPoint.x > mScrollView.contentOffset.x)
            page = page - 1;
     
        if(page < 0 || page >= mNoOfPages)
           return;
     
                //Applying alpha to the subview, where mSubviewsArray contains all the subviews of a       //scrollview
         UIView *nextPage = [mSubviewsArray objectAtIndex:page];
        if(nextPage)
        {
          if(mPreviousTouchPoint.x > mScrollView.contentOffset.x)
                nextPage.alpha = 1-newValue;
            else
                nextPage.alpha = newValue;
        }
}
}

No comments:

Post a Comment