创建工程,创建一个UIScrollView属性,并遵循其协议;
#define kWidth self.view.frame.size.width//屏幕宽
#define kHeight self.view.frame.size.height//屏幕高
创建轮播图属性,(也可以直接用UIImageView)
@interface ViewController ()<UIScrollViewDelegate> @property (nonatomic, strong) UIScrollView *scrollV; @property (nonatomic, strong) CarouselFigureView *carouse; @end
在这里我用到了自己封装好的轮播图,感兴趣的可以下载下来看看效果,懒得下的,也可以直接将CarouselFigureView类(我封装的轮播图类)写成UIImageView就能看到效果
已封装轮播图的下载地址:http://download.csdn.net/detail/margaret_mo/9451381
还有轮播图类里,用到的头文件,下载地址:http://download.csdn.net/detail/margaret_mo/9469767
用轮播图的导入头文件:#import "CarouselFigureView.h"
设置UIScrollView,将轮播图加在ScrollView上
- (void)viewDidLoad { [super viewDidLoad]; self.scrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)]; self.scrollV.delegate = self; self.scrollV.backgroundColor = [UIColor cyanColor]; self.scrollV.contentSize = CGSizeMake(0, kHeight + 100); [self.view addSubview:self.scrollV]; self.carouse = [CarouselFigureView craouseFigureViewWithFrame:CGRectMake(0, 0, kWidth, kWidth) interval:5 style:CarouselFigureViewPicturesArrayStyleDefault]; self.carouse.picturesArray = @[@"1.png", @"2.png", @"3.png", @"4.png"]; [self.scrollV addSubview:self.carouse]; }
在UIScrollView的轮动事件里,根据scrollView的偏移量contentOffset改变轮播图的frame
#pragma mark 正在拖动 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat y = -scrollView.contentOffset.y; self.carouse.frame = CGRectMake(-y/2, -y, kWidth + y, kWidth + y); }
运行,下拉scrollView就能看到放大效果了,很简单的操作