我正在使用iCarousal,我必须在我的iCarousal视图中使用三个webview,五个imageview和两个Labels共10个iCarousal视图。我需要做的是根据标签获取标签,webviews和imageview在下面的方法中,当iCarousel移动时改变它们的值。
- (void)carouselCurrentItemIndexDidChange:(iCarousel *)objcarousel
我通过objcarousel.currentItemIndex
获取iCarousel的当前索引,但是当我使用标记获取视图时,我无法获取当前视图。它提供了第二个下一个视图。
代码:
- (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。
答案 0 :(得分:6)
您应该使用itemViewAtIndex:
和currentItemIndex
来获取单个项目视图。然后,在该视图上使用viewWithTag:
。
问题是viewWithTag:
搜索所有子视图并返回第一个匹配项。如果您在轮播本身上运行它,那么您无法保证首先找到哪个视图并返回。
UIWebView *WEB = (UIWebView *)[[carousel1 itemViewAtIndex:carousel1.currentItemIndex] viewWithTag:2];