iPhone下弹出视图的方法
2015-08-20 08:20
239 查看
iPhone 下不支持 UIPopoverController,而使用 presentViewController:animated:completion: 方法得到的效果会全屏显示。有时候我们只想在部分区域类弹出视图,点击视图外区域自动消失。这里提供两种方案。
方案1:
1)增加 UIWindow,在新的窗口中添加视图控制器。
window =UIWindow(frame:
UIScreen.mainScreen().bounds)
window.windowLevel =UIWindowLevelNormal
window.backgroundColor =UIColor.clearColor()
window.rootViewController =FakeViewController()
2)增加背景视图,用于响应消息
dimBackground =
UIView(frame: UIScreen.mainScreen().bounds)
dimBackground.backgroundColor =UIColor.clearColor()
let gr:UITapGestureRecognizer =
UITapGestureRecognizer(target:self, action:Selector("dismiss"))
dimBackground.addGestureRecognizer(gr)
window.rootViewController!.view.addSubview(dimBackground)
3)创建actionSheet视图(这里省略),用于显示,为了得到移动的动画效果,先将其移出屏幕
var tmpFrame:
CGRect = self.actionSheet.frame
tmpFrame.origin.y =UIScreen.mainScreen().bounds.size.height
actionSheet.frame = tmpFrame;
window.rootViewController!.view.addSubview(actionSheet)
4)从下往上移动到屏幕之内
self.window.hidden =false
UIView.animateWithDuration(0.2, animations:{
self.dimBackground.backgroundColor =UIColor(white:
0.0, alpha:0.2)
let frame:
CGRect = UIScreen.mainScreen().bounds
self.actionSheet.frame =CGRectMake(0,
frame.size.height-self.actionSheet.frame.size.height,self.actionSheet.frame.size.width,self.actionSheet.frame.size.height);
})
5)点击背景视图响应消息,销毁窗口(具体可根据要求,或者保存,下次需要再次移到屏幕内即可)
func dismiss() {
UIView.animateWithDuration(0.2, animations: {[weak weakSelf =self] () ->
Voidin
self.dimBackground.backgroundColor =UIColor.clearColor()
let frame:
CGRect = UIScreen.mainScreen().bounds
self.actionSheet.frame =CGRectMake(0, frame.size.height,self.actionSheet.frame.size.width,self.actionSheet.frame.size.height);
}) { (complete) -> Void
in
self.window =nil
}
}
方案2:
1)创建要显示的 UIViewController
var frame:CGRect =
self.view.bounds
frame.origin.y = frame.size.height
- 44.0
frame.size.height =44.0
let pageJumpController =
PageJumpController(frame: frame, totolPage: (currentPageItem!.totalPage), delegate:self)
2)加入视图到上层视图上,并将视图控制器加入其栈中
self.view.addSubview(pageJumpController.view)
self.addChildViewController(pageJumpController);
pageJumpController.didMoveToParentViewController(self)
3)不想显示时,移出栈即可。注意,如果只将子视图移除,其控制器仍然在栈中,被父控制器所有,不会被销毁。根据具体情况实现
self.view.removeFromSuperview()
self.removeFromParentViewController()
方案1:
1)增加 UIWindow,在新的窗口中添加视图控制器。
window =UIWindow(frame:
UIScreen.mainScreen().bounds)
window.windowLevel =UIWindowLevelNormal
window.backgroundColor =UIColor.clearColor()
window.rootViewController =FakeViewController()
2)增加背景视图,用于响应消息
dimBackground =
UIView(frame: UIScreen.mainScreen().bounds)
dimBackground.backgroundColor =UIColor.clearColor()
let gr:UITapGestureRecognizer =
UITapGestureRecognizer(target:self, action:Selector("dismiss"))
dimBackground.addGestureRecognizer(gr)
window.rootViewController!.view.addSubview(dimBackground)
3)创建actionSheet视图(这里省略),用于显示,为了得到移动的动画效果,先将其移出屏幕
var tmpFrame:
CGRect = self.actionSheet.frame
tmpFrame.origin.y =UIScreen.mainScreen().bounds.size.height
actionSheet.frame = tmpFrame;
window.rootViewController!.view.addSubview(actionSheet)
4)从下往上移动到屏幕之内
self.window.hidden =false
UIView.animateWithDuration(0.2, animations:{
self.dimBackground.backgroundColor =UIColor(white:
0.0, alpha:0.2)
let frame:
CGRect = UIScreen.mainScreen().bounds
self.actionSheet.frame =CGRectMake(0,
frame.size.height-self.actionSheet.frame.size.height,self.actionSheet.frame.size.width,self.actionSheet.frame.size.height);
})
5)点击背景视图响应消息,销毁窗口(具体可根据要求,或者保存,下次需要再次移到屏幕内即可)
func dismiss() {
UIView.animateWithDuration(0.2, animations: {[weak weakSelf =self] () ->
Voidin
self.dimBackground.backgroundColor =UIColor.clearColor()
let frame:
CGRect = UIScreen.mainScreen().bounds
self.actionSheet.frame =CGRectMake(0, frame.size.height,self.actionSheet.frame.size.width,self.actionSheet.frame.size.height);
}) { (complete) -> Void
in
self.window =nil
}
}
方案2:
1)创建要显示的 UIViewController
var frame:CGRect =
self.view.bounds
frame.origin.y = frame.size.height
- 44.0
frame.size.height =44.0
let pageJumpController =
PageJumpController(frame: frame, totolPage: (currentPageItem!.totalPage), delegate:self)
2)加入视图到上层视图上,并将视图控制器加入其栈中
self.view.addSubview(pageJumpController.view)
self.addChildViewController(pageJumpController);
pageJumpController.didMoveToParentViewController(self)
3)不想显示时,移出栈即可。注意,如果只将子视图移除,其控制器仍然在栈中,被父控制器所有,不会被销毁。根据具体情况实现
self.view.removeFromSuperview()
self.removeFromParentViewController()
相关文章推荐
- 吉克1111-1114第七周讲座班、家庭作业(动态规划,期限:2014年4月25日本23点-周五晚上,科委飞信通知学生)
- 多线程并发问题(三)
- xxx
- xxx
- iOS的电话号码344格式以及判断电话号码的正则表达式
- Hibernate-----5、持久化对象
- win10中Groove音乐串流怎么播放OneDrive中的音乐收藏?
- 【冬瓜哥手绘】大话众核心处理器体系结构
- javascript 事件
- ScollView嵌套ListView和GridView问题
- scikit-learn:class and function reference(看看你到底掌握了多少。。)
- UVA12538 - Version Controlled IDE
- Windows Server 2016第三预览版简体中文镜像下载
- Docker 会取代虚拟机吗?
- android studio的安装
- 浅谈zookeeper的在hbase集群中的作用
- linux文件上传下载笔记(rz,sz,sftp,scp)命令
- 删除失败的域控制器
- 《Java设计模式》之策略模式
- Enterprise Solution 开源项目资源汇总 Visual Studio Online 源代码托管 企业管理软件开发框架