UICollectionView - resizing cells on device rotate - Swift

后端 未结 9 1578
醉梦人生
醉梦人生 2020-12-04 23:12

I\'ve created a UICollectionView, so that I can arrange views into neat columns. I\'d like there to be a single column on devices > 500 pixels wide.

In order to achi

9条回答
  •  借酒劲吻你
    2020-12-04 23:56

    You can create a mask content and move it to the collectionView. After the landscape/portrait animation is finished, you must remove it ASAP.

    Here is an example:

    @property (strong, nonatomic) UIImageView *maskImage;
    
    .........
    
    - (UIImageView *) imageForCellAtIndex: (NSInteger) index {
        UICollectionView *collectionView = self.pagerView.test;
        FSPagerViewCell *cell = nil;
        NSArray *indexPaths = [collectionView indexPathsForVisibleItems];
        for (NSIndexPath *indexPath in indexPaths) {
            if (indexPath.item == index) {
                cell = (FSPagerViewCell *)[collectionView cellForItemAtIndexPath: indexPath];
                break;
            }
        }
        if (cell) {
            return cell.imageView;
        }
        return nil;
    }
    
    
    - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator
    
    {
        [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    
        [coordinator animateAlongsideTransition:^(id context)
         {
             UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
             [self test_didRotateFromInterfaceOrientation: orientation];
    
             UIImageView *imageView = [self imageForCellAtIndex: self.pagerView.currentIndex];
             if (imageView) {
                     UIImageView *imageView = [self imageForCellAtIndex: self.pagerView.currentIndex];
                     CGSize itemSize = self.pagerView.itemSize;
                     UIImageView *newImage = [[UIImageView alloc] initWithImage: imageView.image];
                     [newImage setFrame: CGRectMake((_contentView.bounds.size.width - itemSize.width)/2.0f, 0, itemSize.width, itemSize.height)];
                     newImage.contentMode = imageView.contentMode;
                     newImage.clipsToBounds = imageView.clipsToBounds;
                     newImage.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
                     [self.pagerView addSubview: newImage];
    
                     self.maskImage = newImage;
             }
    
             [self.pagerView.test performBatchUpdates:^{
                 [self.pagerView.test setCollectionViewLayout:self.pagerView.test.collectionViewLayout animated:YES];
             } completion:nil];
             // do whatever
         } completion:^(id context)
         {
             [self.maskImage removeFromSuperview];
         }];
    }
    

提交回复
热议问题