我正在使用iCarousal,我必须在我的iCarousal视图中使用三个webview,五个imageview和两个Labels共10个iCarousal视图.我需要做的是根据下面的标记获取标签,webviews和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 = @" "; 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.
您应该使用itemViewAtIndex:
with currentItemIndex
来获取单个项目视图.然后,viewWithTag:
在该视图上使用.
问题是viewWithTag:
搜索所有子视图并返回第一个匹配.如果您在旋转木马本身上运行它,那么您无法保证首先找到哪个视图并返回.
UIWebView *WEB = (UIWebView *)[[carousel1 itemViewAtIndex:carousel1.currentItemIndex] viewWithTag:2];