如何使用 FlipAnimation 从每个 CollectionViewCell 打开一个新的 ViewController 并关闭 Swift 3
Posted
技术标签:
【中文标题】如何使用 FlipAnimation 从每个 CollectionViewCell 打开一个新的 ViewController 并关闭 Swift 3【英文标题】:How to open a new ViewController from every CollectionViewCell using a FlipAnimation and dismiss Swift 3 【发布时间】:2017-07-08 06:37:46 【问题描述】:已经实现了collectionview,它可以正常工作,但是我想要FlipAnimation,同时从单元格中打开一个新的视图控制器并解散回单元格。如何达到同样的效果。因为我对动画一无所知。请帮助。谢谢。 代码
import UIKit
class DashBoardVC: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
@IBOutlet weak var logoutbtn: UIButton!
@IBOutlet weak var menubtn: UIButton!
var names = ["Dashboard","Add Book","Delete Book","Search Book","On Demand","About Us"]
var useimages = [UIImage(named:"dashboard_icon2"),UIImage(named:"book_blue_add"),UIImage(named:"book_blue_delete"),UIImage(named:"book_blue_view"),UIImage(named:"book_blue_new"),UIImage(named:"businesspeople2"),]
override func viewDidLoad()
super.viewDidLoad()
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
return names.count
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellcollect", for: indexPath) as! mycollectioncell
cell.collectLabel.text! = names[indexPath.row]
cell.collectimage.image = useimages[indexPath.row]
return cell
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
let padding : CGFloat = 15
let collectionViewSize = collectionView.frame.size.width - padding
return CGSize(width: collectionViewSize/2, height: collectionViewSize/2)
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
switch(indexPath.row)
case 0:
let desVC = storyboard.instantiateViewController(withIdentifier: "DashboardClick") as! DashboardClick
self.navigationController?.pushViewController(desVC, animated: true)
break
case 1:
let desVC = storyboard.instantiateViewController(withIdentifier: "AddBookVC") as! AddBookVC
self.navigationController?.pushViewController(desVC, animated: true)
break
case 2:
let desVC = storyboard.instantiateViewController(withIdentifier: "DeleteBookVC") as! DeleteBookVC
self.navigationController?.pushViewController(desVC, animated: true)
break
case 3:
let desVC = storyboard.instantiateViewController(withIdentifier: "SearchBookVC") as! SearchBookVC
self.navigationController?.pushViewController(desVC, animated: true)
break
case 4:
let desVC = storyboard.instantiateViewController(withIdentifier: "onDemandBookVC") as! onDemandBookVC
self.navigationController?.pushViewController(desVC, animated: true)
break
default:
break
override func viewWillAppear(_ animated: Bool)
self.navigationController?.navigationBar.isHidden = true
self.navigationController?.toolbar.isHidden = true
@IBAction func menutapped(_ sender: Any)
var appdelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
appdelegate.centerContainer?.toggle(MMDrawerSide.left, animated: true, completion: nil)
mycollectioncell.swift
import UIKit
class mycollectioncell: UICollectionViewCell
@IBOutlet weak var collectLabel: UILabel!
@IBOutlet weak var collectimage: UIImageView!
【问题讨论】:
【参考方案1】:这里 Student 是 NSObjectClass
var Arraydata:[Student] = []
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
let arr = Arraydata[indexPath.row]
if indexPath.row == arr
let story = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
UIView.beginAnimations("", context: nil)
UIView.setAnimationDuration(1.0)
UIView.setAnimationCurve(UIViewAnimationCurve.easeInOut)
UIView.setAnimationTransition(UIViewAnimationTransition.flipFromRight, for: (self.navigationController?.view)!, cache: false)
self.navigationController?.pushViewController(story, animated: true)
UIView.commitAnimations()
【讨论】:
感谢您的回复,但请参考我编辑的问题。 我已经更新了代码。所以试着把代码放在你的应用程序中它是 100% 工作的 非常感谢,这正是我想要的,嘿,当它翻转时,场景后面是黑色背景,我们不能设置图像或任何颜色来代替它吗?。 嗨,rob,你能帮我解决我的最新问题吗?***.com/questions/45436010/….以上是关于如何使用 FlipAnimation 从每个 CollectionViewCell 打开一个新的 ViewController 并关闭 Swift 3的主要内容,如果未能解决你的问题,请参考以下文章