简体   繁体   中英

UIScrollView Subview Scaling

I'm attempting to scale a UIScrollView's subviews, based off of how far away they are from the center of the container. I think I'm close, but it isn't quite right. The views to the left of the center line are scaled a bit less than those to the right.

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    for( unsigned int i=0; i<[scrollView.subviews count]; i++ )
    {
        UIView *v = [scrollView.subviews objectAtIndex:i];

        // not quite right here
        float scale = 1. - ( abs(scrollView.center.x - ( v.center.x - scrollView.contentOffset.x ) ) / scrollView.contentSize.width/2 );

        v.alpha = scale;

        v.transform = CGAffineTransformIdentity;
        v.transform = CGAffineTransformScale(v.transform, scale, scale);

    }
}

If anyone has any thoughts, I'd greatly appreciate it, as it has been a long day fighting with this.

Testing your scale calculation you can see there's something wrong in the first part of it. Imagine the following values: scrollview center x = 10 scrollView contentOffset x = 30 v1 center x = 5 v2 center x = 15

v1 and v2 are mirrored (5 pixels to the left of the center and 5 to the right of the center), so they should return the same scale value. If you do the calculation with your code, it returns 35 and 25, which means the calculation isn't right.

I'm not sure how do you want your scale to behave exactly but a suggestion would be:

float scale = 1. - ( (scrollView.contentOffset.x - abs( v.center.x - scrollView.center.x ) ) / scrollView.contentSize.width/2 );

Edit: try this one:

float scale = 1. - (abs( v.center.x - scrollView.center.x ) ) / scrollView.contentSize.width/2 );

There are many ways of doing your calculation. My point is that you should make sure that your operations are giving you the expected results with fake values, and yours is not. Your dividend is giving wrong results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM