在整个视图上使用UIPanGestureRecognizer来移动子视图

时间:2012-07-20 21:44:37

标签: ios uigesturerecognizer gesture-recognition uipangesturerecognizer

以下内容创建了一个移动整个视图的手势识别器:

UIPanGestureRecognizer * recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
recognizer.delegate = self;
[self.view addGestureRecognizer:recognizer];

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {

    CGPoint translation = [recognizer translationInView:self.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, 
                                     recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

}

但我想移动其中一个子视图。我不能这样做:

[self.theSubview addGestureRecognizer:recognizer];

因为我想识别整个屏幕上的手势。应该移动的对象可能很小,我不想强​​迫用户“抓住”它。如何修改处理程序方法以仅移动子视图?

1 个答案:

答案 0 :(得分:0)

不会告发' T:

self.theSubview.center = CGPointMake(self.theSubview.center.x + translation.x, 
                                     self.theSubview.center.y + translation.y);

诀窍?