无法在双击时缩放图像

时间:2016-06-07 06:40:25

标签: ios objective-c

我按如下方式加载imageview。 self.displayReceivedImageScrollView是头文件中定义的scrollview属性。

   _displayImage.image = nil;


        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) {
            NSData *data0 = [NSData dataWithContentsOfURL: [NSURL URLWithString:savedValue]];
            UIImage *image = [UIImage imageWithData: data0];

            dispatch_sync(dispatch_get_main_queue(), ^(void) {
                _displayImage.image = image;
            });
        });


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

    [doubleTap setNumberOfTapsRequired:2];

    [self.displayReceivedImageScrollView addGestureRecognizer:doubleTap];

以下是我的doubleTap处理程序。我希望当用户双击图像时缩放图像。

- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {

    if(self.displayReceivedImageScrollView.zoomScale > self.displayReceivedImageScrollView.minimumZoomScale)
        [self.displayReceivedImageScrollView setZoomScale:self.displayReceivedImageScrollView.minimumZoomScale animated:YES];
    else
        [self.displayReceivedImageScrollView setZoomScale:self.displayReceivedImageScrollView.maximumZoomScale animated:YES];

}

但问题是,图像没有被缩放。 handleDoubleTap方法根本没有被解雇为什么会这样?

4 个答案:

答案 0 :(得分:1)

您可以通过将视图放入ScrollView并配置:

来启用缩放功能
@property(nonatomic) CGFloat minimumZoomScale;     // default is 1.0
@property(nonatomic) CGFloat maximumZoomScale;     // default is 1.0. must be > minimum zoom scale to enable zooming

答案 1 :(得分:1)

您正在为图像视图添加识别器。

您是否启用了用户互动?

self.displayReceivedImageScrollView.userInteractionEnabled = YES;

默认情况下,交互将被禁用,因此它不会响应任何触摸事件。

答案 2 :(得分:0)

以下是您在评论中向我提出的示例代码:

以下是如何声明UIPinchGestureRecognizer

-(IBAction)scaleImage:(UIPinchGestureRecognizer *)recognizer{
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale = 1;
}

在您的ViewDidLoad或其他地方,将其添加到您的View或其他内容中:

UIPinchGestureRecognizer *scaleImage =[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scaleImage:)];
[View_Or_Somehting_Else addGestureRecognizer:scaleImage];

注意:识别器的代码以及将其添加到视图中的位置必须位于同一文件中,或者将其作为方法中的参数传递;)

答案 3 :(得分:0)

您需要启用用户交互,只需从界面构建器中选择您的ImageView,然后转到属性检查器并选中相互作用框。

enter image description here