ios - 用户完成滚动后,UIScrollView 不会停留在原地

标签 ios

在我的 UIView 中,我有一个填充第一个 View 的 UIScrollView,因此当内容大于 iPhone 屏幕大小时,用户可以向下滚动页面。它运行良好,但是当用户完成滚动运动时——即移开他的手指,页面会弹回到原来的位置。显然那不是我想要的,如何避免?

这是声明和使用UIScrollView类的UIView类中的相关代码。

@implementation TestView


- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
    }

    CGRect scrollViewFrame = CGRectMake(0, 0, 320, 460);
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
    scrollView.canCancelContentTouches=NO;
    [self addSubview:scrollView];

    CGSize scrollViewContentSize = CGSizeMake(320, 500);
    [scrollView setContentSize:scrollViewContentSize];

    CGRect rectForBigRedSquare = CGRectMake(50, 50, 200, 200);
    UILabel *redSquare = [[UILabel alloc] initWithFrame:rectForBigRedSquare];
    [redSquare setBackgroundColor:[UIColor redColor]];

    [scrollView addSubview:redSquare];
    return self;
}

另一个问题是:如何让用户只能向下滚动,即看到底部不在 View 中的内容,但不能向上滚动,以便之前有空间内容的开始。在

最佳答案

基本上你只需要根据内容设置你的scrollview的contentSize。

CGSize scrollViewSize = CGSizeMake(newWidth, newHeight);
[self.myScrollView setContentSize:scrollViewSize];

关于ios - 用户完成滚动后,UIScrollView 不会停留在原地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8180087/

相关文章:

ios - 将手势识别器添加到 UIImage,而不是 UIImageView

ios - -[UIScrollView zoomToRect :animated:] weird behavior when contentSize < bounds. 大小

ios - 导航 Controller 无法正常工作

objective-c - UITableView 因 reloadRowsAtIndexPaths 崩溃

ios - 从Xcode 10和iOS 12开始的Fabric Answer崩溃iOS应用

ios - 如何自动计算tableview单元格高度和标签高度

ios - 滚动 uitableview 时单元格 subview 消失

iphone - 检查 iOS 中 Safari 的内存使用情况

ios - 显然,CLLocation 对象不能精确地归档/取消归档

ios - 同一项目中的多个目标 - 但不是精简版与完整版