问题描述:

I have two buttons on which I want to change their alpha while scrolling 2 views horizontally.

I want one button to start as 0.3 alpha and the other one as 1.0. When I scroll to the other view I want them to switch values: first button to 1.0 and second to 0.3.

I just played with one button inside the scrollViewDidScroll delegate:

 allBtn.alpha = scrollView.contentOffset.x / scrollView.bounds.size.width * 0.3

I will have 0.3 alpha after scroll, but when I go back, it will be 0.

Any idea how to do this correctly ?

网友答案:
- (CGFloat)calculateButtonAlphaUsingScrollView:(UIScrollView *)scrollView withAlphaForOffsetZero:(CGFloat)alphaForOffsetZero withAlphaForOffsetMax:(CGFloat)alphaForOffsetMax {
    CGFloat width = scrollView.frame.size.width;
    CGFloat contentWidth = scrollView.contentSize.width;
    CGFloat scrollLength = contentWidth - width;
    CGFloat scrollOffset = scrollView.contentOffset.x;
    CGFloat scrollValue = scrollOffset / scrollLength;
    CGFloat alphaChange = alphaForOffsetMax - alphaForOffsetZero;
    return alphaForOffsetZero + alphaChange * scrollValue;
}
网友答案:

if widths of your views are equal you can try this "formula":

button1.alpha = (scrollView.contentOffset.x / (scrollView.bounds.size.width/2) * 0.7) + 0.3
button2.alpha = 1 - button1.alpha + 0.3
相关阅读:
Top