【问题标题】:How to change alpha value along with scrolling如何在滚动时更改 alpha 值
【发布时间】:2014-08-12 11:40:28
【问题描述】:

我找不到这种棘手问题的任何地方。我的问题是当用户开始滚动时我需要更改 alpha 值。在滚动开始时 alpha 值应该是 1,然后在滚动 alpha 值中间应该是0.5,最后它必须是 0。这是我需要做的。我无法通过谷歌搜索找到。请帮帮我

【问题讨论】:

  • 我已经用几个图像实现了滚动视图..我还有另一个图像视图。我试图通过滚动来改变 alpha 值
  • 怎么样?告诉我们你使用了什么代码

标签: ios objective-c uiscrollview


【解决方案1】:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    /* This is the offset at the bottom of the scroll view. */
    CGFloat totalScroll = scrollView.contentSize.height - scrollView.bounds.size.height;

    /* This is the current offset. */
    CGFloat offset = - scrollView.contentOffset.y;

    /* This is the percentage of the current offset / bottom offset. */
    CGFloat percentage = offset / totalScroll;

    /* When percentage = 0, the alpha should be 1 so we should flip the percentage. */
    scrollView.alpha = (1.f - percentage);
}

【讨论】:

    【解决方案2】:

    这是在 Swift 中的操作方法,我的控制器中有 2 个 UILabel,我需要增加第一个 UILabel 的 alpha 并减少第二个的 alpha:

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        if (scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height)) {
            // Bottom
            
        }
    
        if (scrollView.contentOffset.y < 0){
            // Top
            
            lblTitleHeader.alpha = 0
            
            lblTitleBody.alpha = 1
            
        }
    
        if (scrollView.contentOffset.y >= 0 && scrollView.contentOffset.y < (scrollView.contentSize.height - scrollView.frame.size.height)){
            // Middle
            
            let percentage: CGFloat = (scrollView.contentOffset.y) / 20
            
            // This label loses alpha when you scroll down (or slide up)
            lblTitleHeader.alpha = (percentage) 
            
    
            // This label gets more alpha when you scroll up (or slide down)
            lblTitleBody.alpha = (1 - percentage) 
            
            
            
        }
        
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-30
      • 1970-01-01
      • 2020-12-21
      • 1970-01-01
      相关资源
      最近更新 更多