2016-08-04 3 views
2

가로로 미끄러지는 콜렉션 뷰를 사용하고 있으며 아이폰 "app closer"와 비슷한 셀의 사용자 정의 레이아웃을 수행하려고합니다. 이와 같은 효과를 얻는 방법이나 시작해야 할 부분이 없으므로 포인터를 얻으려고했습니다. 나는 자신을 설명하는 것이 매우 어렵다. 그래서 나는 그것이 어떻게 행동해야하는지 지금까지의 행동에서부터 원하는 효과를 설명하려고 노력했다.Collectionview 사용자 정의 셀 레이아웃/동작

미리 감사드립니다. 내가 이걸 같은 유사한 전망과 함께 일한

enter image description here

+1

나는 그런 당신이 어떻게 시작하는이어야 아이디어를 얻을 것이다 –

+0

감사 무엇을, 당신은 예 다음은 효과의 많은 종류를 찾을 수 있습니다, https://github.com/nicklockwood/iCarousel 여기에 표시된 참조해야한다고 생각 많이! 이는 맞춤 효과를위한 정확한 엔진과 같습니다. – Imbue

답변

3

내 프로젝트의 GitHub의 link입니다. This is the screenshot of the view 요구 사항과 내보기 간의 유일한 차이점은 왼쪽 셀도 확대해야한다는 것입니다. 두 세포 (왼쪽 셀과 셀을 마우스 오른쪽)가 동일하게 노출되었을 때 그것을 위해 내 CollectionView.m 파일에 다음 코드를 포함 한

1) 정확하게 스크롤을 중지 :이보기를 달성하기 위해 나는 두 가지 문제가 발생했습니다.

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset 
{ 
    float pageWidth = 240; // width + space 

    float currentOffset = scrollView.contentOffset.x; 
    float targetOffset = targetContentOffset->x; 
    float newTargetOffset = 0; 

    if (targetOffset > currentOffset) 
     newTargetOffset = ceilf(currentOffset/pageWidth) * pageWidth; 
    else 
     newTargetOffset = floorf(currentOffset/pageWidth) * pageWidth; 

    if (newTargetOffset < 0) 
     newTargetOffset = 0; 
    else if (newTargetOffset > scrollView.contentSize.width) 
     newTargetOffset = scrollView.contentSize.width; 

    targetContentOffset->x = currentOffset; 
    [scrollView setContentOffset:CGPointMake(newTargetOffset, 0) animated:YES]; 

    int index = newTargetOffset/pageWidth; 

    if (index == 0) { // If first index 
     CollectionViewCell *cell =(CollectionViewCell *) [self cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]]; 
     cell.transform = CGAffineTransformIdentity; 
     cell = (CollectionViewCell *)[self cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index + 1 inSection:0]]; 
     //cell.transform = TRANSFORM_CELL_VALUE; 

    }else{ 
     CollectionViewCell *cell =(CollectionViewCell *)[self cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]]; 
     //cell.transform = CGAffineTransformIdentity; 

     index --; // left 
     cell =(CollectionViewCell *)[self cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]]; 
      // cell.transform = TRANSFORM_CELL_VALUE; 
     index ++; 
     index ++; // right 
     cell = (CollectionViewCell *)[self cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]]; 
     //cell.transform = TRANSFORM_CELL_VALUE; 
    } 
} 

2) 둘째로 당신은 그것을 위해 내가 UICollectionViewFlowLayout 클래스를 사용하고, 요구 사항을 달성하기 위해 셀에 적절한 제약 조건을 제공해야합니다. 여기에서는 보이는 셀에 적절한 제약 조건을 제공합니다. FlowLayout의 코드는 다음과 같습니다

#import "CollectionLayout.h" 


@implementation CollectionLayout 
{ 
    NSIndexPath *mainIndexPath; 
} 

-(void)prepareLayout{ 
    [super prepareLayout]; 
} 

-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    UICollectionViewLayoutAttributes *attributes = [[super layoutAttributesForItemAtIndexPath:indexPath] copy]; 
    CATransform3D theTransform = CATransform3DIdentity; 
    const CGFloat theScale = 1.05f; 
    theTransform = CATransform3DScale(theTransform, theScale, theScale, 1.0f); 
    attributes.transform3D=theTransform; 
    return attributes; 
} 

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds { 
    return YES; 
} 

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{ 
    NSArray *attributesSuper = [super layoutAttributesForElementsInRect:rect]; 
    NSArray *attributes = [[NSArray alloc]initWithArray:attributesSuper copyItems:YES]; 
    NSArray *cellIndices = [self.collectionView indexPathsForVisibleItems]; 
    NSIndexPath *neededIndexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
    for(NSInteger i=0;i<cellIndices.count;i++){ 
     NSIndexPath *indexPath = [cellIndices objectAtIndex:i]; 
     if(indexPath.row>neededIndexPath.row){ 
      neededIndexPath=indexPath; 
     } 
     NSLog(@"%ld,%ld",(long)indexPath.row,(long)indexPath.section); 
    } 
    if(cellIndices.count==0){ 
     mainIndexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
    }else{ 
     if(neededIndexPath.row>0) 
      mainIndexPath = [NSIndexPath indexPathForRow:neededIndexPath.row-1 inSection:0]; 
    } 

    for (UICollectionViewLayoutAttributes *attribute in attributes) 
    { 
     [self applyTransformToLayoutAttributes:attribute]; 
    } 
    return attributes; 
} 

-(void) applyTransformToLayoutAttributes:(UICollectionViewLayoutAttributes *)attribute{ 
    if(attribute.indexPath.row == mainIndexPath.row){ 
     attribute.size = CGSizeMake(self.collectionView.bounds.size.width-40, self.collectionView.bounds.size.height); 
     attribute.zIndex+=10; 
    } 
} 

#pragma mark - Transform related 

@end 

내 github의 링크에서 프로젝트를 복제하면, 당신은 내가 무엇을했는지 쉽게 이해하고 당신은 당신의 view.You 만 제공해야합니다 달성하기 위해 자신의 코드를 작성 할 수 있습니다 이 부분의 제약. 각 셀에 대해 zIndex를 올바르게 조정해야합니다 ().

-(void) applyTransformToLayoutAttributes:(UICollectionViewLayoutAttributes *)attribute{ 
     if(attribute.indexPath.row == mainIndexPath.row){ 
      attribute.size = CGSizeMake(self.collectionView.bounds.size.width-40, self.collectionView.bounds.size.height); 
      attribute.zIndex+=10; 
     } 
    } 

난 당신이 내 포인트를 얻었기를 바랍니다.

참고 : 만 아이폰 5S에 github에 코드를 테스트 한 다른 장치에 대해 조금 조정할 필요가있을 수 있습니다.

+0

고마워요! 그것은 위대한 영감이었고, 나는 예상대로 똑같이 작동하는 내 자신의 변형을했습니다. 그게 대단한 일이야! – Imbue