首页 > 其他 > 详细

控件悬停

时间:2015-11-04 00:40:09      阅读:276      评论:0      收藏:0      [点我收藏+]
#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

 

控件悬停

原文:http://www.cnblogs.com/developer-ios/p/4934762.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!