在每个 iCarousal 视图上创建 Web 视图和按钮时遇到问题 [英] Trouble in creating Webview and Button on each iCarousal View

查看:20
本文介绍了在每个 iCarousal 视图上创建 Web 视图和按钮时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 iCarousel加载 youtube 视频.当我们滚动 iCarousel 时,会加载相应的 youtube 网址并播放视频.为此,我使用 webview 来加载 url.我还需要在每个轮播视图上有一个 按钮,这样我就可以在单击相应轮播视图时执行一个操作,即使当用户点击轮播视图它应该响应操作.

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.

轮播方法:

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

为此目的,我使用了轮播下方的一个按钮.当我使用 webview 按钮下方的按钮时,它没有响应.当我使用滚动视图上方的按钮时,按钮可以工作,但是当我们拖动中央视图时,轮播没有滚动的旋转木马.

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.

所以请建议我应该怎么做?我需要的只是在点击相应的轮播视图并带有 webview 时的一个操作.

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.

推荐答案

向图像视图添加点击手势(并确保在图像视图上设置 userInteractionEnabled = YES).这比使用会干扰其他交互的按钮更简单.

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.

这篇关于在每个 iCarousal 视图上创建 Web 视图和按钮时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆