捏缩放图像查看器

时间:2014-03-13 10:04:29

标签: objective-c ios7

我有UIImageView。我需要捏缩放。

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 90, 320, 38)];
[imageView setImage:[UIImage imageNamed:@"accesspanel.png"]];
[self.view addSubview: imageView];

3 个答案:

答案 0 :(得分:10)

您可以在scrollview中添加imageview,并可以使用Scrollview委托来实现此目的

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageView;
}

- (void)viewDidLoad 
{
    [super viewDidLoad];

    self.scrollView.minimumZoomScale=0.5;

    self.scrollView.maximumZoomScale=6.0;

    self.scrollView.contentSize=CGSizeMake(1280, 960);

    self.scrollView.delegate=self;
}

答案 1 :(得分:8)

首先--->在您的图像视图中添加捏合手势 - :

UIPinchGestureRecognizer *pgr = [[UIPinchGestureRecognizer alloc] 
    initWithTarget:self action:@selector(handlePinchGesture:)];
pgr.delegate = self;
[imageView addGestureRecognizer:pgr];

第二--->实施捏手势 - :

- (void)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer {

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

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

      CGFloat currentScale = [[[gestureRecognizer 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 - [gestureRecognizer scale]); // new scale is in the range (0-1)
      newScale = MIN(newScale, kMaxScale / currentScale);
      newScale = MAX(newScale, kMinScale / currentScale);
      CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale);
      [gestureRecognizer view].transform = transform;

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

答案 2 :(得分:0)

UIPinchGestureRecognizer *pinchGestureRecognizer=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGestureDetected:)];
[pinchGestureRecognizer setDelegate:self];
[_third_imageview addGestureRecognizer:pinchGestureRecognizer];

- (void)pinchGestureDetected:(UIPinchGestureRecognizer *)recognizer
{
    UIGestureRecognizerState state = [recognizer state];

    if (state == UIGestureRecognizerStateBegan || state ==UIGestureRecognizerStateChanged)
    {
        CGFloat scale = [recognizer scale];
        [recognizer.view    setTransform:CGAffineTransformScale(recognizer.view.transform, scale, scale)];
        [recognizer setScale:1.0];
    }

}