我应该如何在PinchGestureRecognizer上保持缩放比例?

时间:2015-09-07 06:29:24

标签: ios objective-c iphone c xcode

我想通过PinchGestureRecognizer制作缩放功能。 我可以通过此代码进行扩展,但每当我捏出时,imageView都会恢复。 我想制作一个功能,如果我有时缩放imageView,它不会每次都恢复。 代码scrollView.minimumZoomScale = 3.0scrollView.maximumZoomScale = 6.0无效。

我该怎么办?

这是我的代码

        scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        scrollView.frame = self.view.bounds;
        scrollView.backgroundColor = [UIColor blueColor];
        scrollView.contentSize = CGSizeMake(imgView.bounds.size.width+100, imgView.bounds.size.height);
        scrollView.frame = CGRectMake(0,50,320,500);
        scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        scrollView.pagingEnabled = YES;
        scrollView.bouncesZoom = YES;
        scrollView.minimumZoomScale = 3.0;
        scrollView.maximumZoomScale = 6.0;
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:scrollView];

        UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]
                                                  initWithTarget:self action:@selector(handlePinchGesture:)];
        [scrollView addGestureRecognizer:pinchGesture];

            img =[UIImage imageNamed:[NSString stringWithFormat:@"a.jpg"]];
            imgView = [[UIImageView alloc]initWithImage:img];
            imgView.frame = CGRectMake(0,0, self.view.frame.size.width, 448);
            imgView.userInteractionEnabled = YES;
            [scrollView imgView];


- (void)handlePinchGesture:(UIPinchGestureRecognizer *)sender {
    CGFloat factor = [(UIPinchGestureRecognizer *)sender scale];
    imgView.transform = CGAffineTransformMakeScale(factor, factor);
    NSLog(@"factor %f",factor);

}

3 个答案:

答案 0 :(得分:0)

尝试使用CGAffineTransformScale代替:

imgView.transform = CGAffineTransformScale(imgView.transform, factor, factor);

答案 1 :(得分:0)

试用此代码

    UIScrollView   *scrollview =[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,imageViewHeight)];
    scrollview.showsVerticalScrollIndicator=YES;
    scrollview.scrollEnabled=YES;
    scrollview.maximumZoomScale = 3.0;
    scrollview.delegate = self;
    scrollview.userInteractionEnabled=YES;
    [self.view addSubview:scrollview];`

答案 2 :(得分:0)

注意:这只是达到要求的另一种方式

如果您正在使用UIScrollView,则无需添加UIPinchGestureRecognizer来默认滚动视图,它具有捏合手势。

用于缩放,需要覆盖委托方法

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

- (CGRect)zoomRectForScrollView:(UIScrollView *)scrollView withScale:(float)scale withCenter:(CGPoint)center

例如

    //initilization
    scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.frame = self.view.bounds;
    scrollView.backgroundColor = [UIColor blueColor];
    scrollView.contentSize = CGSizeMake(imgView.bounds.size.width+100, imgView.bounds.size.height);
    scrollView.frame = CGRectMake(0,50,320,500);
    scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    scrollView.pagingEnabled = YES;
    scrollView.bouncesZoom = YES;
    scrollView.minimumZoomScale = 3.0;
    scrollView.maximumZoomScale = 6.0;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;

    scrollView.delegate = self; /* add this line */

    [self.view addSubview:scrollView];


//this view will be scaled accordingly
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return imgView; //return the view which is the subview of scroll view need to be zoomed 
}

//it is the rect for the scaling
- (CGRect)zoomRectForScrollView:(UIScrollView *)scrollView withScale:(float)scale withCenter:(CGPoint)center
{
   CGRect zoomRect;

   zoomRect.size.height = scrollView.frame.size.height / scale;
   zoomRect.size.width  = scrollView.frame.size.width  / scale;

   zoomRect.origin.x = center.x - (zoomRect.size.width  / 2.0);
   zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);
   return zoomRect;
}