【问题标题】:Cannot pan UIScrollView after zooming缩放后无法平移 UIScrollView
【发布时间】:2016-04-11 16:34:57
【问题描述】:

我使用以下代码以编程方式创建了一个 UIScrollView:

.h:

@interface MainController : UIViewController <UIScrollViewDelegate>

@property (nonatomic, retain) UIScrollView *mainScrollView;
@property (nonatomic, retain) UIView *mainView;

.m:

- (void) initVariables
{
    // Setters

    screenRect = [[UIScreen mainScreen] bounds];
    screenWidth = screenRect.size.width;
    screenHeight = screenRect.size.height;

    // Alloc-init methods
    mainScrollView = [[UIScrollView alloc] init];
    mainView = [[UIView alloc] init];
}

- (void) setUpScrollView
{
    mainScrollView.frame = CGRectMake(0.0f, 0.0f, screenWidth, screenHeight);
    mainScrollView.contentSize = CGSizeMake(screenWidth*2, screenHeight*2);
    mainScrollView.minimumZoomScale = 1.0;
    mainScrollView.maximumZoomScale = 4.0;
    mainScrollView.backgroundColor = [UIColor blueColor];
    mainScrollView.scrollEnabled = YES;
    mainScrollView.delegate = self;

    mainView.frame = CGRectMake((screenWidth/2) - 25.0f, (screenHeight/2) - 25.0f, 50.0f, 50.0f);

    UIImageView *testImageView = [[UIImageView alloc] init];
    testImageView.backgroundColor = [UIColor redColor];
    testImageView.frame = CGRectMake(0.0f, 0.0f, 50.0f, 50.0f);

    // Add subviews

    [self.view addSubview:mainScrollView];
    [mainScrollView addSubview:mainView];
    [mainView addSubview:testImageView];

}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return mainView;
}

现在缩放效果很好。但是,我只能在 UIScrollView 处于最小缩放时进行平移。在所有其他时间,当我捏拉放大时,当我松开手指时,UIView 会稍微偏离中心,尽管我在缩放时处于中心位置,并且我无法再平移 UIScrollView。

另外,放大后,如果我再次缩小到最小缩放,我仍然无法平移。

TL;DR - 换句话说,UIScrollView 的平移仅在使用捏合缩放功能之前,之后它会中断。

如何防止这种情况发生,并让 UIScrollView 的平移始终处于启用状态?所有帮助表示赞赏。

【问题讨论】:

    标签: ios objective-c uiview uiscrollview


    【解决方案1】:

    尝试在scrollView中添加另一个屏幕大小相同的视图,然后添加mainView和imageView。它可能会解决你的问题。 :)

    【讨论】:

    • 该死的,不知道这是怎么回事!非常感谢哥们:)
    • 原因是,Scrollview 根据内容的大小滚动。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    • 1970-01-01
    • 1970-01-01
    • 2019-05-23
    • 1970-01-01
    相关资源
    最近更新 更多