SORU
21 Kasım 2012, ÇARŞAMBA


Arayüzü Yönünü dönme sırasında bir UİCollectionView içinde contentOffset tutmak

Bir UİCollectionViewController arabirimi yönlendirme değişiklikleri işlemek için çalışıyorum. Başarmak için çalışıyorum ne için istiyorumaynıarayüzü bir dönüş sonra contentOffset. Sınırları değiştirme oranı karşılık değiştirilmesi gerekiyor anlamına gelmektedir.

Bir içerik uzaklığı ile portre başlayarak {iyi bir çalışma olmuş.boyutu.genişlik* 2, 0} ...

UICollectionView in portait

... içeriği manzara da dengelemek için neden {iyi bir çalışma olmuş.boyutu.genişlik* 2, 0} (ve tersi).

UICollectionView in landscape

Yeni ofset hesaplamak sorun değil, ama, nerede veya ne zaman bilmiyorum, pürüzsüz bir animasyon set. Yani ücreti ne yaptığımı willRotateToInterfaceOrientation:duration: düzeni geçersiz ve içeriği didRotateFromInterfaceOrientation: ofset sıfırlama:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                duration:(NSTimeInterval)duration;
{
    self.scrollPositionBeforeRotation = CGPointMake(self.collectionView.contentOffset.x / self.collectionView.contentSize.width,
                                                    self.collectionView.contentOffset.y / self.collectionView.contentSize.height);
    [self.collectionView.collectionViewLayout invalidateLayout];
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;
{
    CGPoint newContentOffset = CGPointMake(self.scrollPositionBeforeRotation.x * self.collectionView.contentSize.width,
                                           self.scrollPositionBeforeRotation.y * self.collectionView.contentSize.height);
    [self.collectionView newContentOffset animated:YES];
}

Bu içerik rotasyon sonra farkı değiştirir.

Nasıl rotasyon sırasında ayarlayabilirim? Çok garip bir davranış içine yeni içerik willAnimateRotationToInterfaceOrientation:duration: ofset ama bu sonuçlar koymaya çalıştım.

Örnek GitHub projem bulunabilir.

CEVAP
11 Mart 2014, Salı


Çözüm 1, "" . buna ek

Eğer ihtiyacın olan şey sadece contentOffset doğru bir pozisyonda biter emin olmak için, UİCollectionViewLayout bir alt sınıfı oluşturmak ve uygulayabilirsiniztargetContentOffsetForProposedContentoffset:yöntem. Örneğin böyle bir sayfa hesaplamak için:

- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset
{
    NSInteger page = ceil(proposedContentOffset.x / [self.collectionView frame].size.width);
    return CGPointMake(page * [self.collectionView frame].size.width, 0);
}

Ama yüz olacak işte sorun bu geçişin animasyon son derece garip. Benim durumumda seninkiyle hemen hemen aynı olan) yapıyorum

Çözüm 2, "pürüzsüz animasyon"

1) İlk tarafından yönetilen olabilir hücre boyutunu ayarlayıncollectionView:düzen:sizeForİtemAtİndexPath:aşağıdaki gibi yöntem temsilci

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout  *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return [self.view bounds].size;
}

[Self.not sınırları view] cihaza göre rotasyon değişecek.

2) cihaz döndürmek üzereyken, toplama üzerine bir imageView tüm boyutlarını maskeli görünümü ekliyorum. Bu görünüm aslında collectionView tuhaflık üstüne (çünkü) gizler ve willRotatoToİnterfaceOrientation beri: yöntem buna göre döndürme animasyon bir blok içinde verilir. Ayrıca gösterilen indexPath göre bir sonraki contentOffset tutuyorum rotasyon yapıldıktan sonra contentOffset yapabilmem için:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                duration:(NSTimeInterval)duration
{
    // Gets the first (and only) visible cell.
    NSIndexPath *indexPath = [[self.collectionView indexPathsForVisibleItems] firstObject];
    KSPhotoViewCell *cell = (id)[self.collectionView cellForItemAtIndexPath:indexPath];

    // Creates a temporary imageView that will occupy the full screen and rotate.
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[[cell imageView] image]];
    [imageView setFrame:[self.view bounds]];
    [imageView setTag:kTemporaryImageTag];
    [imageView setBackgroundColor:[UIColor blackColor]];
    [imageView setContentMode:[[cell imageView] contentMode]];
    [imageView setAutoresizingMask:0xff];
    [self.view insertSubview:imageView aboveSubview:self.collectionView];

    // Invalidate layout and calculate (next) contentOffset.
    contentOffsetAfterRotation = CGPointMake(indexPath.item * [self.view bounds].size.height, 0);
    [[self.collectionView collectionViewLayout] invalidateLayout];
}

UİCollectionViewCell benim alt imageView ortak bir özelliği olduğunu unutmayın.

3) son olarak, son adım "snap" içeriği geçerli bir sayfa ve geçici imageview kaldırmak için ofset.

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [self.collectionView setContentOffset:contentOffsetAfterRotation];
    [[self.view viewWithTag:kTemporaryImageTag] removeFromSuperview];
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • BruBearBaby

    BruBearBaby

    25 Ocak 2011
  • MrSuicideSheep

    MrSuicideShe

    9 NİSAN 2010
  • WPBeginner - WordPress Tutorials

    WPBeginner -

    17 Temmuz 2009