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]];
- }
- }
- }
效果图:
IOS写一个能够支持全屏的WebView的更多相关文章
- 使用AVPlayer自定义支持全屏的播放器(五)—Swift重构版本
前言 很早之前开源了一个简单的视频播放器,由于年久失修,效果惨目忍睹,最近特意花时间对其进行了深度重构.旧版本后期不再维护,新版本使用Swift实现,后续会增加更多功能.不想看文字的请自行下载代码-- ...
- iOS 实现单个页面支持横竖屏,其他页面只能竖屏
最近在自己的项目里面 有需要做一个需求 : app中某一个页面支持横竖屏, 而其他页面只能竖屏. 1 2 实现方法如下: 1 首先需要Xcode中选中支持的屏幕方向 2 Appdelegate中 . ...
- 如何制作一个完美的全屏视频H5
写在前面的话: 最近一波H5广告火爆整个互联网圈,身为圈内人,我们怎能 不! 知!道! :( 嘘!真不知道的也继续看下去,有收获 ↓ ) So,搞懂这个并不难. 这篇文章将带你从头到尾了解H5 ...
- iOS端一次视频全屏需求的实现(转)
对于一个带有视频播放功能的app产品来说,视频全屏是一个基本且重要的需求.虽然这个需求看起来很简单,但是在实现上,我们前后迭代了三套技术方案.这篇文章将介绍这三种实现方案中的利弊和坑点,以及实现过程中 ...
- ios单独的页面支持横竖屏的状态调整,HTML5加载下(更新2)
单独的页面支持横竖屏的状态调整,HTML5加载下 工程中设置只支持竖屏状态,在加载HTML5的界面可以是横竖屏的,在不对工程其他界面/设置做调整的同时,可以这样去 #import "View ...
- 手动写一个类支持foreach循环
之前初学时看过可以实现Iterable接口实现Iterator迭代器的支持,并且也支持foreach循环.现在学习了数据结构,手动写一个单链表支持foreach循环吧. 手写foreach循环步骤: ...
- iOS开发——点击图片全屏显示
点击图片,全屏显示,然后再点击屏幕一下,返回. 没啥难的,直接上代码: // // ViewController.m // Demo-hehe // // Created by yyt on 1 ...
- iOS 全屏侧滑/UIScrollView/UISlider间滑动冲突
代码地址如下:http://www.demodashi.com/demo/13848.html 效果预览 一.前期准备 有一个支持全屏侧滑返回的视图控制器ViewController,ViewCont ...
- iOS 视频全屏功能 学习
项目中,也写过类似"视频全屏"的功能, 前一阵子读到今日头条 的一篇技术文章,详细介绍三种旋转方法差异优劣最终择取.文章从技术角度看写的非常好,从用户角度看,也用过多家有视频功能的 ...
随机推荐
- [CSharp] C#开源大全
商业协作和项目管理平台-TeamLab 网络视频会议软件-VMukti 驰骋工作流程引擎-ccflow [免费]正则表达式测试工具-Regex-Tester Windows-Phone-7-SDK E ...
- [牛客网练习赛 45 F] Magic Slab 解题报告 (最大权闭合子图)
interlinkage: https://ac.nowcoder.com/acm/contest/847/F description: solution: 最大权闭合子图; 每个单元格看成一个正权点 ...
- UVA-12578 10:6:2 计算几何 模拟
题面 题意:给你一块长方形,告诉你长:宽=10:6 里面有一个圆,长:半径=5:1,给你长方形的长,求圆的面积和剩余部分的面积 题解:直接模拟输出就好 #include<bits/stdc++. ...
- MongoDB Master-Slave cluster with authentication setup
Master Server create mongo db folder with sub folders like data, conf, && log mkdir -p /opt/ ...
- 在ubuntu中安装Markdown神器Typora
title: 在ubuntu中安装Markdown神器Typora toc: false date: 2018-09-01 17:48:15 categories: methods tags: ubu ...
- Ubuntu16.04下将hadoop2.7.3源代码导入到eclipse neon中
0.为什么会有这篇: 这篇文章的目的在于帮助想学习hadoop源码的内容,却在导入的过程中出现了各种问题的人. 或许你一定找了很多博客都无果,那么不用担心,我和你一样,这也是这篇文章存在的意义,废话少 ...
- MFC知识点总结
一:消息1.什么是消息?消息是驱动windows系统运行的基础.从计算机的角度来看,消息就是一个整数. (1)一个无符号整数,是消息值: (2)消息附带的WPARAM和LPARAM类型的参 ...
- MemCached总结一:Unbutu操作系统下memcached服务器安装和telnet方式连接memcache
1.在Unbutu上安装memcached服务器 sudo apt-get update sudo apt-get install memcached 2. 确认memcached是否安装 要确认me ...
- 阿里云API网关!
API 网关(API Gateway)提供高性能.高可用的 API 托管服务,帮助用户对外开放其部署在 ECS.容器服务等阿里云产品上的应用,提供完整的 API 发布.管理.维护生命周期管理.用户只需 ...
- 路飞学城Python-Day186
Evernote Export 持续集成 持续集成,简单的说就是持续集成频繁的将代码集成到主干,它的好处主要有1.快速发现错误,没完成一点更新,就集成到主干,可以快速发现错误,定位错误也会比较容易,2 ...