I am using iCarousel for loading youtube videos
.When we scroll the iCarousel respective youtube url is loaded and it plays the video.For this purpose I am using webview to load the urls.I also need one button on each Carousel view so that I can implement one action when respective carousel view is clicked even when video is loading when user taps at the carousel view it should respond to action.
Carousel Methods:
Edit:
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
return 5;
}
- (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel1
{
UIWebView *WEBPreviuos = (UIWebView *)[[carousel1 itemViewAtIndex:previousIndex] viewWithTag:2];
[WEBPreviuos loadHTMLString:nil baseURL:[[NSBundle mainBundle] resourceURL]];
previousIndex=carousel1.currentItemIndex;
UIWebView *WEB = (UIWebView *)[[carousel1 itemViewAtIndex:carousel1.currentItemIndex] viewWithTag:2];
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>";
NSString *html = [NSString stringWithFormat:youTubeVideoHTML, WEB.frame.size.width, WEB.frame.size.height,@"5H9jnUqTXeQ"];
[WEB loadHTMLString:html baseURL:[[NSBundle mainBundle] resourceURL]];
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIWebView *youTubeWebView=nil;
//create new view if no view is available for recycling
if (view == nil)
{
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 280, 180)];
view.userInteractionEnabled=YES;
((UIImageView *)view).image = [UIImage imageNamed:@"page.png"];
view.contentMode = UIViewContentModeCenter;
youTubeWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 280, 180)];
//youTubeWebView.scalesPageToFit = YES;
youTubeWebView.backgroundColor=[UIColor blackColor];
youTubeWebView.opaque=NO;
youTubeWebView.tag=2;
youTubeWebView.userInteractionEnabled=YES;
youTubeWebView.mediaPlaybackRequiresUserAction=NO;
youTubeWebView.delegate = self;
[view addSubview:youTubeWebView];
UIButton *VideoButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
VideoButton.frame=CGRectMake(0, 0, 280, 180);
VideoButton.userInteractionEnabled=YES;
VideoButton.backgroundColor=[UIColor clearColor];
[VideoButton addTarget:self action:@selector(VideoButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[youTubeWebView addSubview:VideoButton];
[view bringSubviewToFront:youTubeWebView];
}
else
{
//get a reference to the label in the recycled view
youTubeWebView=(UIWebView*) [view viewWithTag:2];
}
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>";
NSString *html = [NSString stringWithFormat:youTubeVideoHTML, youTubeWebView.frame.size.width, youTubeWebView.frame.size.height,@"5H9jnUqTXeQ"];
if(index==0)
{
// label.text = [MainIndexArray objectAtIndex:index];
[youTubeWebView loadHTMLString:html baseURL:[[NSBundle mainBundle] resourceURL]];
}
else
{
[youTubeWebView loadHTMLString:nil baseURL:[[NSBundle mainBundle] resourceURL]];
}
return view;
}
I am using one button below the carousel for this purpose.When I use button below the webview button is not responding.When I use button above the scrollview then the button works but the carousel is not scrolling when we drag the central view of the carousel.
So please suggest me what should I do?What I need is just an action when a respective Carousel view is clicked with webview on it.