gpt4 book ai didi

swift - UICollectionView 上的后退按钮

转载 作者:搜寻专家 更新时间:2023-11-01 07:22:07 24 4
gpt4 key购买 nike

我正在尝试向我的 UICollectionView 添加一个后退/返回按钮,到目前为止我有这段代码来实现该按钮:

import UIKit

class EmojiPopup: UIView,UICollectionViewDataSource,UICollectionViewDelegate
{
var collocationView : UICollectionView!
var arrImagesList:NSMutableArray!

var blur:UIBlurEffect = UIBlurEffect()

override init(frame: CGRect)
{

super.init(frame: frame)

arrImagesList = NSMutableArray()
self.backgroundColor = UIColor.purpleColor().colorWithAlphaComponent(0.2)
let layout = UICollectionViewFlowLayout()
//header gap
layout.headerReferenceSize = CGSizeMake(20,20)
//collection view item size
layout.itemSize = CGSizeMake(70, 70)
layout.minimumInteritemSpacing = 25
layout.minimumLineSpacing = 25
collocationView = UICollectionView(frame: CGRectMake(50,50,UIScreen.mainScreen().bounds.screenWidth - 100,UIScreen.mainScreen().bounds.screenHeight - 100), collectionViewLayout: layout)
self.addSubview(collocationView)

// Create the blurEffect and apply to view
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.ExtraLight)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.alpha = 0.7
blurEffectView.frame = self.bounds
self.addSubview(blurEffectView)

collocationView.backgroundColor = UIColor.purpleColor().colorWithAlphaComponent(0.002)
collocationView.dataSource = self
collocationView.delegate = self
collocationView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellIdentifier")

//hide scrollbar
self.collocationView.showsVerticalScrollIndicator = false

//back button
let btnBack = UIButton(frame:TCRectMake(x:138 ,y:523,width:45,height:45))
btnBack.setImage(UIImage(named:"back"), forState: UIControlState.Normal)
btnBack.addTarget(self, action:"btnBackClick", forControlEvents: UIControlEvents.TouchUpInside)
self.addSubview(btnBack)

//back button func
func btnBackClick()
{

}

let fm = NSFileManager.defaultManager()
let path = NSBundle.mainBundle().resourcePath!
let items = try! fm.contentsOfDirectoryAtPath(path)

for item in items
{
if item.hasSuffix("png") && item.containsString("@") == false && item.containsString("AppIcon") == false && item.containsString("tick_blue") == false && item.containsString("video_camera") == false
{
arrImagesList.addObject(item)
}
}
}
var completeHandler:((String)->())?
func showDetails(viewParent:UIView,doneButtonClick:((String)->())?)
{
completeHandler = doneButtonClick
viewParent.addSubview(self)
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return arrImagesList.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let identifier="ImageCell\(indexPath.section)\(indexPath.row)"
collectionView.registerClass(ImageViewCell.self, forCellWithReuseIdentifier:identifier)

let cell = collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath) as! ImageViewCell
cell.backgroundColor = UIColor(white:1, alpha:0)
cell.imgView.image = UIImage(named:arrImagesList[indexPath.row] as! String)
cell.imgView.backgroundColor = UIColor.clearColor()
cell.imgView.opaque = false
cell.imgView.contentMode = .ScaleAspectFit

//keeps blur to background
self.bringSubviewToFront(collocationView)

return cell
}
// func collectionView(collectionView: UICollectionView,
// layout collectionViewLayout: UICollectionViewLayout,
// sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
// {
// let width=UIScreen.mainScreen().bounds.size.width-50
// return CGSize(width:width/3, height:width/3)
// }

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
//let cell=collectionView.cellForItemAtIndexPath(indexPath) as! ImageViewCell

UIView.animateWithDuration(0.3, animations:{
self.collocationView.alpha=0
}, completion: { finished in

if self.completeHandler != nil
{
self.completeHandler!(self.arrImagesList[indexPath.row] as! String)
}
self.removeFromSuperview()
})

}
func showDetails(viewParent:UIView,dictData : [String:String],index:Int,doneButtonClick:(()->())?,cancelBUttonClick:(()->())?)
{
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

如果用户按下后退按钮,我希望关闭 Collection View ,但我不确定在后退按钮功能中输入什么。如果可能的话,我希望用户返回到主视图 Controller (mapview)

最佳答案

我假设您在谈论 UICollectionViewController 而不是 UICollectionViewUICollectionViewController 内部有一个 UICollectionView。您可以“关闭”(关闭)UICollectionViewController 但不能“关闭”(关闭)UICollectionView。您甚至可以关闭内部有 UICollectionViewUIViewController

你有两个选择:

  1. 将 Collection View Controller (和主视图 Controller )放在导航 Controller 中,这样您就可以使用导航 Controller 已经实现的默认后退按钮。
  2. 您可以从主视图 Controller 中以模态方式呈现 Collection View Controller 。然后你需要添加一个关闭按钮(不是后退按钮)来关闭 Controller (主视图 Controller 将留在“后面”所以当你关闭 UICollectionViewController 它将再次变得可见。

路途遥远。我建议你阅读这个 getting started guide来自 Apple,在那里你可以弄清楚导航 Controller 是如何工作的以及它们的作用。这是您在使用 Swift 进行开发时需要学习的东西。我建议您进一步阅读整个教程。阅读该章后,您应该了解 iOS 应用程序的导航流程并实现后退按钮导航。

如果您在学习该教程时发现任何问题,请告诉我。

关于swift - UICollectionView 上的后退按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38313041/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com