다른 해결책을 추가하겠습니다. 스냅 기능이 완벽하지는 않습니다 (페이징 기능이 설정된 경우만큼 좋지는 않지만 충분히 잘 작동합니다).
나는 Soroush의 솔루션을 구현하려했지만 나에게 적합하지 않습니다.
UICollectionView
은 그것이 중요한 UIScrollViewDelegate
방법에 응답 할 것입니다 UIScrollView
의 서브 클래스이기 때문에 :
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
withVelocity:(CGPoint)velocity
targetContentOffset:(inout CGPoint *)targetContentOffset
targetContentOffset
(AN 포인터 입출력)를 사용하면 컬렉션 뷰의 정지 점을 재정의 할 수있는 (가로로 스 와이프하면이 경우 x
).
변수의 몇 가지에 대한 빠른 노트는 아래의 발견 :
self.cellWidth
-이 컬렉션 뷰 셀의 폭 (당신이 원하는 경우에 당신도 거기에 하드 코드 수)입니다
self.minimumLineSpacing
- 이것은 셀 사이에 설정하는 최소 줄 간격입니다.
self.scrollingObjects
은 컬렉션보기에 포함 된 개체의 배열입니다.
그래서, 생각과 같이 컬렉션 뷰의 위임에이 방법을 구현하는 것입니다) 스크롤을 중지 할 때 알고, 수 :
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
withVelocity:(CGPoint)velocity
targetContentOffset:(inout CGPoint *)targetContentOffset
{
if (self.currentIndex == 0 && velocity.x < 0) {
// we have reached the first user and we're trying to go back
return;
}
if (self.currentIndex == (self.scrollingObjects.count - 1) && velocity.x > 0) {
// we have reached the last user and we're trying to go forward
return;
}
if (velocity.x < 0) {
// swiping from left to right (going left; going back)
self.currentIndex--;
} else {
// swiping from right to left (going right; going forward)
self.currentIndex++;
}
float xPositionToStop = 0;
if (self.currentIndex == 0) {
// first row
xPositionToStop = 0;
} else {
// one of the next ones
xPositionToStop = self.currentIndex * self.cellWidth + (self.currentIndex + 1) * self.minimumLineSpacing - ((scrollView.bounds.size.width - 2*self.minimumLineSpacing - self.cellWidth)/2);
}
targetContentOffset->x = xPositionToStop;
NSLog(@"Point of stopping: %@", NSStringFromCGPoint(*targetContentOffset));
}
당신이 가질 수있는 피드백을 기대한다 더 나은 장소에서 스냅을 만듭니다.나는 또한 제대로 iOS7에의로서 기본이되는 콜렉션 뷰를 선택하고 탭 이벤트 ... 더 나은 솔루션을 찾고에 스위프트의
이를하지만, 전달하지 않습니다하겠습니다. 어떤 아이디어? – eanticev
iOS7에서 (적어도 전에는 사실 일 수 있음) 스크롤 뷰가 필요하지 않습니다. 왼쪽 및 오른쪽 슬쩍 제스처를 감지하여 컬렉션보기 1 페이지 앞으로/뒤로 오프셋을 설정하는 데 사용하십시오. – buildsucceeded
나는 내 인생이 iOS7에서 작동하도록 할 수는 없다. "비밀"스크롤 뷰의'scrollViewDidScroll'은 전달되지 않습니다. –