Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在PinchGestureRecognitor上保持缩放比例?_Ios_Objective C_Iphone_C_Xcode - Fatal编程技术网

Ios 如何在PinchGestureRecognitor上保持缩放比例?

Ios 如何在PinchGestureRecognitor上保持缩放比例?,ios,objective-c,iphone,c,xcode,Ios,Objective C,Iphone,C,Xcode,我想通过pinchgestrerecognizer实现缩放功能。 我可以根据这段代码进行缩放,但只要我一剪掉,imageView就会恢复。 我想做一个函数,如果我有时缩放imageView,它不会每次都恢复。 代码scrollView.minimumZoomScale=3.0,scrollView.maximumZoomScale=6.0不起作用 我该怎么办 这是我的密码 scrollView = [[UIScrollView alloc] initWithFrame:self.

我想通过
pinchgestrerecognizer
实现缩放功能。 我可以根据这段代码进行缩放,但只要我一剪掉,imageView就会恢复。 我想做一个函数,如果我有时缩放imageView,它不会每次都恢复。 代码
scrollView.minimumZoomScale=3.0
scrollView.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);

}

尝试使用
CGAffineTransformScale
,例如:

imgView.transform = CGAffineTransformScale(imgView.transform, factor, factor);
试试这个代码

    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];`

注意:这只是实现您需求的另一种方式

有一件事,如果您使用的是UIScrollView,则无需添加
UIPinchGestureRecognizer
来滚动视图,默认情况下,它具有收缩手势

对于缩放,需要重写委托方法

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

比如说

    //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;
}

可能是我的复制品,我能做到!非常感谢你!我认为,当你使用自己的捏手姿势时,最小的缩放并没有效果。进行转换后,确保
imgView
的帧在某些定义的限制范围内,例如,确保宽度<600,高度<400或其他,如果其较大,则减小其大小。大概
    //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;
}