zoukankan      html  css  js  c++  java
  • cocos2dx IOS 使用UIWebView来载入网页

    
    
    //  WebViewIOS.h
    //  evilcard
    //
    //  Created by keltonxian on 6/18/14.
    //
    //
    
    #ifndef __WEBVIEWIOS_H__
    #define __WEBVIEWIOS_H__
    
    #import <UIKit/UIKit.h>
    class WebViewIOS;
    
    @interface SocialView : UIView <UIWebViewDelegate> {
        UIWebView *webView;
        UIToolbar *toobar;
        UIView *spinner;
        CGRect frameSpinner;
        BOOL isSending;
    }
    
    @property (nonatomic) WebViewIOS *parentLayer;
    
    + (void)openURL:(NSString *)url rect:(CGRect)rect parent:(WebViewIOS *)parent;
    - (void)sendRequest;
    - (void)sendRequest:(NSString *)url;
    - (void)backClicked:(id)sender;
    
    @end
    
    #include "cocos2d.h"
    
    USING_NS_CC;
    
    class WebViewIOS : public CCLayer {
    public:
        CREATE_FUNC(WebViewIOS);
        void openURL(const char *url);
        void callbackClickBack();
    protected:
        virtual bool init();
        WebViewIOS(void);
        ~WebViewIOS(void);
        virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
        virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
        virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
        virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);
        virtual void keyBackClicked();
        
    };
    
    
    #endif
    //
    //  WebViewIOS.h
    //
    //  Created by keltonxian on 6/18/14.
    //
    //
    
    #include "WebViewIOS.h"
    #import "EAGLView.h"
    
    @implementation SocialView
    
    @synthesize parentLayer;
    
    + (void)openURL:(NSString *)url rect:(CGRect)rect parent:(WebViewIOS *)parent
    {
        SocialView *socialView = [[SocialView alloc] initWithFrame:rect];
        socialView.parentLayer = parent;
        [[EAGLView sharedEGLView] addSubview:socialView];
        [socialView sendRequest:url];
    }
    
    - (id)initWithFrame:(CGRect)frame {
    	self = [super initWithFrame:frame];
    	if (self == nil) {
    		return self;
    	}
        float toolBarheight = 35;
        webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, toolBarheight, frame.size.width, frame.size.height-toolBarheight)];
    	webView.opaque = NO;
    	webView.backgroundColor = [UIColor clearColor];
    	[self addSubview:webView];
    	[webView release];
        webView.delegate = self;
        
        toobar = [[UIToolbar alloc] init];
        [toobar setFrame:CGRectMake(0, 0, frame.size.width, toolBarheight)];
        toobar.barStyle = UIBarStyleBlackOpaque;
        UIBarButtonItem *mBackButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(backClicked:)];
        [toobar setItems:[NSArray arrayWithObjects:mBackButton,nil] animated:YES];
        [self addSubview:toobar];
        [toobar release];
        
        spinner = nil;
        frameSpinner = CGRectMake(0, toolBarheight, frame.size.width, frame.size.height-toolBarheight);
        
        return self;
    }
    
    - (void)backClicked:(id)sender {
        [self removeFromSuperview];
        parentLayer->callbackClickBack();
    }
    
    - (void)dealloc {
        [super dealloc];
    }
    
    - (void)sendRequest {
        
    }
    
    - (void)sendRequest:(NSString *)url {
        NSURL *u = [NSURL URLWithString:url];
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:u cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
    	[webView loadRequest:requestObj];
    	[webView setContentMode:UIViewContentModeScaleToFill];
    }
    
    - (void)webViewDidStartLoad:(UIWebView *)webView {
        NSLog(@"webView start load");
        if (nil != spinner) {
            [spinner removeFromSuperview];
        }
        spinner = [[UIView alloc] initWithFrame:frameSpinner];
        UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    	[indicator setCenter:CGPointMake(frameSpinner.size.width/2, frameSpinner.size.height/2)];
        [spinner addSubview:indicator];
        [indicator release];
    	[indicator startAnimating];
    	[self addSubview:spinner];
    	[spinner release];
    }
    
    - (void)webViewDidFinishLoad:(UIWebView *)webView {
        NSLog(@"webView finish load");
        [spinner removeFromSuperview];
        spinner = nil;
    }
    
    - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
        [spinner removeFromSuperview];
        spinner = nil;
        NSLog(@"%@
    %d
    %@", error, [error code], [error domain]);
        NSString *msg = [error description];
        UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"IOS Alert" message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [msgbox autorelease];
        [msgbox show];
    }
    
    @end
    
    WebViewIOS::WebViewIOS(void)
    {
        
    }
    
    WebViewIOS::~WebViewIOS(void)
    {
        
    }
    
    bool WebViewIOS::init()
    {
        if ( !CCLayer::init() )
        {
            return false;
        }
        
        setTouchEnabled(true);
        setTouchMode(kCCTouchesOneByOne);
        setTouchPriority(-0x7ffffff0); // MAX_INT 0x7fffffff
        setKeypadEnabled(true);
        
        return true;
    }
    
    void WebViewIOS::callbackClickBack()
    {
        this->removeFromParentAndCleanup(true);
    }
    
    void WebViewIOS::openURL(const char *url)
    {
        CCEGLView *openglView = CCEGLView::sharedOpenGLView();
        CCSize size = openglView->getFrameSize();
        CGRect rect = CGRectMake(0, 0, size.width, size.height);
        [SocialView openURL:[NSString stringWithCString:url encoding:NSASCIIStringEncoding] rect:rect parent:this];
    }
    
    bool WebViewIOS::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) {
        return true;
    }
    
    void WebViewIOS::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent) {
    }
    
    void WebViewIOS::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) {
    }
    
    void WebViewIOS::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent) {
    }
    
    void WebViewIOS::keyBackClicked() {
    }


    不浪费时间。直接先上代码。没写凝视,反正功能比較简单,调用时这样即可啦:

    CCScene *scene = CCDirector::sharedDirector()->getRunningScene();
        EvilWebViewIOS *webView = EvilWebViewIOS::create();
        scene->addChild(webView, 0x7ffffff0); // MAX_INT 0x7fffffff
        webView->openURL(pszUrl);



  • 相关阅读:
    Please check logcat output for more details
    如何移植freertos
    依赖: nginx-common (= 1.14.0-0ubuntu1) 但是它将不会被安装
    错误:22 http://ppa.launchpad.net/fkrull/deadsnakes/ubuntu bionic Release 404 Not Found [IP: 91.189.95.83 80]
    由于没有公钥,无法验证下列签名:
    jQuery的TAB切换+定时器
    CSS问题1:div中 li宽度不固定 ie6和ie7不兼容不自动换行
    (转)Sqlite中INTEGER PRIMARY KEY AUTOINCREMENT和rowid的使用
    (转)JS加载顺序
    (转)在网页中JS函数自动执行常用三种方法
  • 原文地址:https://www.cnblogs.com/mqxnongmin/p/10820921.html
Copyright © 2011-2022 走看看