//定义宏 #define WIDTH (self.view.frame.size.width) #define HEITH 200 @interface RootViewController ()<UIScrollViewDelegate> @property (nonatomic,retain) UIScrollView *scrollView; @property (nonatomic,retain) UIImageView *imageView; @end @implementation RootViewController // 创建UIScrollView - (void)create { _scrollView = [[UIScrollViewalloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height)]; //控制只能上下移动 _scrollView.contentSize =CGSizeMake(0,1000); // 添加到view上 [self.viewaddSubview:_scrollView]; //设置背景色为亮灰色 _scrollView.backgroundColor = [UIColorlightGrayColor]; // 释放 [_scrollView release]; // 创建UIImageView _imageView = [[UIImageViewalloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, 200)]; // 给_imageView 一张图片 _imageView.image = [UIImageimageNamed:@"lufei"]; // 把_imageView加到_scrollView [_scrollView addSubview:_imageView]; // 设置代理人 _scrollView.delegate =self; [_imageView release]; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { // 偏移量y的变化 CGFloat dy = scrollView.contentOffset.y; NSLog(@"%f", dy); // 判断拉倒方向 if (dy < 0) { // 利用公式 _imageView.frame =CGRectMake(-(-dy * (WIDTH /HEITH)) / 2, dy,WIDTH - dy * (WIDTH /HEITH), HEITH - dy); } } - (void)viewDidLoad { [superviewDidLoad]; // Do any additional setup after loading the view. [selfcreate]; } @end
© 著作权归作者所有
文章评论(0)