4

我正在使用 iCarousal,其中我必须在 iCarousal 视图上使用三个 webview、五个 imageview 和两个标签,总共 10 个 iCarousal 视图。我需要做的是根据下面的标签获取标签、webview 和 imageview方法并在 iCarousel 移动时更改它们的值。

- (void)carouselCurrentItemIndexDidChange:(iCarousel *)objcarousel

我正在获取 iCarousel 的当前索引,objcarousel.currentItemIndex但是当我获取带有标签的视图时,我无法获取当前视图。它提供了第二个下一个视图。

代码:

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    return MainIndexArray.count;
}

- (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel1
{

    if(carousel1.currentItemIndex<3)
    {

    }
    else
    {
        UIWebView *WEB=(UIWebView *) [carousel1 viewWithTag:2];
        NSLog(@"WEB====%@",WEB);

        [WEB stopLoading];

        static NSString *youTubeVideoHTML = @"<!DOCTYPE html><html><head><style>body{margin:0px 0px 0px 0px;}</style></head> <body> <div id=\"player\"></div> <script> var tag = document.createElement('script'); tag.src = \"http://www.youtube.com/player_api\"; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var player; function onYouTubePlayerAPIReady() { player = new YT.Player('player', { width:'%0.0f', height:'%0.0f', videoId:'%@', events: { 'onReady': onPlayerReady, } }); } function onPlayerReady(event) { event.target.playVideo(); } </script> </body> </html>";

        NSLog(@"currentItemIndex=====%d",carousel1.currentItemIndex);

        NSString *html = [NSString stringWithFormat:youTubeVideoHTML, WEB.frame.size.width, WEB.frame.size.height,[YouTubeUrlArray objectAtIndex:0]];

        [WEB loadHTMLString:html baseURL:[[NSBundle mainBundle] resourceURL]];

    } 
}


- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    UILabel *label = nil;
    UIWebView *web=nil;

    //create new view if no view is available for recycling
    if (view == nil)
    {
        view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 280, 200.0f)];
        ((UIImageView *)view).image = [UIImage imageNamed:@"page.png"];
        view.contentMode = UIViewContentModeCenter;

        if(index<3)
        {
            label = [[UILabel alloc] initWithFrame:view.bounds];
            label.backgroundColor = [UIColor grayColor];
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [label.font fontWithSize:50];
            label.tag = 1;
            [view addSubview:label];

        }
        else
        {
            web=[[UIWebView alloc]initWithFrame:view.bounds];
            web.tag=2;
            [view addSubview:web];
        }
    }
    else
    {
        //get a reference to the label in the recycled view

        if(index<3)
        {
            label = (UILabel *)[view viewWithTag:1];
        }
        else
        {
            web=(UIWebView*) [view viewWithTag:2];
        }
    }


    if(index<3)
    {
        label.text = [MainIndexArray objectAtIndex:index];
    }
    else
    {
        [web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];
    }

    return view;
}

所以请建议我在 iCarousel 滚动时应该怎么做才能获取当前标签或当前 webview 或当前 imageview。

4

1 回答 1

6

您应该使用itemViewAtIndex:withcurrentItemIndex来获取单个项目视图。然后,viewWithTag:在该视图上使用。

问题是viewWithTag:搜索所有子视图并返回第一个匹配项。如果您在轮播本身上运行它,则无法保证首先找到并返回哪个视图。

UIWebView *WEB = (UIWebView *)[[carousel1 itemViewAtIndex:carousel1.currentItemIndex] viewWithTag:2];
于 2014-04-03T11:28:04.613 回答