【发布时间】: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