UIPanGestureRecognizer UIView insertSubview下面的子视图

时间:2014-06-19 12:27:33

标签: ios objective-c uibutton uipangesturerecognizer

我为UIButton添加了PanGestureRecognizer,
我不明白以下几点:

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
switch([recognizer state]){
    case  UIGestureRecognizerStateBegan: {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [button setFrame:recognizer.view.frame];
        [button setBackgroundColor: [UIColor redColor]];

//      [self.view insertSubview:button belowSubview:recognizer.view];
        _tempButton = button;

    }break;
    case  UIGestureRecognizerStateChanged: {

        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];

    }break;
}
}

在上面的例子中,一切顺利。开始。停止。从先前位置拖动。

但如果我取消注释这些行

//      [self.view insertSubview:button belowSubview:recognizer.view];
发生了一些奇怪的事情 1)开始拖动UIButton。红色的uibutton被添加到视图中。 NORMAL
2)停止拖动UIButton。这是正常 3)当我从先前位置(上一步)再次开始拖动UIButton时,会创建一个红色按钮下方的新按钮,此按钮开始作为原点按钮移动。我在第2步之后离开的按钮消失了。的 STRANGE

first step, normal second step, something strange

为什么?

0 个答案:

没有答案