循环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];

    

    

 

}

Cocos Creator中的ScrollView组件默认是不支持自动循环滑动的,但是我们可以通过一些技巧来实现这个功能。 首先,我们可以在ScrollView的content节点中添加两个相同的子节点,一个在另一个的左边或上方,然后让content节点的宽度或高度设置为原来的两倍,使得两个子节点可以完全显示出来。 接下来,我们需要监听ScrollView的滑动事件,当用户滑动到一个子节点的边缘时,将ScrollView的content节点的位置重设到另一个子节点的位置,实现无缝连接的循环滑动效果。 具体的代码实现可以参考以下例子: 1. 创建两个相同的子节点NodeA和NodeB,设置它们的位置和内容。 2. 创建一个ScrollView节点,并将NodeA和NodeB添加到ScrollView的content节点中。 3. 监听ScrollView的scrolling事件,获取滑动的偏移量。 4. 判断滑动方向和边界条件,当滑动超过一个子节点的边缘时,根据滑动方向将content节点的位置设置为另一个子节点的位置。 示例代码如下: ```javascript // 在组件的onLoad函数中添加以下代码 cc.Class({ extends: cc.Component, properties: { scrollView: cc.ScrollView, nodeA: cc.Node, nodeB: cc.Node }, onLoad: function () { this.scrollView.node.on('scrolling', this.onScrolling, this); }, onScrolling: function (event) { // 获取ScrollView的滑动偏移量 var offsetX = this.scrollView.getScrollOffset().x; var offsetY = this.scrollView.getScrollOffset().y; // 如果滑动超过一个子节点的边缘 if (offsetX >= this.nodeA.width || offsetY >= this.nodeA.height) { // 重设content节点的位置到另一个子节点的位置 this.scrollView.scrollToLeft(); // 或者使用下面的代码将content节点的位置重设到另一个子节点的位置 // this.scrollView.scrollToOffset(cc.v2(this.nodeA.width, this.nodeA.height)); } }, }); ``` 通过以上方法,我们可以实现ScrollView的自动循环滑动效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值