3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

下にあるViewのrecognizerを利用する

Posted at

2つのUIViewが重なっているときに、上のViewに設定したrecognizerから下のViewのrecognizerを呼び出してみた。

view1の引き数の値はview2のものなので、View1の外側をタッチしてもPanやPanができてしまう。
タッチされた位置を把握する処理を付け加えればいいのかもしれない。


@implementation XXXViewController
{
    UIView *_view1;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // view1 -- view2の下側にある
    _view1 = [[UIView alloc] initWithFrame:CGRectMake(100, 150, 100, 100)];
    [_view1 setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:_view1];

    UIPinchGestureRecognizer *pinch1 = [[UIPinchGestureRecognizer alloc]
                                       initWithTarget:self action:@selector(handlePinch1:)];
    [_view1 addGestureRecognizer:pinch1];
    UIPanGestureRecognizer *pan1 = [[UIPanGestureRecognizer alloc]
                                    initWithTarget:self action:@selector(handlePan1:)];
    [_view1 addGestureRecognizer:pan1];
    
    
    // view2 -- alpha0.5でview1の上を覆っている。
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(50, 100, 200, 200)];
    [view2 setBackgroundColor:[UIColor blueColor]];
    view2.alpha = 0.5;
    [self.view addSubview:view2];
    
    UIPinchGestureRecognizer *pinch2 = [[UIPinchGestureRecognizer alloc]
                                       initWithTarget:self action:@selector(handlePinch2:)];
    [view2 addGestureRecognizer:pinch2];
    UIPanGestureRecognizer *pan2 = [[UIPanGestureRecognizer alloc]
                                    initWithTarget:self action:@selector(handlePan2:)];
    [view2 addGestureRecognizer:pan2];
}

// view1用:引き数recognizerの値はview2の値なので、正しい位置情報ではない。
-(void)handlePinch1:(UIPinchGestureRecognizer *)recognizer
{
    _view1.transform = CGAffineTransformScale(_view1.transform, recognizer.scale, recognizer.scale);
    recognizer.scale = 1;
}
- (void)handlePan1:(UIPanGestureRecognizer *)recognizer
{
    CGPoint transPoint = [recognizer translationInView:self.view];
    _view1.center = CGPointMake(_view1.center.x + transPoint.x, _view1.center.y + transPoint.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}
// view2用:view1のrecognizerを呼ぶだけ
-(void)handlePinch2:(UIPinchGestureRecognizer *)recognizer
{
    [self handlePinch1:recognizer];
}
- (void)handlePan2:(UIPanGestureRecognizer *)recognizer
{
    [self handlePan1:recognizer];
}

@end
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?