2

ピンチインすると画像が拡大する処理を作ったのですが、決まった位置が拡大します。UIPinchGestureRecognizerScrollViewを使って拡大機能を作ってみても、真ん中や左上など固定の位置が拡大してしまいます。色々なサンプルも試したのですが、拡大位置が固定しているピンチインのサンプルしか見つけられませんでした。どうすればピンチインする位置を中心として画像を拡大させることができるでしょうか?現在簡単なピンチイン(位置が固定している)のコードを作ったので下記に記します。どなたか分かる方がいれば教えていただきたいです。すみませんが、よろしくお願いします。

ViewController.h

@interface ViewController : UIViewController<UIGestureRecognizerDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

ViewController.m

@interface ViewController () {
    CGAffineTransform currentTransForm;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [self.view addGestureRecognizer:pinch];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)pinchAction : (UIPinchGestureRecognizer *)sender {

    if (sender.state == UIGestureRecognizerStateBegan) {
        currentTransForm = _imageView.transform;
    }

    CGFloat scale = [sender scale];

    _imageView.transform = CGAffineTransformConcat(currentTransForm, CGAffineTransformMakeScale(scale, scale));
}

@end
2
  • それは、サンプルを探すんじゃなくて、プログラマ自身のアイデアで対処する問題かと。UIGestureRecognizer(UIPinchGestureRecognizerのスーパークラス)のメソッド、「 - locationOfTouch:inView: 」を使って、二本指それぞれの座標(CGPoint)を取得できます。足して2で割った、中間位置を、拡大、縮小の中心位置とするプログラムを、お考えください。
    – Harawo
    2015年3月18日 8:46
  • ご回答ありがとうございます。ご提示いただいた方法で自分で作ってみたいと思います。
    – tarofess
    2015年3月18日 9:20

0

この質問に回答するには、ログインする必要があります。

のタグが付いた他の質問を参照する。