思路: scrollview pageControl开启,利用三张图片实现轮播 ,始终显示中间view,通过左右滑动改变两边图片即可 中间始终未当前滑动到的图片
#import"ViewController.h"
#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)//屏幕宽度
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)//屏幕高度
@interfaceViewController()
@property(nonatomic,strong)UIScrollView*rootScrollView;
@property(nonatomic,assign)NSUIntegercurrentIndex;//从0开始
@property(nonatomic,strong)NSArray*imgNames;
@property(nonatomic,strong)NSMutableArray*imgViewsArray;//存放三个imgView
@end
@implementationViewController
{
CGFloatfrginX;
}
- (void)viewDidLoad {
[superviewDidLoad];
_imgViewsArray=[[NSMutableArrayalloc]init];
_imgNames=[[NSArrayalloc]initWithObjects:@"ez.png",@"jie.png",@"nj.png",@"xiazi.png",nil];
self.navigationController.navigationBarHidden=YES;
[selfinitUI];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)initUI{
_currentIndex=0;
_rootScrollView=[[UIScrollViewalloc]initWithFrame:CGRectMake(0,140,SCREEN_WIDTH,280)];
_rootScrollView.backgroundColor=[UIColorredColor];
_rootScrollView.pagingEnabled=YES;
_rootScrollView.delegate=self;
_rootScrollView.contentSize=CGSizeMake(SCREEN_WIDTH*4,0);
[self.viewaddSubview:_rootScrollView];
[selfaddImageArray];
}
-(void)addImageArray{
for(NSUIntegeri=0; i<3;i++) {
UIImageView*imgView= [[UIImageViewalloc]initWithFrame:CGRectMake(SCREEN_WIDTH*i,0,SCREEN_WIDTH,280)];
imgView.userInteractionEnabled=YES;
imgView.tag=100+i;
UILabel*lab=[[UILabelalloc]initWithFrame:CGRectMake((SCREEN_WIDTH*(i+1))-180,200,40,40)];
lab.text=[NSStringstringWithFormat:@"%ld",i+1];
lab.textColor=[UIColorblackColor];
lab.backgroundColor=[UIColorblueColor];
lab.tag=100+i;
[_rootScrollViewaddSubview:imgView];
// [_rootScrollView addSubview:lab];
[_imgViewsArrayaddObject:imgView];
}
//scrollview滚动到中间位置
_rootScrollView.contentOffset=CGPointMake(SCREEN_WIDTH,0);
[selfloadData];
}
-(void)loadData{
UIImageView*midImg=_imgViewsArray[1];//中间imageview
midImg.image=[UIImageimageNamed:_imgNames[_currentIndex]];
UIImageView*leftImg=_imgViewsArray[0];//左边imageview
UIImageView*rightImg=_imgViewsArray[2];//右边imageview
if(_currentIndex==0) {
//第一张
leftImg.image=[UIImageimageNamed:_imgNames[_imgNames.count-1]];
rightImg.image=[UIImageimageNamed:_imgNames[_currentIndex+1]];//右边
}elseif(_currentIndex==_imgNames.count-1){
//最后一张
leftImg.image=[UIImageimageNamed:_imgNames[_currentIndex-1]];
rightImg.image=[UIImageimageNamed:_imgNames[0]];//右边
}else{
//其它
leftImg.image=[UIImageimageNamed:_imgNames[_currentIndex-1]];
rightImg.image=[UIImageimageNamed:_imgNames[_currentIndex+1]];//右边
}
}
/**************scrollview delegate*****************/
-(void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView{
if(frginX
NSLog(@"左划+1");
if(_currentIndex==_imgNames.count-1) {
_currentIndex=0;
}else{
_currentIndex++;
}
}
if(frginX>scrollView.contentOffset.x) {
NSLog(@"右划-1");
if(_currentIndex==0) {
_currentIndex=_imgNames.count-1;
}else{
_currentIndex--;
}
}
scrollView.contentOffset=CGPointMake(SCREEN_WIDTH,0);
[selfloadData];
}
-(void)scrollViewWillBeginDragging:(UIScrollView*)scrollView{
frginX=scrollView.contentOffset.x;
}