android 下拉放大背景图,Swift - 实现下拉时背景图片放大效果(仿QQ个人资料页面)...
在 QQ的个人信息页面上方有个背景图片,当我们下拉整个页面时,这个背景图会随着拖动距离而变大。下面通过样例演示这个效果如何实现。
1,效果图
当下拉页面时,背景图片会随着偏移量的增加而放大。
2,样例代码
实现方法是在滚动视图(scrollView)的 scrollViewDidScroll响应方法中获取偏移量,并根据偏移量来动态改变 imageView的位置和尺寸。
import UIKit
class ViewController: UIViewController {
let screenWidth = UIScreen.main.bounds.width // 屏幕宽度
let screenHeight = UIScreen.main.bounds.height // 屏幕高度
var imageView: UIImageView! // 图片视图
let imageViewHeight: CGFloat = 200 // 图片默认高度
var tableView: UITableView! //表格视图
let rowNumber = 50 // 表格数据条目数
let rowHeight: CGFloat = 40 // 表格行高
override func viewDidLoad() {
super.viewDidLoad()
// 首先创建一个滚动视图,图片还是tableView都放在这个滚动视图中
let scrollView = UIScrollView()
scrollView.frame = self.view.frame
scrollView.contentSize = CGSize(width: screenWidth,
height: CGFloat(rowNumber) * rowHeight + imageViewHeight)
scrollView.contentInsetAdjustmentBehavior = .never
scrollView.delegate = self
self.view.addSubview(scrollView)
// 初始化图片视图
self.imageView = UIImageView()
self.imageView.frame = CGRect(x: 0, y: 0, width: screenWidth,
height: imageViewHeight)
self.imageView.image = UIImage(named: "bg.jpeg")
self.imageView.contentMode = .scaleAspectFill
scrollView.addSubview(self.imageView)
//创建表视图
self.tableView = UITableView(frame: CGRect(x: 0, y: imageViewHeight,
width: screenWidth, height: CGFloat(rowNumber) * rowHeight), style:.plain)
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.rowHeight = CGFloat(rowHeight)
self.tableView.isScrollEnabled = false
//创建一个重用的单元格
self.tableView!.register(UITableViewCell.self,
forCellReuseIdentifier: "SwiftCell")
scrollView.addSubview(self.tableView!)
}
}
extension ViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
//获取偏移量
let offset = scrollView.contentOffset.y
// 改变图片大小
if offset <= 0 {
self.imageView.frame = CGRect(x: 0, y: offset, width: screenWidth,
height: imageViewHeight - offset)
}
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
//在本例中,有1个分区
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
//返回表格行数(也就是返回控件数)
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int)
-> Int {
return rowNumber
}
//创建各单元显示内容(创建参数indexPath指定的单元)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
-> UITableViewCell {
//为了提供表格显示性能,已创建完成的单元需重复使用
let identify:String = "SwiftCell"
//同一形式的单元格重复使用,在声明时已注册
let cell = tableView.dequeueReusableCell(
withIdentifier: identify, for: indexPath)
cell.textLabel?.text = "数据条目:\(indexPath.row + 1)"
return cell
}
}
功能改进:增加导航栏
1,效果图
(1)我们在上面样例的基础上增加一个导航栏,不过默认情况下导航栏不可见。而下拉时背景图片放大效果同之前的一样。
(2)当向上滑动页面时,背景栏会逐渐显示出来。同时在背景图快要完全移出时,导航栏标题才显示出来。
2,样例代码import UIKit
class ViewController: UIViewController {
let screenWidth = UIScreen.main.bounds.width // 屏幕宽度
let screenHeight = UIScreen.main.bounds.height // 屏幕高度
//导航栏背景视图
var barImageView: UIView?
var imageView: UIImageView! // 图片视图
let imageViewHeight: CGFloat = 200 // 图片默认高度
var tableView: UITableView! //表格视图
let rowNumber = 50 // 表格数据条目数
let rowHeight: CGFloat = 40 // 表格行高
override func viewDidLoad() {
super.viewDidLoad()
//获取导航栏背景视图
self.barImageView = self.navigationController?.navigationBar.subviews.first
//修改导航栏背景色
self.navigationController?.navigationBar.barStyle = .black
self.navigationController?.navigationBar.barTintColor = .orange
// 首先创建一个滚动视图,图片还是tableView都放在这个滚动视图中
let scrollView = UIScrollView()
scrollView.frame = self.view.frame
scrollView.contentSize = CGSize(width: screenWidth,
height: CGFloat(rowNumber) * rowHeight + imageViewHeight)
scrollView.contentInsetAdjustmentBehavior = .never
scrollView.delegate = self
self.view.addSubview(scrollView)
// 初始化图片视图
self.imageView = UIImageView()
self.imageView.frame = CGRect(x: 0, y: 0, width: screenWidth,
height: imageViewHeight)
self.imageView.image = UIImage(named: "bg.jpeg")
self.imageView.contentMode = .scaleAspectFill
scrollView.addSubview(self.imageView)
//创建表视图
self.tableView = UITableView(frame: CGRect(x: 0, y: imageViewHeight,
width: screenWidth, height: CGFloat(rowNumber) * rowHeight), style:.plain)
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.rowHeight = CGFloat(rowHeight)
self.tableView.isScrollEnabled = false
//创建一个重用的单元格
self.tableView!.register(UITableViewCell.self,
forCellReuseIdentifier: "SwiftCell")
scrollView.addSubview(self.tableView!)
}
override func viewDidAppear(_ animated: Bool) {
// 默认情况下导航栏全透明
self.barImageView?.alpha = 0
}
}
extension ViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
//获取偏移量
let offset = scrollView.contentOffset.y
// 改变图片大小
if offset <= 0 {
self.imageView.frame = CGRect(x: 0, y: offset, width: screenWidth,
height: imageViewHeight - offset)
}
// 导航栏背景透明度改变
var delta = offset / (imageViewHeight - 64)
delta = CGFloat.maximum(delta, 0)
self.barImageView?.alpha = CGFloat.minimum(delta, 1)
// 根据偏移量决定是否显示导航栏标题(上方图片快完全移出时才显示)
self.title = delta > 0.9 ? "hangge.com" : ""
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
//在本例中,有1个分区
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
//返回表格行数(也就是返回控件数)
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int)
-> Int {
return rowNumber
}
//创建各单元显示内容(创建参数indexPath指定的单元)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
-> UITableViewCell {
//为了提供表格显示性能,已创建完成的单元需重复使用
let identify:String = "SwiftCell"
//同一形式的单元格重复使用,在声明时已注册
let cell = tableView.dequeueReusableCell(
withIdentifier: identify, for: indexPath)
cell.textLabel?.text = "数据条目:\(indexPath.row + 1)"
return cell
}
}
源码下载:
hangge_2272.zip
android 下拉放大背景图,Swift - 实现下拉时背景图片放大效果(仿QQ个人资料页面)...相关推荐
- 直播间背景图怎么设计?直播间背景设计软件分享。
直播间背景图怎么设计?直播间背景是指直播时所展示的背景墙或屏幕,通常用于营造氛围和增强视觉效果.在直播中,背景可以承载主播个人风格.节目内容.品牌形象等信息,同时也是吸引观众注意力和提高观看体验的重要 ...
- 如何让网页背景图铺满整页(html+csss实现网页背景图铺满整页);a标签删下划线、禁用;innerHTMLouterHTML;css字体间距hover图片放大
background背景图片设置 body {width: 100%;height: 100%;background: url(../images/bg.png) no-repeat;backgrou ...
- 图片加载未完成时的默认背景图处理(仅限自己封装的图片加载工具)
Android图片加载时,由于网络状况 图片大小 手机内存等情况会导致加载图片的时长不同,这时候就涉及到给ImagevVew设置默认图片的问题. 当然了,最简单的方法就是在每个使用ImageVie ...
- div中插入图片_Web前端开发基础知识,设置网页背景图,如何在网页中插入图片...
图片 一.图片的表现形式 当我们在制作页面的时候,通常会遇到图片的三种表现形式,如下: 1.内容图片 内容图片是页面中真正的内容,没有内容图片,就无法完整的理解页面内容.如淘宝网上的商品展示图片,这些 ...
- python天气查询小程序加背景图_微信小程序开发背景图显示功能
这两天开发微信小程序,在设置背景图片时,发现在wxss里面设置background-image:(url) 属性,不管是开发工具还是线上都无法显示.经过查资料发现,background-image只能 ...
- 怎么更换vscode背景图,放入自己喜欢的图片
安装background插件 vscode插件市场下载插件 然后配置 这里配置给大家 // 是否开启背景图显示"background.enabled": true,// true- ...
- 抖音同款 抖音 城堡 微信背景图,抖音城堡微信背景图
抖音同款 抖音城堡 微信背景图
- php添加背景图及设置格式,PHP添加PNG图片背景透明水印操作类定义与用法示例
本文实例讲述了PHP添加PNG图片背景透明水印操作类定义与用法.分享给大家供大家参考,具体如下: 图片相关操作类 class ImageTool { private $imagePath;//图片路径 ...
- vue 背景图 自适应_Vue的自适应视频背景播放器
vue 背景图 自适应 Vue响应视频背景播放器 (vue-responsive-video-background-player) Play your own videos in background ...
最新文章
- 什么是蠕虫,木马以及二者跟病毒是什么关系!
- php简单的log文件
- 关于JavaScript 数组 的一切
- 上拉加载 php,php+jquery 上拉加载
- sql 两表数据合并_多表查询SQL语句
- 机箱硬盘指示灯不亮_一文学会“剪不断,理还乱”的主板跳线与机箱连接
- pytorch torchvision.transform.Compose
- 蔚来2018年平均每天亏掉2641万,车辆交付预期环比腰斩
- Python学习教程:教你用Python通过微信来控制电脑摄像头
- Ubuntu 16.04 安装Matlab R2015b
- 热力图heatmap.js使用中的思路解析
- php 字符显示不出来,ps文字显示不出来怎么办?
- c++ 求N个数的最大公约数和最小公倍数
- android音量键调节听筒音量的大小
- ApowerREC 1.2.4破解版 亲测能用
- 【C语言】写一个斗牛小游戏的发牌器
- python编程基础知识入门
- querylist V4 图片下载
- matlab运算结果中怎么用pretty,Pretty MuPad:将分配,表达和结果输出到一行 - 如何创建该功能?...
- 什么是云桌面计算机,云电脑桌面是什么?这是一篇良心科普文
热门文章
- 泛在电力物联网建设路线
- JSON.parse和JSON.stringify的用法
- QPainter绘制图片填充方式(正常大小、剪切大小、自适应大小、平铺)
- QLabel之动态阴影边框
- 03 Redis 网络IO模型简介
- 终于找全了!Go的三种常见的安装方式,各种系统多个版本应有尽有
- Matlab典型随机信号模拟
- 关于java博网即时通讯软件的设计与实现
- python基于pingouin包进行统计分析:anova函数执行非平衡双因素方差分析(Two-Way ANOVA、设置effsize参数为n2)、以dataframe的形式输出分析结果
- vue 2个方法先后执行_让人内心强大的2个方法,分享给大家