• 控件悬停


    #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
  • 相关阅读:
    luogu P1613 跑路
    luogu P2047 社交网络
    luogu P2740 [USACO4.2]草地排水Drainage Ditches
    「字典树」最大异或对
    「贪心」耍杂技的牛
    「贪心」士兵
    「贪心」糖果传递
    「几何」[USACO12DEC]疯狂的栅栏Crazy Fences
    「LCA」[USACO10HOL]牛的政治Cow Politics
    「二分答案 + 前缀和」防线
  • 原文地址:https://www.cnblogs.com/developer-ios/p/4934762.html
Copyright © 2020-2023  润新知