#import "ViewController.h"
#define SCROLL_L [UIScreen mainScreen].bounds.size.width
#define SCROLL_R [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView *scrollview;
@property(nonatomic,strong)UIPageControl *pageContol;
@property(nonatomic,retain)NSMutableArray *image;
@end
@implementation ViewController
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [superinitWithCoder:coder];
if (self) {
self.title =@"相册";
}
returnself;
}
- (void)viewDidLoad {
[superviewDidLoad];
self.automaticallyAdjustsScrollViewInsets =NO;
//滚动视图
_scrollview = [[UIScrollViewalloc]init];
_scrollview.bounds =CGRectMake(0,0, SCROLL_L,SCROLL_R);
_scrollview.center =self.view.center;
_scrollview.backgroundColor = [UIColor whiteColor];
_scrollview.contentSize =CGSizeMake(SCROLL_L *8,SCROLL_R);
_scrollview.pagingEnabled =YES;
_scrollview.directionalLockEnabled =YES;
_scrollview.showsHorizontalScrollIndicator =NO;
_scrollview.showsVerticalScrollIndicator =NO;
_scrollview.delegate =self;
[self.viewaddSubview:_scrollview];
_image= [NSMutableArrayarray];
for (NSInteger index =1; index<7; index++) {
NSString *filenname = [NSStringstringWithFormat:@"haiz%ld.jpg",index];
[_imageaddObject:filenname];
}
[_imageaddObject:@"haiz1副本.jpg"];
[_imageinsertObject:@"haiz6.jpg"atIndex:0];
for (NSInteger index=0; index<8; index++) {
UIImageView *imageBV = [[UIImageViewalloc]initWithFrame:CGRectMake(SCROLL_L*index,0, SCROLL_L,SCROLL_R)];
imageBV.userInteractionEnabled =YES;
imageBV.image = [UIImageimageNamed:_image[index]];
[_scrollviewaddSubview:imageBV];
}
//分页控制控件
_pageContol = [[UIPageControlalloc]init];
_pageContol.bounds =CGRectMake(0,0, SCROLL_L,30);
_pageContol.center = CGPointMake(CGRectGetMidX(self.view.bounds),CGRectGetMidY(self.view.bounds)+260);
_pageContol.numberOfPages =6;
_pageContol.currentPage=0;
_pageContol.currentPageIndicatorTintColor= [UIColorredColor];
[self.viewaddSubview:_pageContol];
//点击隐藏
UITapGestureRecognizer *tapGesturRecognizer = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(tapGes:)];
[_scrollviewaddGestureRecognizer:tapGesturRecognizer];
}
#pragma mark--------------UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
// //当处于真的第一张图片右滑动的时候,直接跳转到假的第一张
// NSInteger curreb=round(scrollView.contentOffset.x/SCROLL_L);
// _pageContol.currentPage = curreb;
NSLog(@"%f",scrollView.contentOffset.x
);
//处理pageControl
NSInteger currentPage = (NSInteger)round(scrollView.contentOffset.x /SCROLL_L);
if (scrollView.contentOffset.x >CGRectGetWidth(self.view.bounds) * 6) {
//在真的最后一张图片向左滑动
_pageContol.currentPage =0;
} elseif (scrollView.contentOffset.x <CGRectGetWidth(self.view.bounds )) {
//在真的第一张图片向右滑动
_pageContol.currentPage =5;
} else {
//正常情况
_pageContol.currentPage = currentPage -1;
}
//如果是在真正第一张图片向右滑动,则跳到假的的第一张图片
if (scrollView.contentOffset.x <CGRectGetWidth(self.view.bounds)) {
[scrollView setContentOffset:CGPointMake(CGRectGetWidth(self.view.bounds) * 7, 0) animated:NO];
//如果是在假的第一张张图片向左滑动,则跳到真正第一张图片
} elseif (scrollView.contentOffset.x >CGRectGetWidth(self.view.bounds) * 7) {
[scrollView setContentOffset:CGPointMake(CGRectGetWidth(self.view.bounds),0) animated:NO];
}
}
-(void)tapGes:(UITapGestureRecognizer *)sender{
if (self.navigationController.navigationBar.hidden) {
[self.navigationControllersetNavigationBarHidden:NOanimated:YES];
} else {
[self.navigationControllersetNavigationBarHidden:YESanimated:YES];
}
}