UI 界面设计 滚动视图

#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.centerCGPointMake(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];

    }

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值