iPhone - 缩放视图

时间:2013-01-04 07:27:44

标签: iphone ios zoom

我有一个UIView。在那之上,我添加了一个UIImageView作为子视图。我想在Iam触摸UIImageview时放大主视图。当没有触摸时,它应该缩小。我怎么能这样做?

3 个答案:

答案 0 :(得分:0)

以下是viewcontroller.h文件的代码

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{
    UIImage *image;
    UIImageView *img;
    UIPinchGestureRecognizer *twoFingerPinch;
}

@end

遵循viewcontroller.m文件的代码

在viewDidLoad方法中用于初始化图像视图和UIPinchGestureRecognizer。

- (void)viewDidLoad
{
    [super viewDidLoad];
    zoomInButton=[[UIButton alloc]init];
    zoomOutButton=[[UIButton alloc]init];
    // Do any additional setup after loading the view, typically from a nib.
    image = [UIImage imageNamed:@"demo.png"];
    img = [[UIImageView alloc] initWithImage:image];
    img.userInteractionEnabled = YES;
    img.backgroundColor = [UIColor clearColor];

    img.contentMode =  UIViewContentModeCenter;
    img.frame = CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height);
    [self.view addSubview:img];

    twoFingerPinch = [[UIPinchGestureRecognizer alloc]
                      initWithTarget:self
                      action:@selector(twoFingerPinch:)];
    [img addGestureRecognizer:twoFingerPinch];
}

并且访问用于缩放图像视图的放大/缩小方法,这也用于UIViewController。

- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer
{
    //    NSLog(@"Pinch scale: %f", recognizer.scale);
    if (recognizer.scale >1.0f && recognizer.scale < 2.5f) {
        CGAffineTransform transform = CGAffineTransformMakeScale(recognizer.scale, recognizer.scale);
        img.transform = transform;
    }
}

答案 1 :(得分:0)

你要做的是给mainView的标签100,然后写下面的代码

   - (void)viewDidLoad
  {
        UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] init];
         [pinchRecognizer setDelegate:self];
        [self.view addGestureRecognizer:pinchRecognizer];
  }



  -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
     UITouch    *touch = [touches anyObject];
      if (!([touch view].tag == 100)) {




if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
    lastScale = 1.0;//lastscale is CGFLoat defiend in .h file
}

CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);

CGAffineTransform currentTransform = babyImage.transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);

[babyImage setTransform:newTransform];
lastScale = [(UIPinchGestureRecognizer*)sender scale];


     }
 }

让我知道它的工作与否!!!!!

快乐编码!!!!!!!!!

答案 2 :(得分:0)

要放大图像,可以使用UIScrollView作为UIImageView的父视图。

  • 根据您的要求设置滚动视图的最小和最大缩放属性。我根据最大图像大小将最小比例设置为1,将最大比例设置为2/3。
  • 您还需要实施UIScrollViewDelegate并在实施时

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

    返回您的UIImageView插座对象。

  • 您还可以在滚动视图上实现点击手势识别器,以便在双击时缩放到原始尺寸。

这将照顾您的缩放。享受。