Ios 如何在UITableView标题上方添加具有视差效果的图像并保持标题粘性?
这是一张解释我想做的一切的图片: 我的问题是,我将如何构建我的视图结构。表视图的标题应固定在表的顶部。但是,位于表视图标题上方的最上面的图像呢。是否必须在UIScrollView中添加表视图Ios 如何在UITableView标题上方添加具有视差效果的图像并保持标题粘性?,ios,swift,uiviewanimationtransition,catransform3d,uiview-hierarchy,Ios,Swift,Uiviewanimationtransition,Catransform3d,Uiview Hierarchy,这是一张解释我想做的一切的图片: 我的问题是,我将如何构建我的视图结构。表视图的标题应固定在表的顶部。但是,位于表视图标题上方的最上面的图像呢。是否必须在UIScrollView中添加表视图 视差效果可以通过CATTransferM3D实现,但我如何实现我想要的,这是我的问题。有很多演示,但我想定制。您可以将图像视图添加到视图中,如- let imageView = UIImageView() let lblName = UILabel() imageView.frame = CGRect(
视差效果可以通过
CATTransferM3D
实现,但我如何实现我想要的,这是我的问题。有很多演示,但我想定制。您可以将图像视图添加到视图中,如-
let imageView = UIImageView()
let lblName = UILabel()
imageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 300)
imageView.image = UIImage.init(named: "poster")
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
view.addSubview(imageView)
lblName.frame = CGRect(x: 20, y: 100, width: 200, height: 22)
lblName.text = "Steve Jobs"
lblName.textColor = UIColor.white
lblName.font = UIFont.systemFont(ofSize: 26)
lblName.clipsToBounds = true
imageView.addSubview(lblName)
之后,在tableview委托方法中,您可以添加scrollviewDidScroll
方法,如-
let y = 300 - (scrollView.contentOffset.y + 300)
let height = min(max(y, 60), 400)
imageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: height)
lblName.frame = CGRect(x: 20, y: height - 30, width: 200, height: 22)
我希望这会有帮助。如果我错了,请纠正我
我想知道如何实现视差粘性标题,我发现它可以实现 这篇文章是用Swift 2写的,但我已经用Swift 4.2重新编码了 CustomHeaderView
import UIKit
class CustomHeaderView: UIView {
//MARK:- Variables
//MARK: Constants
//MARK: Variables
var imageView:UIImageView!
var colorView:UIView!
var bgColor = UIColor(red: 235/255, green: 96/255, blue: 91/255, alpha: 1)
var titleLabel = UILabel()
var articleIcon:UIImageView!
//MARK:- Constructor
init(frame:CGRect, title: String) {
self.titleLabel.text = title.uppercased()
super.init(frame: frame)
setUpView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//MARK:- Private methods
private func setUpView() {
backgroundColor = UIColor.white
imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
addSubview(imageView)
colorView = UIView()
colorView.translatesAutoresizingMaskIntoConstraints = false
addSubview(colorView)
let constraints:[NSLayoutConstraint] = [
imageView.topAnchor.constraint(equalTo: self.topAnchor),
imageView.leadingAnchor.constraint(equalTo: self.leadingAnchor),
imageView.trailingAnchor.constraint(equalTo: self.trailingAnchor),
imageView.bottomAnchor.constraint(equalTo: self.bottomAnchor),
colorView.topAnchor.constraint(equalTo: self.topAnchor),
colorView.leadingAnchor.constraint(equalTo: self.leadingAnchor),
colorView.trailingAnchor.constraint(equalTo: self.trailingAnchor),
colorView.bottomAnchor.constraint(equalTo: self.bottomAnchor)
]
NSLayoutConstraint.activate(constraints)
imageView.image = UIImage(named: "bg-header")
imageView.contentMode = .scaleAspectFill
colorView.backgroundColor = bgColor
colorView.alpha = 0.6
titleLabel.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(titleLabel)
let titlesConstraints:[NSLayoutConstraint] = [
titleLabel.centerXAnchor.constraint(equalTo: self.centerXAnchor),
titleLabel.topAnchor.constraint(equalTo: self.topAnchor, constant: 28),
]
NSLayoutConstraint.activate(titlesConstraints)
titleLabel.font = UIFont.systemFont(ofSize: 15)
titleLabel.textAlignment = .center
articleIcon = UIImageView()
articleIcon.translatesAutoresizingMaskIntoConstraints = false
addSubview(articleIcon)
let imageConstraints:[NSLayoutConstraint] = [
articleIcon.centerXAnchor.constraint(equalTo: self.centerXAnchor),
articleIcon.centerYAnchor.constraint(equalTo: self.centerYAnchor, constant: 6),
articleIcon.widthAnchor.constraint(equalToConstant: 40),
articleIcon.heightAnchor.constraint(equalToConstant: 40)
]
NSLayoutConstraint.activate(imageConstraints)
articleIcon.image = UIImage(named: "article")
}
//MARK:- Public methods
func decrementColorAlpha(offset: CGFloat) {
if self.colorView.alpha <= 1 {
let alphaOffset = (offset/500)/85
self.colorView.alpha += alphaOffset
}
}
func decrementArticleAlpha(offset: CGFloat) {
if self.articleIcon.alpha >= 0 {
let alphaOffset = max((offset - 65)/85.0, 0)
self.articleIcon.alpha = alphaOffset
}
}
func incrementColorAlpha(offset: CGFloat) {
if self.colorView.alpha >= 0.6 {
let alphaOffset = (offset/200)/85
self.colorView.alpha -= alphaOffset
}
}
func incrementArticleAlpha(offset: CGFloat) {
if self.articleIcon.alpha <= 1 {
let alphaOffset = max((offset - 65)/85, 0)
self.articleIcon.alpha = alphaOffset
}
}
}
导入UIKit
类CustomHeaderView:UIView{
//标记:-变量
//标记:常数
//马克:变量
var-imageView:UIImageView!
var colorView:UIView!
var bgColor=UIColor(红色:235/255,绿色:96/255,蓝色:91/255,阿尔法:1)
var titleLabel=UILabel()
var Article图标:UIImageView!
//马克:-建造师
init(帧:CGRect,标题:String){
self.titleLabel.text=title.uppercased()
super.init(frame:frame)
setUpView()
}
必需的初始化?(编码器aDecoder:NSCoder){
fatalError(“初始化(编码者:)尚未实现”)
}
//MARK:-私有方法
私有函数设置视图(){
backgroundColor=UIColor.white
imageView=UIImageView()
imageView.TranslatesAutoResizezingMaskintoConstraints=false
添加子视图(图像视图)
colorView=UIView()
colorView.TranslatesAutoResizezingMachineToConstraints=false
添加子视图(彩色视图)
let约束:[NSLayoutConstraint]=[
imageView.topAnchor.constraint(等式:self.topAnchor),
imageView.leadingAnchor.constraint(等式:self.leadingAnchor),
imageView.trailingAnchor.constraint(等式:self.trailingAnchor),
imageView.bottomAnchor.constraint(等式:self.bottomAnchor),
colorView.topAnchor.constraint(相等:self.topAnchor),
colorView.leadingAnchor.constraint(等式:self.leadingAnchor),
colorView.trailingAnchor.constraint(等式:self.trailingAnchor),
colorView.bottomAnchor.constraint(等式:self.bottomAnchor)
]
NSLayoutConstraint.activate(约束)
imageView.image=UIImage(名为:“bg头”)
imageView.contentMode=.ScaleSpectFill
colorView.backgroundColor=bgColor
colorView.alpha=0.6
titleLabel.translatesAutoResizengMaskintoConstraints=假
self.addSubview(标题标签)
let titlesConstraint:[NSLayoutConstraint]=[
标题标签.centerXAnchor.constraint(等于:self.centerXAnchor),
titleLabel.topAnchor.constraint(等式:self.topAnchor,常量:28),
]
NSLayoutConstraint.activate(标题约束)
titleLabel.font=UIFont.systemFont(大小:15)
titleLabel.textAlignment=.center
articleIcon=UIImageView()
articleIcon.TranslatesAutoResizezingMaskintoConstraints=false
添加子视图(文章图标)
让imageConstraints:[NSLayoutConstraint]=[
articleIcon.centerXAnchor.constraint(等于:self.centerXAnchor),
articleIcon.centerYAnchor.constraint(等式:self.centerYAnchor,常数:6),
articleIcon.widthAnchor.constraint(相等常量:40),
articleIcon.heightAnchor.constraint(相等常量:40)
]
NSLayoutConstraint.activate(图像约束)
articleIcon.image=UIImage(名为“文章”)
}
//标记:-公共方法
func递减色差(偏移量:cGFOAT){
如果self.colorView.alpha=0{
设alphaOffset=max((偏移-65)/85.0,0)
self.articleIcon.alpha=字母偏移量
}
}
func incrementColorAlpha(偏移量:CGFloat){
如果self.colorView.alpha>=0.6{
设alphaOffset=(offset/200)/85
self.colorView.alpha-=alphaOffset
}
}
func incrementArticleAlpha(偏移量:CGFloat){
如果self.articleIcon.alpha Int{
返回1
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回100
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
let cell=tableView.dequeueReusableCell(带有标识符:“cell”,用于:indexath)
cell.textlab?.text=“Article\(indexPath.row)”
返回单元
}
//标记:UITableViewDelegate实现
func scrollViewDidScroll(scrollView:UIScrollView){
如果scrollView.contentOffset.y<0{
self.headerHeightConstraint.constant+=abs(scrollView.contentOffset.y)
headerView.incrementColorAlpha(偏移量:self.headerHeightConstraint.Constraint)
headerView.incrementArticleAlpha(偏移量:self.headerHeightConstraint.Constraint)
}
否则,如果scrollView.contentOffset.y>0&&self.headerHeightConstraint.constant>=65{
self.headerHeightConstraint.constant-=scrollView.contentOffset.y/100
headerView.decrementColorAlpha(偏移量:scrollView.contentOffset.y)
headerView.DecrementArticalpha(偏移量:self.headerHeightConstraint.Constraint常数)
如果自人头重量恒定常数<65{
自人头高度恒定常数=65
}
}
}
func ScrollViewDiEndDraging(scrollView:UIScrollView,将减速减速:Bool){
如果self.headerHeightConstraint.constant>
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
//MARK:- Variables
//MARK: Constants
//MARK: Variables
var tableView:UITableView!
var headerView:CustomHeaderView!
var headerHeightConstraint:NSLayoutConstraint!
//MARK: - Lifecycle methods
override func viewDidLoad() {
super.viewDidLoad()
setUpHeader()
setUpTableView()
}
//MARK: - Private methods
private func setUpHeader() {
headerView = CustomHeaderView(frame: CGRect.zero, title: "Articles")
headerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(headerView)
headerHeightConstraint = headerView.heightAnchor.constraint(equalToConstant: 150)
headerHeightConstraint.isActive = true
let constraints:[NSLayoutConstraint] = [
headerView.topAnchor.constraint(equalTo: view.topAnchor),
headerView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
headerView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
]
NSLayoutConstraint.activate(constraints)
}
private func setUpTableView() {
tableView = UITableView()
tableView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(tableView)
let constraints:[NSLayoutConstraint] = [
tableView.topAnchor.constraint(equalTo: headerView.bottomAnchor),
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
]
NSLayoutConstraint.activate(constraints)
tableView.register(UITableViewCell.self,forCellReuseIdentifier: "cell")
tableView.dataSource = self
tableView.delegate = self
}
private func animateHeader() {
self.headerHeightConstraint.constant = 150
UIView.animate(withDuration: 0.4, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: [.curveEaseInOut], animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
//MARK: - UITableView implementation
//MARK: UITableViewDataSource implementation
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 100
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = "Article \(indexPath.row)"
return cell
}
//MARK: UITableViewDelegate implementation
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.y < 0 {
self.headerHeightConstraint.constant += abs(scrollView.contentOffset.y)
headerView.incrementColorAlpha(offset: self.headerHeightConstraint.constant)
headerView.incrementArticleAlpha(offset: self.headerHeightConstraint.constant)
}
else if scrollView.contentOffset.y > 0 && self.headerHeightConstraint.constant >= 65 {
self.headerHeightConstraint.constant -= scrollView.contentOffset.y/100
headerView.decrementColorAlpha(offset: scrollView.contentOffset.y)
headerView.decrementArticleAlpha(offset: self.headerHeightConstraint.constant)
if self.headerHeightConstraint.constant < 65 {
self.headerHeightConstraint.constant = 65
}
}
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if self.headerHeightConstraint.constant > 150 {
animateHeader()
}
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if self.headerHeightConstraint.constant > 150 {
animateHeader()
}
}
}
1.)
pod "MXParallaxHeader"
2.)
pod install
3.)
Add new file choose a (User Interface) View as a new template and name the
file. eg.. ParallaxView and tap on the create.
4.)
Add new file choose a (Cocoa Touch Class) View as a new template and name the file. eg.. ParallaxView and tap on the Next.
5.)
just visit this https://github.com/PageMenu/PageMenu/blob/master/Classes/CAPSPageMenu.swift and download the CAPSPageMenu.swift file and drag from your downloads and drop to your project destination folder.
6.)
import MXParallaxHeader
7.)
class MyParralax: UIViewController, MXScrollViewDelegate, CAPSPageMenuDelegate
{// Parant Controller Code }
var scrollView : MXScrollView!
let Parallax = Bundle.main.loadNibNamed("ParallaxView", owner: nil, options: nil)?.first as? ParallaxView
let controller1 : VC1 = VC1.instantiateFromStoryboard()
let controller2 : VC2 = VC2.instantiateFromStoryboard()
var controllerArray : [UIViewController] = []
var pageMenu : CAPSPageMenu?
import UIKit
class VC1: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// child conroller
}
class func instantiateFromStoryboard() -> VC1
{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
return storyboard.instantiateViewController(withIdentifier: "VC1") as! VC1
}
}
func setParallaxMenu(){
self.scrollView = MXScrollView()
self.scrollView.backgroundColor = UIColor.green
self.scrollView.delegate = self
self.scrollView.parallaxHeader.view = Parallax // You can set the parallax header view from a nib.
self.scrollView.parallaxHeader.height = 446.0 // desired hieght or hight of the xib file
self.scrollView.parallaxHeader.mode = MXParallaxHeaderMode.fill
self.scrollView.parallaxHeader.minimumHeight = UIApplication.shared.statusBarFrame.size.height + (self.navigationController?.navigationBar.frame.height)!
let newFrame = CGRect(x: 0,y: UIApplication.shared.statusBarFrame.size.height + (self.navigationController?.navigationBar.frame.height)!, width: self.view.frame.size.width, height: self.view.frame.size.height - (UIApplication.shared.statusBarFrame.size.height + (self.navigationController?.navigationBar.frame.height)!)) // scrollview's frame calculation
scrollView.frame = newFrame
scrollView.contentSize = newFrame.size
self.scrollView.delegate = self
view.addSubview(scrollView)
self.pagemenuSetup()
}
func pagemenuSetup()
{
controllerArray.removeAll()
controllerArray.append(controller1)
controllerArray.append(controller2)
controller1.title = "ORANGE"
controller2.title = "YELLOW"
// Customize menu (Optional)
let parameters: [CAPSPageMenuOption] = [
.menuItemSeparatorWidth(4.3),
.scrollMenuBackgroundColor(UIColor(red: 25.0/255.0, green: 26.0/255.0, blue: 36.0/255.0, alpha: 1.0)),
.viewBackgroundColor(UIColor.clear),
.selectionIndicatorColor(UIColor.white),
.bottomMenuHairlineColor(UIColor.clear),
.unselectedMenuItemLabelColor(UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 0.5)),
.menuItemFont(UIFont(name: "Helvetica", size: 16.0)!),
.enableHorizontalBounce(false),
.menuHeight(52.0),
.menuMargin(0.0),
.menuItemWidth(self.view.bounds.width/2),
.selectionIndicatorHeight(15.0),
.menuItemSeparatorPercentageHeight(0.1),
.iconIndicator(true),
.iconIndicatorView(self.getIndicatorView())
]
// Initialize scroll menu
var frame = view.frame
scrollView.frame = frame
scrollView.contentSize = frame.size
let Height = self.view.frame.size.height - (UIApplication.shared.statusBarFrame.size.height + (self.navigationController?.navigationBar.frame.height)!)
frame.size.height = Height
self.pageMenu = CAPSPageMenu(viewControllers: controllerArray, frame: frame, pageMenuOptions: parameters)
pageMenu!.delegate = self
self.scrollView.addSubview(pageMenu!.view)
view.addSubview(scrollView)
}
private func getIndicatorView()->UIView
{
let imgView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width/2, height: 15.0))
imgView.image = UIImage(named: "Indicator")
imgView.contentMode = .scaleAspectFit
return imgView
}