UIScrollView 循环



@interface cycleScrollerViewController : UIViewController<UIScrollViewDelegate> {



UIScrollView *scView;



UIPageControl *pageCon;




UIImageView *mImgView; //显示当前页图片的主imageView


NSMutableArray *imgViewArray; //存储所有imageview的数组




int currentPage_; //当前页



int totalPages_; //总图片数量
}






@property (nonatomic, retain) IBOutlet UIScrollView *scView;




@property (nonatomic, retain) IBOutlet UIPageControl *pageCon;


@end







#import "cycleScrollerViewController.h"


@implementation cycleScrollerViewController




@synthesize scView;




@synthesize pageCon;




/*
// The designated initializer. Override to perform setup that is required before the view is loaded.




- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
   


 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/


/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.




- (void)loadView {
}
*/






// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.




- (void)viewDidLoad {
    [super viewDidLoad];




imgViewArray = [[NSMutableArray alloc] init];




mImgView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 280, 420)];



currentPage_ = 0;




totalPages_ = 6;


//将所有图片放入imageview后存入数组

for (int i = 0; i < totalPages_; i++) {



UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"image%d.jpg", i]];



UIImageView *imgView = [[UIImageView alloc] initWithImage:img];

[imgViewArray addObject:imgView];



[imgView release];
}

//主imageView显示第一张图片

mImgView.image = [[imgViewArray objectAtIndex:0] image];

[self.view addSubview:mImgView];



[self.view sendSubviewToBack:mImgView];




//设置scrollview的宽度,为3页内容



//当前scrollview位置为第2页



[self.scView setContentSize:CGSizeMake(320 * 3, 460)];

self.scView.contentOffset = CGPointMake(320, 0);

self.scView.delegate = self;





self.pageCon.numberOfPages = totalPages_;

self.pageCon.currentPage = 0;
}






/*
// Override to allow orientations other than the default portrait orientation.




- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/






- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}






- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}




- (void)dealloc {
    [super dealloc];
}








#pragma mark -
#pragma mark scroll view delegate


//通过scrollview委托来实现首尾相连的效果




- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {



    //开始滑动时,主imageview图片置空



     mImgView.image = nil;


   //scrollview上添加三个imageview



   //第一个imageview的图片为当前图片的前一张图片(如果当前图片为第一张则显示最后一张图片)




   //第二个imageview的图片为当前图片



   //第三个imageview的图片为当前图片的后一张图片(如果当前图片为最后一张则显示第一张图片)



   UIImageView *imView1 = [imgViewArray objectAtIndex:(currentPage_ == 0 ? (totalPages_ - 1) : (currentPage_ - 1))];



   UIImageView *imView2 = [imgViewArray objectAtIndex:currentPage_];

   UIImageView *imView3 = [imgViewArray objectAtIndex:(currentPage_ == (totalPages_ - 1) ? 0 : (currentPage_ + 1))];



[imView1 setFrame:CGRectMake(20.0, 20.0, 280.0, 420.0)];
[imView2 setFrame:CGRectMake(340.0, 20.0, 280.0, 420.0)];
[imView3 setFrame:CGRectMake(660.0, 20.0, 280.0, 420.0)];
[self.scView addSubview:imView1];



[self.scView addSubview:imView2];



[self.scView addSubview:imView3];
}






- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {


//scrollview结束滚动时判断是否已经换页



if (self.scView.contentOffset.x > 320.0) {


//如果是最后一张图片,则将主imageview内容置为第一张图片



//如果不是最后一张图片,则将主imageview内容置为下一张图片



if (currentPage_ < (totalPages_ - 1)) {



currentPage_ ++;



mImgView.image = ((UIImageView *)[imgViewArray objectAtIndex:currentPage_]).image;



else {



currentPage_ = 0;



mImgView.image = ((UIImageView *)[imgViewArray objectAtIndex:currentPage_]).image;
}





else 


if (self.scView.contentOffset.x < 320.0) {


//如果是第一张图片,则将主imageview内容置为最后一张图片



//如果不是第一张图片,则将主imageview内容置为上一张图片



if (currentPage_ > 0) {



currentPage_ --;



mImgView.image = ((UIImageView *)[imgViewArray objectAtIndex:currentPage_]).image;
}


 else {



currentPage_ = totalPages_ - 1;



mImgView.image = ((UIImageView *)[imgViewArray objectAtIndex:currentPage_]).image;
}





else {




//没有换页,则主imageview仍然为之前的图片



mImgView.image = ((UIImageView *)[imgViewArray objectAtIndex:currentPage_]).image;



NSLog(@"current offset :%f", self.scView.contentOffset.x);
}

//始终将scrollview置为第2页



[self.scView setContentOffset:CGPointMake(320.0, 0.0)];




//移除scrollview上的图片



for (UIImageView *theView in [self.scView subviews]) {

[theView removeFromSuperview];
}



self.pageCon.currentPage = currentPage_;




}


@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值