IOS写一个可以支持全屏的WebView

这样来写布局

一个TitleView作为顶部搜索栏:

@implementation TitleView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self initTilte];
    }
    return self;
}

-(void)initTilte{
    UITextField* field = [[UITextField alloc] initWithFrame:CGRectMake(20, 10, 230, 30)];
    _textField = field;
    _textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;//自动调整自己的位置,使自己的左边距和右边距和superview保持不变
    _textField.enablesReturnKeyAutomatically = YES;  //使return在输入时可以使用
    _textField.placeholder = @"please input:";     //设置hint的值
    _textField.textAlignment = NSTextAlignmentLeft;  //文字靠左显示
    _textField.borderStyle = UITextBorderStyleNone;   //没有边框
    _textField.font = [UIFont systemFontOfSize:18.0f];   //设置字体大小
    _textField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    _textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;  //这两个使文字会居中显示
    _textField.clearButtonMode = UITextFieldViewModeWhileEditing;  //当输入时有clear button
    UIImage* image = [[UIImage imageNamed:@"text_field_bg.png"] stretchableImageWithLeftCapWidth:20 topCapHeight:0];   //stretchableImageWithLeftCapWidth使图片有拉伸效果
    _textField.delegate = self; //给textField设置代理
    _textField.background = image;  //背景设置
    _textField.leftViewMode = UITextFieldViewModeAlways;
    [_textField setText:@"http://m.baidu.com"];
    CGRect frame = [_textField frame];
    frame.size.width = 15;
    UIView* view1 = [[UIView alloc] initWithFrame:frame];
    _textField.leftView = view1;  //上面几句话设置文字跟textField的左边有些距离

    [self addSubview:_textField];

    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom]; //init一个button,可以自定义背景
    [button setBackgroundImage:[[UIImage imageNamed:@"cancel_but_bg.png"] stretchableImageWithLeftCapWidth:20 topCapHeight:0] forState:UIControlStateNormal];
    button.frame = CGRectMake(260,10, 49, 30);  //设置button的背景,有normal和highlight两种状态
    [button setBackgroundImage:[[UIImage imageNamed:@"cancel_but_bg2.png"] stretchableImageWithLeftCapWidth:20 topCapHeight:0] forState:UIControlStateHighlighted];
    [button setTitleColor:[CCommon RGBColorFromHexString:@"#333333" alpha:1.0f] forState:UIControlStateNormal];

    [button setTitleColor:[CCommon RGBColorFromHexString:@"#000000" alpha:1.0f] forState:UIControlStateHighlighted];    [button addTarget:self action:@selector(onclick) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"搜索" forState:UIControlStateNormal];
    <strong> button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;//这个让button字体周边有自适应的margin</strong>

    [self addSubview:button];

    [button addTarget:self action:@selector(onclick) forControlEvents:UIControlEventTouchUpInside];

    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320,2)];
    view.backgroundColor = [UIColor grayColor];
    [self addSubview:view];

}

-(void)resignTextField{
    [_textField resignFirstResponder];
}

-(void)onclick{
    [_customUiWebViewController onclick];//让controller执行搜索页面的操作
}

viewcontroller的viewdidload:

- (void)viewDidLoad
{
    [super viewDidLoad];

    _titleView = [[TitleView alloc] initWithFrame:[[CustomUiWebViewUIManager sharedInstance] getSearchBarView]];
    _titleView.customUiWebViewController = self;
    [self.view addSubview:_titleView];

    // Do any additional setup after loading the view.
    CustomWidgetUiWebView* tView = [[CustomWidgetUiWebView alloc] initWithFrame:
                                    [[CustomUiWebViewUIManager sharedInstance] getWebViewFrameBySearchResultView]];
    self.customWidgetUiWebView = tView;

    [self.customWidgetUiWebView setOpaque:NO];
    [self.customWidgetUiWebView setBackgroundColor:[UIColor whiteColor]];
    [self.customWidgetUiWebView setDelegate:self];
    self.customWidgetUiWebView.scalesPageToFit = YES;
    self.customWidgetUiWebView.detectsPhoneNumbers = NO;

//    NSURL* url = [NSURL URLWithString:@"http://m.baidu.com"];
//    NSURLRequest* request = [NSURLRequest requestWithURL:url];
//    [self.customWidgetUiWebView loadRequest:request];

   <strong> NSArray* arr = [self.customWidgetUiWebView subviews];
    UIScrollView* sView = [arr objectAtIndex:0];
    [sView setOpaque:NO];
    [sView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"webview_bg.png"]]];
    //这个把webview的sub view的scrollview的透明度设为不透明,并加上背景
    sView.delegate = self;</strong>

    WebToolBarController* tmpWebToolBarController = [[WebToolBarController alloc] init];//这个是底下的toolbar
    self.webToolBarController = tmpWebToolBarController;

    [_webToolBarController loadWebViewToolBar];
    [self.view addSubview:_customWidgetUiWebView];
    _webToolBarController.webToolBar.frame = [[WebToolBarUIManager sharedInstance] webToolBarFrame];

    [self.view addSubview:_webToolBarController.webToolBar];
    self.webToolBarController.delegate = self;

 <strong>   [self startUpdateButtonStatusTimer]; //这个是起了一个timer来更新back和forward的状态</strong>

    CGRect iconRc = [[CustomUiWebViewUIManager sharedInstance] getFullScreenBtnFrame];
    UIButton* curBtn = [[UIButton alloc] initWithFrame:iconRc];
    [curBtn setImage:[UIImage imageNamed:@"fullScreen.png"] forState:UIControlStateNormal];
    [curBtn setImage:[UIImage imageNamed:@"fullScreen_touch.png"] forState:UIControlStateHighlighted];
    [curBtn addTarget:self action:@selector(fullScreenBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    curBtn.alpha = 0;
    self.fullScreenBtn = curBtn;
    [self.view addSubview:curBtn];

}
-(void)startUpdateButtonStatusTimer{
    [self freeUpdateButtonStatusTimer];
    NSDate* tmpDate = [[NSDate alloc] initWithTimeIntervalSinceNow:0.5];
    NSTimer* tmpTimer = [[NSTimer alloc] initWithFireDate:tmpDate interval:10.0 target:self selector:@selector(UpdateButtonStatusTimerFired:) userInfo:nil repeats:YES];  <strong>//0.5s以后执行<span style="font-family: Arial, Helvetica, sans-serif;">UpdateButtonStatusTimerFired,并且10.0s再重复执行</span></strong>

    self.updateStatusBarTimer = tmpTimer;
    [[NSRunLoop currentRunLoop] addTimer:_updateStatusBarTimer forMode:NSDefaultRunLoopMode];
}

- (void)UpdateButtonStatusTimerFired:(id)sender
{
	[self updateToobarButtonStatusByWebView];
	//[self freeUpdateButtonStatusTimer];
}

- (void)updateToobarButtonStatusByWebView{
    if (![_customWidgetUiWebView isLoading]) {
        return;
    }

    [_webToolBarController setItem:ITEM_BACK enabled:[_customWidgetUiWebView canGoBack]];
    [_webToolBarController setItem:ITEM_FORWARD enabled:[_customWidgetUiWebView canGoForward]];

}

如果执行全屏和离开全屏的操作:

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    CGFloat screenHeight = [UIScreen mainScreen].applicationFrame.size.height;
    NSArray* arr = [self.customWidgetUiWebView subviews];

    UIScrollView* sView = [arr objectAtIndex:0];
    if (sView.contentSize.height < screenHeight) { //如果webview的内容区域小于屏幕高度,则不执行全屏操作
        return;
    }

    if (self.customWidgetUiWebView.widgetEmbedStatus == ENoWidgetEmbed){
        NSLog(@"====y=%f",sView.contentOffset.y);
        if (sView.contentOffset.y >= SearchBar_Height &&
            sView.contentOffset.y < sView.contentSize.height-screenHeight) {//contentOffset<span class="s1" style="font-family: Arial, Helvetica, sans-serif;">是当前显示的区域的</span><span style="font-family: Arial, Helvetica, sans-serif;">origin</span><span class="s1" style="font-family: Arial, Helvetica, sans-serif;">相对于整个</span><span style="font-family: Arial, Helvetica, sans-serif;">scrollView</span><span class="s1" style="font-family: Arial, Helvetica, sans-serif;">的</span><span style="font-family: Arial, Helvetica, sans-serif;">origin</span><span class="s1" style="font-family: Arial, Helvetica, sans-serif;">的位置,如果大于SearchBar_Height,并且小于底部
</span>

            if (isFullScreenMode) {
                return;
            }else{
                [self goToFullScreenMode];
            }
        }else if(isFullScreenMode) {
            [self exitFullScreenMode];
        }

    }
}

-(void)goToFullScreenMode{
    [UIView beginAnimations:@"FullScreenMode" context:NULL];
    [UIView setAnimationDuration:0.5f];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

    _webToolBarController.webToolBar.frame = [[WebToolBarUIManager sharedInstance] webToolBarHideFrame];
    [UIView commitAnimations];

    _titleView.frame =[[CustomUiWebViewUIManager sharedInstance] getSearchBarViewFullView];

    _customWidgetUiWebView.frame = [[CustomUiWebViewUIManager sharedInstance] getWebViewFrameByFullScreenView];

    //shwo button
    [UIView beginAnimations:@"animationID" context:NULL];
    [UIView setAnimationDuration:0.5f];
    _fullScreenBtn.alpha = 1;
    [UIView commitAnimations];
    isFullScreenMode = YES;
    [_titleView resignTextField];

}

-(void)exitFullScreenMode{
    [UIView beginAnimations:@"NotScreenMode" context:NULL];
    [UIView setAnimationDuration:0.2f];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

    [SearchBarViewController sharedInstance].view.frame =
    [[SearchBarUIManager sharedInstance] searchBarViewFrame];

    _webToolBarController.webToolBar.frame = [[WebToolBarUIManager sharedInstance] webToolBarFrame];
    if (!isBtnClickAni) {
        _customWidgetUiWebView.frame = [[CustomUiWebViewUIManager sharedInstance] getWebViewFrameBySearchResultView];
        _titleView.frame = [[CustomUiWebViewUIManager sharedInstance] getSearchBarView];

    }
    [UIView commitAnimations];

    [UIView beginAnimations:@"animationID" context:NULL];
    [UIView setAnimationDuration:0.5f];
    _fullScreenBtn.alpha = 0;
    [UIView commitAnimations];
    isFullScreenMode = NO;

}

- (void)fullScreenBtnClick:(id)sender {
  //  isBtnClickAni = YES;
    [self exitFullScreenMode];
  //  isBtnClickAni = NO;
}

gosafari的代码:

- (void)goSafari
{
	UIActionSheet *actionSheet = [[UIActionSheet alloc]
								  initWithTitle:NSLocalizedString(@"String_QueryOpBySafari", nil)
								  delegate:self
								  cancelButtonTitle:NSLocalizedString(@"String_Cancel", nil)
								  destructiveButtonTitle:nil
								  otherButtonTitles:NSLocalizedString(@"String_OpBySafari", nil),nil];
	[actionSheet showInView:self.view];

}

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
	if (buttonIndex==0)
	{
		if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:[_customWidgetUiWebView stringByEvaluatingJavaScriptFromString:@"document.URL"]]])
		{
			[[CCommon sharedInstance ]appOpenUrl:[_customWidgetUiWebView stringByEvaluatingJavaScriptFromString:@"document.URL"]];
		}
		else
		{
			[[CCommon sharedInstance ]appOpenUrl:[[_customWidgetUiWebView.request URL]absoluteString]];
		}

	}
}

效果图:

代码:http://download.csdn.net/detail/baidu_nod/7734661

IOS写一个可以支持全屏的WebView,布布扣,bubuko.com

时间: 2024-12-18 14:27:11

IOS写一个可以支持全屏的WebView的相关文章

iOS 实现单个页面支持横竖屏,其他页面只能竖屏

最近在自己的项目里面 有需要做一个需求 : app中某一个页面支持横竖屏, 而其他页面只能竖屏. 1 2 实现方法如下: 1 首先需要Xcode中选中支持的屏幕方向  2 Appdelegate中 .h @property (nonatomic,assign)NSInteger allowRotate; 1 .m中 //此方法会在设备横竖屏变化的时候调用 - (NSUInteger)application:(UIApplication *)application supportedInterfa

如果写一个android支持的html文件

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/199

一个简单的全屏图片上下打开显示网页效果

打包下载地址:http://download.csdn.net/detail/sweetsuzyhyf/7602105 上源码看效果: <!DOCTYPE html> <html> <head> <title></title> <style> body { margin: 0; padding: 0; } .wrap { overflow: hidden; position: fixed; z-index: 99999; width:

如何制作一个完美的全屏视频H5

写在前面的话: 最近一波H5广告火爆整个互联网圈,身为圈内人,我们怎能     不! 知!道! :( 嘘!真不知道的也继续看下去,有收获 ↓ ) So,搞懂这个并不难. 这篇文章将带你从头到尾了解H5广告的实现. 本文主要讲一下几个关键点 一.视频内联播放.        -- 想要营造一种文字与视频混排的现象,视频不要影响其他模块 二.视频去控件.  -- 交互视频,不能点击快/慢进或暂停哦 三.去控件全屏播放. -- 想要模拟 明星给我打电话的体验,不能看到明显的视频播放器 四.视频自动播放

如何在Ubuntu手机中使得一个应用是全屏的应用

我们知道很多的开发者想把自己的应用设置为全屏的应用,这样可以使得应用能更多地占用屏幕的有效面积以使得自己的应用更好看.在默认的SDK的样板中,在应用的最上面,有一个"title"的地方占用很多的空间.对于一些应用来说,在主界面中,这个可能并不有用,但是对于使用PageStack的应用来说,这个区域显示一个向左的箭头以返回上一个页面的. 最近我也有这样的问题,我既想使用PageStack给予我的方便,又想拥有全屏的功能.在这篇文章中,我们来介绍如何做到全屏的应用.另外我们值得指出的是:我

10款支持全屏展示的jQuery插件

Developers can check out the list below of jQuery plugins that provide full screen support. These plugins provide full screen website, image galleries, editor, slider, menu, loading bar, Youtube videos and modal windows. 1. fullPage.js fullPage.js is

iOS自定义全屏返回与tableView左划删除手势冲突解决

当自定义一个navigationController实现全屏右划返回时, 使用起来是不是很爽, 代码如下: - (void)viewDidLoad { [super viewDidLoad]; UIGestureRecognizer *gester = self.interactivePopGestureRecognizer; UIPanGestureRecognizer *panGesTer = [[UIPanGestureRecognizer alloc] initWithTarget:ge

HTML5 全屏化操作功能

由于项目中用到了全屏化挫折功能,查看了API后写了一个简单的全屏化model <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <style type="text/css"> .div1{ width: 1000px;height: 500px; border: solid 1px blue;

html5实现全屏的api方法

参考地址 [进入和退出全屏] // Webkit (works in Safari5.1 and Chrome 15) element.webkitRequestFullScreen(); document.webkitCancelFullScreen(); // Firefox 10 element.mozRequestFullScreen(); document.mozCancelFullScreen(); // W3C 提议 element.requestFullscreen(); doc