码迷,mamicode.com
首页 > 其他好文 > 详细

scrollView中内部控件的悬停

时间:2016-05-08 11:51:28      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

以下图为例,图片,红色view和蓝色view是添加在scrollView上的,向上拖动,红色view停留在屏幕顶端不动,其它的继续滚动,向下拖动后,红色view跟着下来

技术分享

代码如下:(注意的是scrollView中自动布局的设置问题)

技术分享
 1 #import "ViewController.h"
 2 @interface ViewController () <UIScrollViewDelegate>
 3 @property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
 4 @property (weak, nonatomic) IBOutlet UIView *blueView;
 5 @property (weak, nonatomic) IBOutlet UIView *redView;
 6 @property (weak, nonatomic) IBOutlet UIImageView *imageView;
 7 @end
 8 @implementation ViewController
 9 - (void)viewDidLoad {
10     [super viewDidLoad];
11     self.scrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(self.blueView.frame));
12 }
13 
14 - (void)scrollViewDidScroll:(UIScrollView *)scrollView
15 {
16     CGFloat imageH = self.imageView.frame.size.height;
17     CGFloat offsetY = scrollView.contentOffset.y;
18     if (offsetY >= imageH) {
19         //将红色控件添加到控制器View中
20         CGRect redFrame = self.redView.frame;
21         redFrame.origin.y = 0;
22         self.redView.frame = redFrame;
23         [self.view addSubview:self.redView];
24     }else {
25         //将红色控件添加到控制器scrollView中
26         CGRect redFrame = self.redView.frame;
27         redFrame.origin.y = 140;
28         self.redView.frame = redFrame;
29         [self.scrollView addSubview:self.redView];
30     }
31     
32 //    if (offsetY < 0) {
33 //        CGFloat scale = 1 - (offsetY / 80);
34 //        self.imageView.transform = CGAffineTransformMakeScale(scale, scale);
35 //    }
36     
37     CGFloat scale = 1 - (offsetY / 20);
38     scale = (scale >= 1) ? scale : 1;
39     self.imageView.transform = CGAffineTransformMakeScale(scale, scale);
40 }
41 @end
技术分享

 

scrollView中内部控件的悬停

标签:

原文地址:http://www.cnblogs.com/ritian/p/5470109.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!