UIScrollView 在捏合时自动向下滚动?
Posted
技术标签:
【中文标题】UIScrollView 在捏合时自动向下滚动?【英文标题】:UIScrollView automatically scrolls down during pinch in? 【发布时间】:2015-02-25 14:45:29 【问题描述】:我有 uiscrollview 并向其添加了图像视图。当我捏住滚动视图时,我可以放大,但滚动视图会自动向下滚动一点。所以我无法缩放 uiimageview 的底部。
UIScrollView *scroll = [[UIScrollView alloc]init];
scroll.minimumZoomScale = 1.0f;
scroll.maximumZoomScale = 16.0f;
scroll.delegate = self;
[scroll setContentSize:CGSizeMake(scroll.frame.size.width, scroll.frame.size.height+2200)];
scroll.frame = self.view.frame;
[scroll setBackgroundColor:[UIColor brownColor]];
[self.view addSubview:scroll];
UIImageView *img = [[UIImageView alloc]init];
[img setBackgroundColor:[UIColor yellowColor]];
[img setImage:[UIImage imageNamed:@"add_button.jpg"]];
[img setFrame:CGRectMake(0, 100, self.view.frame.size.width, 200)];
[img setContentMode:UIViewContentModeScaleAspectFit];
[scroll addSubview:img];
- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView
return [[scrollView subviews] firstObject];
【问题讨论】:
【参考方案1】:这是我的代码,这可能会有所帮助
- (void) pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer
UIView *view = self.showImgView;
if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan || pinchGestureRecognizer.state == UIGestureRecognizerStateChanged)
view.transform = CGAffineTransformScale(view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
pinchGestureRecognizer.scale = 1;
else if (pinchGestureRecognizer.state == UIGestureRecognizerStateEnded)
CGRect newFrame = self.showImgView.frame;
newFrame = [self handleScaleOverflow:newFrame];
newFrame = [self handleBorderOverflow:newFrame];
[UIView animateWithDuration:BOUNDCE_DURATION animations:^
self.showImgView.frame = newFrame;
self.latestFrame = newFrame;
];
【讨论】:
【参考方案2】:我将假设您使用 UIPinchGestureRecognizer 进行捏合事件...
您要做的是在方法的开头和结尾添加这些“if”语句:
- (void)handlePinch:(UIPinchGestureRecognizer *)sender
if (sender.state == UIGestureRecognizerStateBegan)
self.scrollView.enabled = NO;
// pinch handling here
if ((sender.state == UIGestureRecognizerStateEnded) || (sender.state == UIGestureRecognizerStateCancelled))
self.scrollView.enabled = YES;
希望这会有所帮助。
【讨论】:
我正在使用 UIScrollView,它具有默认的捏合手势动作。 - (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView return [[scrollView subviews] firstObject]; 根据您在 OP 中的描述,我认为您想将 UIPinchGestureRecognizer 应用于 UIImageView,而不是 UIScrollView。 @Tim Kokesh UiscrollView 默认获得了 UIPinchGestureRecognizer 所以我更喜欢使用它。还有如何裁剪出现在裁剪矩形中的主图像。以上是关于UIScrollView 在捏合时自动向下滚动?的主要内容,如果未能解决你的问题,请参考以下文章