循环scrollview的例子。

 // ads

    _adsScrollView = [[UIScrollViewalloc] initWithFrame:CGRectMake(0.0f, 0.0f, kMainAdWidth, kMainAdHeight)];

    _adsScrollView.pagingEnabled = YES;

    _adsScrollView.bounces = NO;

    _adsScrollView.showsHorizontalScrollIndicator = NO;

    _adsScrollView.delegate = self;

    UITapGestureRecognizer *tapAds = [[UITapGestureRecognizeralloc] initWithTarget:self

                                                                             action:@selector(clickedAds)];

    [_adsScrollViewaddGestureRecognizer:tapAds];

    [self.viewaddSubview:_adsScrollView];

    

    _adsPageControl = [[UIPageControlalloc] initWithFrame:CGRectMake(0.0f, 134.0f, APPBOUND.size.width, 8.0f)];

    _adsPageControl.hidesForSinglePage = NO;

    _adsPageControl.pageIndicatorTintColor = [UIColorcolorWithHexString:@"#bcddf0"alpha:1.0f];

    _adsPageControl.currentPageIndicatorTintColor = [UIColorwhiteColor];

    [self.viewaddSubview:_adsPageControl];

    

    

    

    [SJBAdModelrequestAdListOnSucceed:^(id data) {

        kNSLog(@"data === %@",data);

        self.adsData = data;

        [self loadAds];

    } onFailure:^{

        kNSLog(@"data ===");

        self.adsData = [SJBAdModeladList];

        [self loadAds];

 

    }];

 

 

 

#pragma mark ==================添加轮播广告图======================

 

#pragma mark - UIScrollViewDelegate

//- (void)scrollViewDidScroll:(UIScrollView *)scrollView

//{

//    if ( scrollView == _adsScrollView ){

//        self.adsPageControl.currentPage = (int)(scrollView.contentOffset.x / kMainAdWidth) - 1;

//    }

//}

 

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    if ( scrollView == _adsScrollView ){

        NSInteger adsCount = [self.adsData count];

        int currentPage = (int)floor(scrollView.contentOffset.x / scrollView.frame.size.width);

        if (currentPage == 0) {

            [scrollView scrollRectToVisible:CGRectMake(kMainAdWidth * (adsCount),0,kMainAdWidth,kMainAdHeight) animated:NO];

        } else if ( currentPage == adsCount+1 ) {

            [scrollView scrollRectToVisible:CGRectMake(kMainAdWidth, 0.0f, kMainAdWidth,kMainAdHeight) animated:NO];

        }

    }

}

 

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

{

    if ( scrollView == _adsScrollView ){

        if ([_adsAutoScrollTimerisValid]) {

            [_adsAutoScrollTimerinvalidate];

        }

    }

}

 

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset

{

    if ( scrollView == _adsScrollView ){

        if (![_adsAutoScrollTimerisValid]) {

            _adsAutoScrollTimer = [NSTimerscheduledTimerWithTimeInterval:5.0

                                                                   target:self

                                                                 selector:@selector(autoScrollAds)

                                                                 userInfo:nil

                                                                  repeats:YES];

        }

    }

}

 

#pragma mark - Private Methods

 

- (void)loadAds

{

    NSInteger adsCount = [self.adsData count];

    self.adsScrollView.contentSize = CGSizeMake( (adsCount+2) * kMainAdWidth, kMainAdHeight);

    self.adsPageControl.numberOfPages = adsCount;

    if (adsCount == 0 ){

        return;

    }

    

    NSString *adImageUrl;

    for (int i=0; i<adsCount+2;i++){

        UIImageView *adImageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * APPBOUND.size.width, 0.0f, kMainAdWidth, kMainAdHeight)];

        [adImageView setContentMode:UIViewContentModeScaleAspectFill];

        if ( i==0 ){

            adImageUrl = [self.adsData[adsCount-1] objectForKey:@"img"];

        }else if ( i==(adsCount+1) ){

            adImageUrl = [self.adsData[0] objectForKey:@"img"];

        }else{

            adImageUrl = [self.adsData[i-1] objectForKey:@"img"];

        }

        [adImageView setImageWithURL:[NSURL URLWithString:adImageUrl]];

        [self.adsScrollView addSubview:adImageView];

    }

    self.adsScrollView.contentOffset = CGPointMake(kMainAdWidth, 0.0f);

    

    if (adsCount > 1){

        [_adsAutoScrollTimerinvalidate];

        _adsAutoScrollTimer = [NSTimerscheduledTimerWithTimeInterval:5.0

                                                               target:self

                                                             selector:@selector(autoScrollAds)

                                                             userInfo:nil

                                                              repeats:YES];

    }

}

 

- (void)autoScrollAds

{

    NSInteger adsCount = [self.adsData count];

    int currentPage = (int)floor(self.adsScrollView.contentOffset.x / self.adsScrollView.frame.size.width);

    if (currentPage == 0) {

        [self.adsScrollViewscrollRectToVisible:CGRectMake(kMainAdWidth * (adsCount),0,kMainAdWidth,kMainAdHeight) animated:NO];

    } else if ( currentPage == adsCount ) {

        [self.adsScrollViewscrollRectToVisible:CGRectMake(kMainAdWidth, 0.0f, kMainAdWidth,kMainAdHeight) animated:NO];

    }else{

        [UIViewanimateWithDuration:0.5fanimations:^{

            self.adsScrollView.contentOffset = CGPointMake(kMainAdWidth * (currentPage + 1), 0.0f);

        }];

    }

}

 

- (void)clickedAds

{

    int currentPage = (int)floor(self.adsScrollView.contentOffset.x / self.adsScrollView.frame.size.width);

    NSDictionary *adData = [self.adsData objectAtIndex:(currentPage-1)];

//    SJBHelperViewController *webVC = [SJBHelperViewController SJBHelperVConURL:[adData objectForKey:@"downUrl"]];

//    [self.navigationController pushViewController:webVC animated:YES];

    TKQWebViewController *webVC = [TKQWebViewControllerwebVCWithURL:[adData objectForKey:@"downUrl"]];

//    [self.navigationController pushViewController:webVC animated:YES];

    [selfpresentViewController:webVC animated:YEScompletion:nil];

    

    

 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值