在缩放

时间:2016-04-11 16:34:57

标签: ios objective-c uiview uiscrollview

我使用以下代码以编程方式创建了一个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的平移?所有帮助赞赏。

1 个答案:

答案 0 :(得分:1)

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