2016-12-14 17 views
4

10ページ以上のUIScrollViewを設定して、3ページの内容が一度に画面に表示されるようにしました(下のスクリーンショット)。機能については UIScrollViewで非アクティブなページを一時的に非表示にする方法

enter image description here

は、私は一時的に画面上の非アクティブなページを無効にする必要があり、そしてすべての非アクティブなページを隠し、そして唯一の目に見えるアクティブなページを維持する方法がある場合、私は思っていました。

これが実現できない場合は、アクティブなページにあるすべてのビューを抽出することはできますか?

ありがとうございます!

+2

をあなたは、横軸に制限UICollectionViewを、使用して考え、そしてカスタムUICollectionViewCellsにページを作ることがありますか?それはあなたのためにすべての重労働を行うだろう。 – Stephen

+0

大きなポイントスティーブン。残念ながら、既存の多くの機能がUIScrollViewテンプレートで作成されており、collectionViewに調整することはできません。 – daspianist

+0

あなたはいつそれらを消滅させますか?スクロールが終了し、スクロールが開始されると表示されます。非アクティブなページはスクロールするとアクティブになるので(何とか表示されるはずです)、どのように見たいのですか? – timaktimak

答えて

2

以下の機能を試してみて、自分の状況に合わせてそれを調整します

func disableInactiveView(){ 
    let offset = scrollView.contentOffset; 
    let frame = scrollView.frame; 

    let shownFrame = CGRect(x: offset.x, y: offset.y , width: frame.size.width, height: frame.size.height) 

    for colorView in scrollView.subviews{ 
     if shownFrame.contains(colorView.frame) { 
      colorView.backgroundColor = UIColor.green; 
     }else{ 
      colorView.backgroundColor = UIColor.red; 
     } 
    } 
} 
1

scrollViewにすべてのビューを追加するには、パフォーマンスのために良いではありません。このようなビューを管理するための最良のソリューションは、UICollectionViewCellのカスタムでUICollectionViewを使用することです。コレクションビューは、必要なものすべてを行います。ビューを再利用し、可視のrectにしたがって正しく配置します。ケースでは、UICollectionViewはそのような何かしようとし使用することはできません。

@interface SampleClass : UIViewController <UIScrollViewDelegate> 

@property (nonatomic) UIScrollView *scrollView; 
@property (nonatomic) NSUInteger viewsCount; 
@property (nonatomic) NSDictionary<NSNumber *, UIView *> *visibleViewsByIndexes; 

@end 

@implementation SampleClass 

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    [self updateViews]; 
    [self hideInactiveViews]; 
} 

- (void)updateViews 
{ 
    CGPoint offset = self.scrollView.contentOffset; 
    CGRect frame = self.scrollView.bounds; 
    CGRect visibleRect = frame; 
    visibleRect.origin.x += offset.x; 

    // Determine indexes of views which should be displayed. 
    NSMutableArray<NSNumber *> *viewsToDisplay = [NSMutableArray new];; 
    for (NSUInteger index = 0; index < self.viewsCount; index++) { 
     CGRect viewFrame = [self frameForViewAtIndex:index]; 
     if (CGRectIntersectsRect(viewFrame, visibleRect)) { 
      [viewsToDisplay addObject:@(index)]; 
     } 
    } 

    // Remove not visible views. 
    NSDictionary<NSNumber *, UIView *> *oldVisibleViewsByIndexes = self.visibleViewsByIndexes; 
    NSMutableDictionary<NSNumber *, UIView *> *newVisibleViewsByIndexes = [NSMutableDictionary new]; 
    for (NSNumber *indexNumber in oldVisibleViewsByIndexes.allKeys) { 
     if (![viewsToDisplay containsObject:indexNumber]) { 
      UIView *viewToRemove = oldVisibleViewsByIndexes[indexNumber]; 
      [viewToRemove removeFromSuperview]; 
     } else { 
      newVisibleViewsByIndexes[indexNumber] = oldVisibleViewsByIndexes[indexNumber]; 
     } 
    } 

    // Add new views. 
    for (NSNumber *indexNumber in viewsToDisplay) { 
     if (![oldVisibleViewsByIndexes.allKeys containsObject:indexNumber]) { 
      UIView *viewToAdd = [self viewAtIndex:indexNumber.unsignedIntegerValue]; 
      viewToAdd.frame = [self frameForViewAtIndex:indexNumber.unsignedIntegerValue]; 
      [self.scrollView addSubview:viewToAdd]; 
     } 
    } 
    self.visibleViewsByIndexes = newVisibleViewsByIndexes; 
} 

- (CGRect)frameForViewAtIndex:(NSUInteger)index 
{ 
    // Return correct frame for view at index. 
    return CGRectZero; 
} 

- (UIView *)viewAtIndex:(NSUInteger)index 
{ 
    return [UIView new]; 
} 

- (void)hideInactiveViews 
{ 
    CGPoint offset = self.scrollView.contentOffset; 
    CGRect frame = self.scrollView.bounds; 
    CGRect visibleRect = frame; 
    visibleRect.origin.x += offset.x; 

    for (UIView *view in self.visibleViewsByIndexes.allValues) { 
     if (CGRectContainsRect(visibleRect, view.frame)) { 
      // Active view. 
      view.alpha = 1; 
     } else { 
      // Inactive view. 
      view.alpha = 0.2; 
     } 
    } 
} 

@end 
関連する問題