2017-02-16 46 views
1

我正在使用Swift 3在iPhone中開發一個簡單的商業應用程序,而我是開發新手。我有一個ViewController與一個UIView,通過圖像與UIPageControl循環。我也在同一個ViewController中有標籤和水平滾動的UICollectionViews。我使用Storyboard配置了我的滾動視圖,並在代碼中配置了UIPageControl。如何在兩個自定義TableViewCells中顯示UIView圖像幻燈片和CollectionViews?

問題:當我將滾動視圖嵌入到ViewController中時,UIPageControl和CollectionViewCells保持在同一位置。

我在這裏有一個示例。請參閱圖片以獲得更好的理解:Image showing my ViewController where the PageControl stays same while the View is scrolled和另一張圖片ViewController while scrolling up

所以,我發現scrollview與我的CollectionView和PageControl滾動發生衝突。

有人可以告訴我如何可以嵌入我的UIView滑動圖像在自定義TableViewCell和其他自定義TableViewCell中的CollectionViews,以啓用垂直滾動?

這裏是我的ViewController.swift:

import UIKit 
class Home: UIViewController{ 
    @IBOutlet var myView: UIView!  
    @IBOutlet var pageControl: UIPageControl! 

    override func viewDidLoad() { 
     super.viewDidLoad()    
     self.addSlideMenuButton() 
     self.title="Home" 
     //Image Loop 
     self.pageControl.currentPage = -1 
     configurePageControl() 
     slide = -1 
     self.changeSlide() 
     var timer = Timer.scheduledTimer(timeInterval: 10 
      , target: self, selector: #selector(changeSlide), userInfo: nil, repeats: true);   

     myView.isUserInteractionEnabled = true 
     let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.swiped(_gesture:))) 

     swipeRight.direction = UISwipeGestureRecognizerDirection.right 
     myView.addGestureRecognizer(swipeRight) 

     let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swiped(_gesture:))) 
     swipeLeft.direction = UISwipeGestureRecognizerDirection.left 
     myView.addGestureRecognizer(swipeLeft)   

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

    var images = [UIImage(named:"banner2"),UIImage(named:"banner1"),UIImage(named:"banner3"),UIImage(named:"banner4"),UIImage(named:"banner5")]  

    var slide:Int = 0   
    func changeSlide(){ 

     var loop : UIImageView!    
     loop = UIImageView(frame: myView.bounds) 
     loop.contentMode = UIViewContentMode.scaleToFill   

     if(slide == images.count-1) 
     { 
      slide=0 
      loop.image = images[slide] 
      self.pageControl.currentPage=slide 
     } 
     else 
     { 
      slide+=1 
      loop.image = images[slide] 
      self.pageControl.currentPage=slide 
     } 
      loop.layer.affineTransform() 
     myView.addSubview(loop)    
    } 


    @IBAction func pageChange(_ sender: AnyObject) {   
    } 

    func swiped(_gesture: UIGestureRecognizer) { 

     var loop : UIImageView! 
     loop = UIImageView(frame: myView.bounds) 
     loop.contentMode = UIViewContentMode.scaleToFill 
     loop.image = images[slide] 
     myView.addSubview(loop) 

     if let swipeGesture = _gesture as? UISwipeGestureRecognizer { 

      switch swipeGesture.direction { 

      case UISwipeGestureRecognizerDirection.right : 

       // decrease index first 
       // check if index is in range 

       if (slide == 0) { 

        slide = images.count-1 
        loop.image = images[slide] 
        self.pageControl.currentPage = slide 
       } 

       else 
       { 

        slide-=1 
        loop.image = images[slide] 
        self.pageControl.currentPage = slide 

       } 

      case UISwipeGestureRecognizerDirection.left: 


       // increase index first 
       // check if index is in range 

       if (slide == images.count-1) { 

        slide = 0 
        loop.image = images[slide] 
        self.pageControl.currentPage = slide 
       } 
       else{ 
        slide+=1 
        loop.image = images[slide] 
        self.pageControl.currentPage = slide 
       }     
      default: 
       break //stops the code/codes nothing. 

      } 
     } 

    } 

    func configurePageControl() { 
     self.pageControl.numberOfPages = images.count 
     self.pageControl.currentPage = slide 
     self.pageControl.tintColor = UIColor.red 
     self.pageControl.pageIndicatorTintColor = UIColor.gray 
     self.pageControl.currentPageIndicatorTintColor = UIColor.green 

     self.myView.addSubview(pageControl) 

     self.view.addSubview(pageControl) 
    }   
} 

這包含UIView的圖像循環和UIPageControl還通過圖像刷卡選項代碼。

有人可以幫我嗎?提前致謝。

回答

1

**這就是我這樣做的:** 第1步: - 首先您需要創建自定義單元格。這裏是自定義單元的代碼:

class MovieCollectionViewCell: UICollectionViewCell { 

var moviePoster = UIImageView() 
var view: UIView = UIView() 
override init(frame: CGRect) { 
    super.init(frame: frame) 

    contentView.addSubview(moviePoster) 
    moviePoster.contentMode = .scaleAspectFill 
    moviePoster.translatesAutoresizingMaskIntoConstraints = false 

    let constraints : [NSLayoutConstraint] = [ 

     moviePoster.topAnchor.constraint(equalTo: contentView.topAnchor), 
     moviePoster.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), 
     moviePoster.trailingAnchor.constraint(equalTo: contentView.trailingAnchor), 
     moviePoster.bottomAnchor.constraint(equalTo: contentView.bottomAnchor) 
    ] 
    NSLayoutConstraint.activate(constraints) 
} 

required init?(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
}  
} 

這會將您的圖像設置爲圖像視圖。我已經使用錨按照迅速3.

步驟2: - 創建的tableView其中swipeView是將被設置到的tableView的headerView的圖。將swipeView中的collectionView作爲其子視圖添加。 之後,將swapView設置爲tableView的headerView並執行pageControl設置。這裏是代碼:

var tableView : UITableView = UITableView(
    frame: CGRect.zero, 
    style: .grouped 
) 
var swipeView : UIView = UIView() 
var myCollectionView: UICollectionView! 
var indicator = MyIndicator() 
var pageControl : UIPageControl = UIPageControl() 
var detailsViewVM : DetailsViewViewModel! 

override func viewDidLoad() { 
    super.viewDidLoad() 


    self.view.backgroundColor = UIColor.white 
    self.view.addSubview(self.tableView) 

    self.tableView.rowHeight = UITableViewAutomaticDimension 
    self.tableView.estimatedRowHeight = 44 
    self.tableView.dataSource = self 
    self.tableView.delegate = self 
    self.tableView.register(
     DetailsCustomTableViewCell.self, 
     forCellReuseIdentifier: "textCell" 
    ) 
    self.tableView.register(
     StarRatingCell.self, 
     forCellReuseIdentifier: "starRatingCell" 
    ) 
    self.tableView.translatesAutoresizingMaskIntoConstraints = false 
// self.tableView.backgroundColor = UIColor(netHex: 0x90caf9) 
    self.view.addSubview(self.tableView) 

    let tableViewConstraints : [NSLayoutConstraint] = [ 
     self.tableView.topAnchor.constraint(equalTo: view.topAnchor), 
     self.tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor), 
     self.tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor), 
     self.tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor) 
    ] 
    NSLayoutConstraint.activate(tableViewConstraints) 

// MARK : Collection view layout 

    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 
    layout.scrollDirection = .horizontal 

    self.myCollectionView = UICollectionView(
     frame: CGRect(
      x: 0, 
      y: 0, 
      width: self.view.bounds.width, 
      height: 200), 
     collectionViewLayout: layout 
    ) 

    self.myCollectionView.isPagingEnabled = true 
    self.myCollectionView.dataSource = self 
    self.myCollectionView.delegate = self 

    self.myCollectionView.register(
     MovieCollectionViewCell.self, 
     forCellWithReuseIdentifier: "MyCell" 
    ) 
    self.swipeView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 200)) 
    self.myCollectionView.backgroundColor = UIColor.gray 
    indicator.StartActivityIndicator(obj: self.myCollectionView) 
    self.swipeView.addSubview(myCollectionView) 
    self.tableView.tableHeaderView = self.swipeView 

// MARK : UIPageControl 

    configurePageControl() 
    self.pageControl.translatesAutoresizingMaskIntoConstraints = false 
    self.pageControl.bottomAnchor.constraint(equalTo: swipeView.bottomAnchor, constant : 8).isActive = true 
    self.pageControl.centerXAnchor.constraint(equalTo: swipeView.centerXAnchor).isActive = true 
    self.pageControl.heightAnchor.constraint(equalToConstant: 44).isActive = true 
    self.pageControl.leadingAnchor.constraint(equalTo: self.swipeView.leadingAnchor).isActive = true 
    self.pageControl.trailingAnchor.constraint(equalTo: self.swipeView.trailingAnchor).isActive = true 

} 

func configurePageControl() { 
     self.pageControl.currentPage = 0 
     self.pageControl.pageIndicatorTintColor = UIColor(netHex : 0xd6eaf8) 
     self.pageControl.currentPageIndicatorTintColor = UIColor(netHex: 0xd81b60) 
     self.swipeView.addSubview(pageControl) 
     swipeView.bringSubview(toFront: pageControl) 
    } 

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { 
    let x = myCollectionView.contentOffset.x 
    let w = myCollectionView.bounds.size.width 
    self.pageControl.currentPage = Int(ceil(x/w)) 
} 

func reloadView() { 
    self.pageControl.numberOfPages = self.detailsViewVM.getMoviePosterArray().count 
    self.myCollectionView.reloadData() 
    indicator.StopActivityIndicator() 
} 

extension DetailsViewController: UICollectionViewDelegateFlowLayout { 

func collectionView(_ collectionView: UICollectionView, 
        layout collectionViewLayout: UICollectionViewLayout, 
        minimumLineSpacingForSectionAt section: Int) -> CGFloat 
{ 

    return 0.0 
} 

    func collectionView(_ collectionView: UICollectionView, 
         layout collectionViewLayout: UICollectionViewLayout, 
         sizeForItemAt indexPath: IndexPath) -> CGSize 
    { 

     return CGSize(width: self.view.bounds.width, height: self.myCollectionView.bounds.height) 
    } 
} 

希望這個答案將解決您的問題。

+0

嘿謝謝你的回覆。是的,你的回答有幫助但是,我對此很陌生並且有些疑惑。什麼是MyIndi​​cator和DetailsViewViewModel!爲什麼使用它?你也可以發佈完整的代碼,讓我更好的理解?特別是用於textCell和starRatingCell的TableViewCell類代碼。 –

+0

txtCell是tableView其餘部分的文本單元格,starRatingCell是我的電影應用程序的評分單元格。 txtCell單元格與MovieCollectionViewCell相同,區別在於它的tableViewCell和UIImageView不同,它們都是UIlabel。 代碼簽出[我即將上映的電影項目](https://github.com/PravinNagargoje/movies) –

相關問題