我找不到任何针对这种粘性问题的地方。我的问题是当用户开始滚动时我需要更改alpha值。滚动alpha值的开始应为1,然后在滚动alpha值的中间应该是0.5,最后它必须是0.这是我需要做的。我用谷歌搜索找不到。帮帮我PLZ
答案 0 :(得分:21)
- (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);
}
答案 1 :(得分:0)
这是如何在 Swift 中执行此操作,我的控制器中有 2 个 UILabel
,我需要增加第一个 UILabel
的 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)
}
}
的 alpha第二个:
{"assembly":"45","Lot Number":"2021 109 M 21 - 1","Expiry date":"12/05/2023","Quantity":"682"}