#import "ViewController.h" @interface ViewController () <UIScrollViewDelegate> /** 价格view */ @property (nonatomic, weak) UIView *priceView; @property (weak, nonatomic) IBOutlet UISwitch *s; @end @implementation ViewController static CGFloat const XMGImageH = 100; - (void)viewDidLoad { [super viewDidLoad]; // 滚动 UIScrollView *scrollView = [[UIScrollView alloc] init]; scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, 400); scrollView.backgroundColor = [UIColor redColor]; scrollView.contentSize = CGSizeMake(0, 600); scrollView.delegate = self; [self.view addSubview:scrollView]; // 图片 UIImageView *imageView = [[UIImageView alloc] init]; imageView.frame = CGRectMake(0, 0, self.view.frame.size.width, XMGImageH); imageView.backgroundColor = [UIColor greenColor]; [scrollView addSubview:imageView]; // 需要悬停的控件 UIView *priceView = [[UIView alloc] init]; priceView.frame = CGRectMake(0, XMGImageH, self.view.frame.size.width, 40); priceView.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.5]; [scrollView addSubview:priceView]; self.priceView = priceView; UIView *otherView = [[UIView alloc] init]; otherView.frame = CGRectMake(0, 140, self.view.frame.size.width, 40); otherView.backgroundColor = [UIColor yellowColor]; //[scrollView addSubview:otherView]; } #pragma mark - <UIScrollViewDelegate> - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView.contentOffset.y >= XMGImageH) { CGRect priceF = self.priceView.frame; priceF.origin = CGPointZero; self.priceView.frame = priceF; [self.view addSubview:self.priceView]; // 往上移动多少距离, 开关就完全透明 CGFloat maxDelta = 20; // 往上移动的距离 CGFloat delta = scrollView.contentOffset.y - XMGImageH; // 减小alpha self.s.alpha = 1 - delta / maxDelta; } else { CGRect priceF = self.priceView.frame; priceF.origin.y = XMGImageH; self.priceView.frame = priceF; [scrollView addSubview:self.priceView]; } } @end