2016-12-14 38 views
4

我有一个UIScrollView,配置了10页以上的页面,其中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

添加所有的意见,滚动视图是不好的表现。管理此类视图的最佳解决方案是使用UICollectionView和自定义UICollectionViewCell s。集合视图将完成您所需的一切 - 重新使用您的视图并根据可见矩形正确放置它们。如果你不能使用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 
相关问题