zoukankan      html  css  js  c++  java
  • 控件悬停

    #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
  • 相关阅读:
    node express 上传文件
    [Java] 对象转型-01
    [Java] 类的Equals方法 (String, Data类都已经自动重写)
    editor does not contain a main type" 错误解决方
    Ubuntu网络连接图标消失解决方法
    [面试] 从尾到头打印链表-递归实现
    C++继承的例子 (1)
    国内访问gmail
    [python] 第7章 函数 第8章 模块
    Devcpp(Dev C++)使用说明及技巧
  • 原文地址:https://www.cnblogs.com/developer-ios/p/4934762.html
Copyright © 2011-2022 走看看