缩放Scrollview的特定区域

时间:2016-05-06 12:08:31

标签: ios objective-c uiscrollview zooming

我遇到了一个案例,我想以编程方式缩放滚动视图的一部分(rect)而不是通过触摸或手势。

for (int i = 0; i <= playerNumber; i++) {
        // here, I want to operate on array called "players" that is   declared in the main method
        Glucksspieltest.players
    }

我已经设置了委托,并且还实现了委托方法。

CGRect cropRect = CGRectMake(xPos * [UIScreen mainScreen].nativeScale, yPos * [UIScreen mainScreen].nativeScale, width1 * [UIScreen mainScreen].nativeScale, height1 * [UIScreen mainScreen].nativeScale);
[self.scrlVPhoto zoomToRect:cropRect animated:YES];

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)aScrollView { return self.containerView; } 是我要缩放的视图。

我已经尝试了一切,但我仍然很困惑如何摆脱这种局面。

请有人帮助我。任何帮助表示赞赏。

提前致谢。

4 个答案:

答案 0 :(得分:1)

我将回答我自己的问题。

我已经搜索过很多文章和数小时的调试我发现当滚动视图被缩放时实际上它的contentSize增加了,它的zoomScale保持不变。

所以我只使用了scrollview的子视图的transform属性(即self.containerView)并设置了scrollview的contentOffset,我得到了我想要的内容。

self.containerView.transform = CGAffineTransformMakeScale(1.8, 1.8); // containerView is subview of scrollview and 1.8 is zoom scale just for eg.
[self.scrlVPhoto setContentOffset:CGPointMake(self.scrlVPhoto.contentOffset.x, (self.scrlVPhoto.contentOffset.y + cropRect.origin.y)) animated:true];

答案 1 :(得分:0)

您应该在scrollview中添加要缩放的视图。在委托方法内部返回要缩放的视图。

答案 2 :(得分:0)

我有创建UIImageView,双击或单击UIImageView时在imageView中设置缩放选项,代码如下:

viewDidLoad方法:

   UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];

    [doubleTap setNumberOfTapsRequired:2];

DoubleTab方法:

- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {
    // zoom in
    float newScale = [myscrollview zoomScale] * 2;

    if (newScale > self.myscrollview.maximumZoomScale){
        newScale = self.myscrollview.minimumZoomScale;
        CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];

        [myscrollview zoomToRect:zoomRect animated:YES];

    }
    else{

        newScale = self.myscrollview.maximumZoomScale;
        CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];

        [myscrollview zoomToRect:zoomRect animated:YES];
    }
}


    - (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {

        CGRect zoomRect;

        // the zoom rect is in the content view's coordinates.
        //    At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds.
        //    As the zoom scale decreases, so more content is visible, the size of the rect grows.
        zoomRect.size.height = [myscrollview frame].size.height / scale;
        zoomRect.size.width  = [myscrollview frame].size.width  / scale;

        // choose an origin so as to get the right center.
        zoomRect.origin.x    = center.x - (zoomRect.size.width  / 2.0);
        zoomRect.origin.y    = center.y - (zoomRect.size.height / 2.0);

        return zoomRect;
    }

您需要委托方法如下所示,

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    if (vmov==nil)
    {

    float newwidth;
    float newheight;

    UIImage *image=detaimageview.image;

    if (image.size.height>=image.size.width){
        newheight=detaimageview.frame.size.height;
        newwidth=(image.size.width/image.size.height)*newheight;

        if(newwidth>detaimageview.frame.size.width){
            float diff=detaimageview.frame.size.width-newwidth;
            newheight=newheight+diff/newheight*newheight;
            newwidth=detaimageview.frame.size.width;

        }

    }
    else{
        newwidth=detaimageview.frame.size.width;
        newheight=(image.size.height/image.size.width)*newwidth;

        if(newheight>detaimageview.frame.size.height){
            float diff=detaimageview.frame.size.height-newheight;
            newwidth=newwidth+diff/newwidth*newwidth;
            newheight=detaimageview.frame.size.height;

            myscrollview.clipsToBounds = NO;

        }
    }
    CGRect frame;
    CGFloat screenWidth;
    CGFloat screenHeight;
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    screenWidth = screenRect.size.width;
    screenHeight = screenRect.size.height-64;
    frame.size.width = newwidth;
    frame.size.height = newheight;
    self.myscrollview.contentSize = frame.size;
    float x,y;
    x=0;
    y=0;
    if (newheight<screenHeight)
    {
        x =screenHeight/2-newheight/2;
    }
    if (newwidth<screenWidth)
    {
        y =screenWidth/2-newwidth/2;
    }
    self.detaimageview.frame = CGRectMake(y,x,newwidth,newheight);

    return self.detaimageview;
    }

    return nil;
}

以上代码是缩放imageView及其为我的项目工作,

希望它有用

答案 3 :(得分:0)

请检查一下。它可能对你有所帮助

 -(void)handlePinchWithGestureRecognizer:(UIPinchGestureRecognizer *)pinchGestureRecognizer{

      //[self.superImage_View setAlpha:0.5];

      if([pinchGestureRecognizer state] == UIGestureRecognizerStateBegan) {
      // Reset the last scale, necessary if there are multiple objects with different scales
      lastScale = [pinchGestureRecognizer scale];
    }

    if ([pinchGestureRecognizer state] == UIGestureRecognizerStateBegan ||
    [pinchGestureRecognizer state] == UIGestureRecognizerStateChanged){



         CGFloat currentScale = [[[pinchGestureRecognizer view].layer valueForKeyPath:@"transform.scale"] floatValue];

    // Constants to adjust the max/min values of zoom
         const CGFloat kMaxScale = 2.0;
         const CGFloat kMinScale = 1.0;

        CGFloat newScale = 1-(lastScale-[pinchGestureRecognizer scale]) ; // new scale is in the range (0-1)
        newScale = MIN(newScale, kMaxScale / currentScale);
        newScale = MAX(newScale, kMinScale / currentScale);
        CGAffineTransform transform = CGAffineTransformScale([[pinchGestureRecognizer view] transform], newScale, newScale);
    [pinchGestureRecognizer view].transform = transform;

    lastScale = [pinchGestureRecognizer scale];  // Store the previous scale factor for the next pinch gesture call


}
   if ([pinchGestureRecognizer state] == UIGestureRecognizerStateEnded) {
          [undoList addObject:cropImage];
    }
}