Trouble in creating Webview and Button on each iCarousal View

空扰寡人 提交于 2019-12-11 13:58:16

问题


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.


回答1:


Add a tap gesture to the image view (and be sure to set userInteractionEnabled = YES on the image view). This is simpler than using a button which will interfere with other interactions.

The gesture will also interfere, so you will need to update the carousel so that the gestures that it uses internally can work at the same time as the new gesture. This is done using the gesture delegate methods.



来源:https://stackoverflow.com/questions/23079646/trouble-in-creating-webview-and-button-on-each-icarousal-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!