2016-10-29 6 views
-1

Ich habe eine CollectionView in einem ViewController. Wenn ich wische, wischt es zu einer anderen Collection View Zelle, die die Höhe und Breite der view.frame istSo animieren Sie beim Durchstreichen der Sammlungsansicht

Ich möchte eine Animation, die startet, wenn ich mit dem Streichen beginnen, um dann zu beenden, wann immer die Sammlung Ansicht in der Mitte einrastet einer Zelle genau wie das gif unten. Ich habe kein Label oder eine Textansicht in meinem Code, aber könnten Sie mir in die richtige Richtung zeigen, wie ich das machen würde? Hier ist mein relevent Code:

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { 

lazy var collectionView: UICollectionView = { 
    let layout = UICollectionViewFlowLayout() 
    layout.scrollDirection = .horizontal 
    layout.minimumLineSpacing = 0 
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout) 
    cv.backgroundColor = .white 
    cv.dataSource = self 
    cv.delegate = self 
    cv.isPagingEnabled = true 
    cv.showsHorizontalScrollIndicator = false 
    return cv 
}() 

let cellId = "cellId" 
let loginCellId = "loginCellId" 

let pages: [Page] = { 
    let firstPage = Page(imageName: "introduction_1") 
    let secondPage = Page(imageName: "introduction_2") 
    let thirdPage = Page(imageName: "introduction_3") 
    let fourthPage = Page(imageName: "introduction_4") 
    return [firstPage, secondPage, thirdPage, fourthPage] 
}() 

lazy var pageControl: UIPageControl = { 
    let pc = UIPageControl() 
    pc.pageIndicatorTintColor = .lightGray 
    pc.currentPageIndicatorTintColor = .darkGray 
    pc.numberOfPages = self.pages.count + 1 
    return pc 
}() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    view.addSubview(collectionView) 
    view.addSubview(pageControl) 

    _ = pageControl.anchor(nil, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 40) 

    collectionView.anchorToTop(view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor) 

    registerCells() 
} 

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) { 

    let pageNumber = Int(targetContentOffset.pointee.x/view.frame.width) 
    pageControl.currentPage = pageNumber 
} 

fileprivate func registerCells() { 
    collectionView.register(PageCell.self, forCellWithReuseIdentifier: cellId) 
    collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: loginCellId) 

} 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return pages.count + 1 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    if indexPath.item == pages.count { 
     let loginCell = collectionView.dequeueReusableCell(withReuseIdentifier: loginCellId, for: indexPath) 
     return loginCell 
    } 

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! PageCell 

    let page = pages[indexPath.item] 
    cell.page = page 

    return cell 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    return CGSize(width: view.frame.width, height: view.frame.height) 
} 

}

class PageCell: UICollectionViewCell { 

var page: Page? { 
    didSet { 
     guard let page = page else { 
      return 
     } 
     imageView.image = UIImage(named: page.imageName) 
    } 
} 

override init(frame: CGRect) { 
    super.init(frame: frame) 

    setupViews() 
} 

let imageView: UIImageView = { 
    let iv = UIImageView() 
    iv.contentMode = .scaleAspectFill 
    iv.backgroundColor = .yellow 
    iv.clipsToBounds = true 
    return iv 
}() 

func setupViews() { 

    addSubview(imageView) 

    imageView.anchorToTop(topAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor) 
} 

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

}

enter image description here

Antwort

Verwandte Themen