2017-04-13 8 views
0

swift3을 사용 중입니다. 내 앱에 수평 스크롤을위한 UIscrollview가 있습니다. 여기에는 페이징이 포함 된 이미지가 포함됩니다. 아래 코드 샘플입니다.iPad에서 수평 스크롤하는 동안 uiscrollview 뒤죽박죽

for index in 0..<storyImages.count { 
     frame.origin.x = self.scrollView.frame.size.width * CGFloat(index) 
     frame.size = self.scrollView.frame.size 
     self.scrollView.isPagingEnabled = true 
     print(self.scrollView.bounds.width * CGFloat(index)) 
     let rect = CGRect(x: CGFloat(self.scrollView.bounds.width * CGFloat(index)), y: 0, width: scrollViewWidth , height: scrollViewHeight) 
     let imageView = UIImageView() 
     imageView.frame = rect 
     imageView.image = storyImages[index] 
    self.scrollView.addSubview(imageView)   
    } 

iPad의 수평 스크롤링에서 실행될 때 동일한 응용 프로그램이 완벽하게 작동하지만 모든 응용 프로그램에서 완벽하게 작동합니다. 조언 해주십시오.

+0

여기서 scrollview의 contentSize를 설정 하시겠습니까? – karthikeyan

답변

1

이 목적으로 UICollectionView를 사용하는 것이 훨씬 낫습니다.

가로 방향으로 스크롤 방향을 설정하고 콜렉션보기에서 페이징을 사용합니다. enter image description here

extension FirstViewController: UICollectionViewDataSource { 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) 
     cell.backgroundColor = indexPath.row % 2 == 0 ? #colorLiteral(red: 1, green: 0.3126001954, blue: 1, alpha: 1) : #colorLiteral(red: 0.1411764771, green: 0.3960784376, blue: 0.5647059083, alpha: 1) 
     return cell 
    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 5 
    } 

} 

extension FirstViewController: UICollectionViewDelegateFlowLayout { 

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

} 
+0

이것은 좋은 대답입니다. 콜렉션 뷰는 셀을 재사용하고 스크롤보기와 같이 모든 것을 메모리에 유지하지 않습니다. 스크롤 뷰는 모든 것을 한 번에 렌더링하지는 않지만 여전히 전체 뷰 계층 구조가 메모리에 있습니다. – Alistra

0

간격 그리고 최소 당신은있는 ScrollView의 후행 오프에 있습니다.

self.scrollView.isPagingEnabled = false