2016-10-29 3 views
-1

ViewController 내부에 collectionView가 있습니다. 스 와이프하면 다른 컬렉션 뷰 셀로 스 와이프됩니다. 뷰 프레임의 높이와 너비는콜렉션 뷰를 스 와이프 할 때 애니메이트하는 방법

입니다.

스 와이프가 시작될 때마다 시작되는 애니메이션을 갖고 싶습니다. 콜렉션 뷰가 중앙에 고정 될 때마다 끝내야합니다. 아래의 gif처럼 셀의 내 코드에는 레이블이나 텍스트 뷰가 없지만이 작업을 수행하는 방법에 대해 올바른 방향으로 알려줄 수 있습니까? 여기 내와 관계있는 코드는 다음과 같습니다 내 관점에서

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

답변