在夹入期间,UIScrollView会自动向下滚动?

时间:2015-02-25 14:45:30

标签: ios objective-c iphone uiscrollview

我有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];
}

2 个答案:

答案 0 :(得分:0)

我将假设您正在使用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;
}

希望这有帮助。

答案 1 :(得分:0)

这是我的代码,这可能会有所帮助

   - (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;
            }];
        }
    }