UIScrollView 只向下滚动

Posted

技术标签:

【中文标题】UIScrollView 只向下滚动【英文标题】:UIScrollView scrolls only downward 【发布时间】:2011-08-03 16:31:25 【问题描述】:

我正在实现一个带有嵌套 UIScrollView 的自定义 UIView。

初始化如下:

- (id)initWithFrame:(CGRect)frame 
    self = [super initWithFrame:frame];
    if (self) 
        scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 10, 252, 200)];        
        scrollView.backgroundColor = [UIColor clearColor];
        scrollView.contentSize = CGSizeMake(500, 400); 
        scrollView.userInteractionEnabled = YES;
        scrollView.pagingEnabled = NO;
        scrollView.scrollEnabled = YES;
        scrollView.clipsToBounds = YES;
        [scrollView setAutoresizesSubviews:YES];
        [scrollView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
        [scrollView setDirectionalLockEnabled:NO];
        [scrollView setDelegate:self];

        [scrollView setShowsHorizontalScrollIndicator:YES];
        [scrollView setShowsVerticalScrollIndicator:YES];
        [scrollView setBounces:YES];
        [scrollView setAlwaysBounceHorizontal:NO];
        [scrollView setAlwaysBounceVertical:NO];
        [scrollView setBouncesZoom:YES];
        [scrollView setDelaysContentTouches:YES];
        [scrollView setCanCancelContentTouches:YES];

        [scrollView setMaximumZoomScale:1];
        [scrollView setMinimumZoomScale:1];
        [scrollView setOpaque:YES];

        [self addSubview:scrollView];                            
    
    return self;

在控制器中:

CustomView *customView = [[CustomView alloc] initWithFrame:CGRectMake(0, 100, 320, 200)];
[self.view addSubview:cut];

问题:滚动视图仅向上滚动(当我向下拖动手指时),当我尝试向左、向右或向下滚动时没有动作

我做错了什么?

【问题讨论】:

【参考方案1】:

您设置的许多属性与默认值相同。您可以在此处找到默认值:UIScrollView Class Reference

我的建议是删除已经具有默认值的属性。特别是滚动的属性。

[scrollView setShowsHorizontalScrollIndicator:YES];
[scrollView setShowsVerticalScrollIndicator:YES];
[scrollView setBounces:YES];
[scrollView setAlwaysBounceHorizontal:NO];
[scrollView setAlwaysBounceVertical:NO];
[scrollView setBouncesZoom:YES];
[scrollView setDelaysContentTouches:YES];
[scrollView setCanCancelContentTouches:YES];

【讨论】:

第一个初始化是这样的:scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 10, 252, 200)]; scrollView.backgroundColor = [UIColor clearColor]; scrollView.contentSize = CGSizeMake(500, 400); scrollView.userInteractionEnabled = YES; scrollView.pagingEnabled = 否; scrollView.scrollEnabled = YES;和行为是一样的

以上是关于UIScrollView 只向下滚动的主要内容,如果未能解决你的问题,请参考以下文章

开始时从底部锚定的 UIScrollview

UIScroll 视图 + UIView + UITableview 问题

调整容器 uiscrollview 中所有 uiscrollview 的大小

何时使用 UITouch 与 UIScroll

UIScrollView 不使用 RubyMotion 滚动

滚动 UIScrollView 时如何缩放