I am using iCarousal in which I have to use three webviews,five imageviews and two Labels on my iCarousal views a total of 10 iCarousal views.What I need to do is fetch the labels,webviews and imageviews on the basis of tag in the below method and change their values when the iCarousel moves.

- (void)carouselCurrentItemIndexDidChange:(iCarousel *)objcarousel

I am getting the current index of the iCarousel by objcarousel.currentItemIndex but when I fetch the views with tag I am unable to get the current view.It gives the second next view.

Code:

- (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;
}

So please suggest me what should I do to get the current label or current webview or current imageview when iCarousel scrolls.

有帮助吗?

解决方案

You should be using itemViewAtIndex: with currentItemIndex to get the individual item view. Then, use viewWithTag: on that view.

The problem is that viewWithTag: searches all subviews and returns the first match. If you run it on the carousel itself then you have no guarantee of which view will be found first and returned.

UIWebView *WEB = (UIWebView *)[[carousel1 itemViewAtIndex:carousel1.currentItemIndex] viewWithTag:2];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top