尝试以下操作:
@interface ViewController () <UIScrollViewDelegate>
@property (nonatomic, weak) IBOutlet UILabel *fadingLabel;
@end
@implementation ViewController
#pragma mark - UIScrollViewDelegate
/* For this method to get called, make sure you set the delegate of your table view to this view controller.*/
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (!self.fadingLabel)
return;
CGFloat labelHeight = CGRectGetHeight(self.fadingLabel.frame);
CGFloat alpha = 1.0f - (scrollView.contentOffset.y / labelHeight);
[self.fadingLabel setAlpha:alpha];
}
@end
以下是快速翻译:
class ViewController: UIViewController, UIScrollViewDelegate {
private weak var fadingLabel: UILabel?
// MARK: - UIScrollViewDelegate
/* For this method to get called, make sure you set the delegate of your table view to this view controller.*/
func scrollViewDidScroll(_ scrollView: UIScrollView) {
guard let label = self.fadingLabel else {
return
}
let labelHeight: CGFloat = label.frame.height
let alpha: CGFloat = 1.0 - (scrollView.contentOffset.y / labelHeight)
label.alpha = alpha
}
}