0

我有一个 UIView。在那个我添加一个 UIImageView 作为子视图。我想在触摸 UIImageview 时放大主视图。当触摸不存在时,它应该缩小。我怎样才能做到这一点?

4

3 回答 3

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;
    }
}
于 2013-01-04T07:29:43.803 回答
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];


     }
 }

让我知道它是否有效!!!!

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

于 2013-01-04T07:51:44.893 回答
0

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

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

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

    返回您的 UIImageView 插座对象。

  • 另外,您可以在滚动视图上实现点击手势识别器,以便在双击时放大原始大小。

这将负责您的缩放。享受。

于 2013-01-04T07:55:19.287 回答