我做了一个基于食品交付应用程序的项目。我添加了3个图像,它们位于视图控制器和页面视图控制器中,因为我希望我的图像水平滑动。
我这个问题的主要目的是在最后一个图像视图中需要一个按钮。请向我提供添加UIButton的完整代码,该UIButton仅在最后一个图像中可见。 例如 - :
(Lets Go)是一个按钮,可在最后一个图像视图中使用。忘记按钮放置。
提前谢谢......
我的代码是这样的 - :
导入UIKit
类ViewController:UIViewController,UIPageViewControllerDataSource {
var pageViewController : UIPageViewController?
let Contentimages = ["splash","up","viewpage_bg"]
override func viewDidLoad() {
super.viewDidLoad()
createpageViewController()
setupPageControl()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func createpageViewController(){
let pageController = self.storyboard?.instantiateViewController(withIdentifier: "pageController") as! UIPageViewController
pageController.dataSource = self
if Contentimages.count > 0 {
let firstController = getItemController(0)!
let startingViewController = [firstController]
pageController.setViewControllers(startingViewController, direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
}
pageViewController = pageController
addChildViewController(pageViewController!)
self.view.addSubview(pageViewController!.view)
pageViewController?.didMove(toParentViewController: self)
}
func setupPageControl(){
let appearance = UIPageControl.appearance()
appearance.pageIndicatorTintColor=UIColor.gray
appearance.currentPageIndicatorTintColor=UIColor.white
appearance.backgroundColor=UIColor.darkGray
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let itemController = viewController as! ItemViewController
if itemController.itemIndex > 0
{
return getItemController(itemController.itemIndex-1)
}
return nil
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let itemController = viewController as! ItemViewController
if itemController.itemIndex+1 < Contentimages.count
{
return getItemController(itemController.itemIndex+1)
}
return nil
}
func presentationCount(for pageViewController: UIPageViewController) -> Int {
return Contentimages.count
}
func presentationIndex(for pageViewController: UIPageViewController) -> Int {
return 0
}
func currentControllerIndex() -> Int {
let pageItemController = self.currentController()
if let controller = pageItemController as?ItemViewController{
return controller.itemIndex
}
return -1
}
func currentController() -> UIViewController? {
if (self.pageViewController?.viewControllers?.count)! > 0{
return self.pageViewController?.viewControllers![0]
}
return nil
}
func getItemController(_ itemIndex: Int) -> ItemViewController? {
if itemIndex < Contentimages.count {
let pageItemController = self.storyboard?.instantiateViewController(withIdentifier: "ItemController") as! ItemViewController
pageItemController.itemIndex = itemIndex
pageItemController.imageName = Contentimages[itemIndex]
return pageItemController
}
return nil
}
}
导入UIKit
类ItemViewController:UIViewController {
var itemIndex: Int = 0;
var imageName: String = ""{
didSet{
if let imageView = contentImageView{
imageView.image = UIImage(named : imageName)
}
}
}
@IBOutlet var contentImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
contentImageView.image = UIImage(named : imageName)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
答案 0 :(得分:1)
您可以在ItemViewController上添加此按钮。然后根据当前页面使按钮隐藏或可见。像这样:
let pageItemController = self.currentController()
let currentPage = self.currentControllerIndex()
if currentPage == Contentimages.count - 1 {
pageItemController.button.isHidden = false
} else {
pageItemController.button.isHidden = true
}
例如这里(如果我正确理解你的代码):
func pageViewController(_ pageViewController: UIPageViewController,
viewControllerAfter viewController: UIViewController) -> UIViewController? {
let itemController = viewController as! ItemViewController
if itemController.itemIndex == Contentimages.count - 1 {
itemController.button.isHidden = false
} else {
itemController.button.isHidden = true
}
if itemController.itemIndex+1 < Contentimages.count {
return getItemController(itemController.itemIndex+1)
}
return nil
}
答案 1 :(得分:0)
尝试使用UICollectionView
来实现此目标:
<强> 1。 UIViewController
包含UICollectionView
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout
{
override func viewDidLoad()
{
super.viewDidLoad()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return 3
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCollectionCell
if indexPath.row == collectionView.numberOfItems(inSection: indexPath.section) - 1
{
cell.letsGoButton.isHidden = false
}
else
{
cell.letsGoButton.isHidden = true
}
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
return CGSize(width: collectionView.bounds.width, height: collectionView.bounds.height)
}
}
<强> 2。自定义UICollectionViewCell
class CustomCollectionCell : UICollectionViewCell
{
@IBOutlet weak var letsGoButton: UIButton!
}
第3。查看层次结构
<强> 4。要记住的事情
dataSource
的{{1}}和delegate
。UICollectionView
设置为Collection View Flow Layout
中的Horizontal
。