循环scrollview的例子。

濮阳钟展
2023-12-01

 // 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];

    

    

 

}

 类似资料: