UIPanGestureRecognizer和BringSubviewToFront

时间:2013-04-13 22:49:54

标签: ios6 uipangesturerecognizer

我有一个应用程序,用户点按一个按钮,应用程序将实例化图像层( UIImageView )。我想这样做,以便用户可以移动选定的图像层,无论他们点击谁。在这里阅读了一些主题后,我了解到我可以使用 UIPanGestureRecognizer 来移动选定的图像层。

- (IBAction)buttonClicked:(id)sender {
    imageview = [[UIImageView alloc] initWithFrame:CGRectMake(100, 0, 300, 22)];
    UIPanGestureRecognizer *imageviewGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(recognizePan:)];
    [imageviewGesture setMinimumNumberOfTouches:1];
    [imageviewGesture setMaximumNumberOfTouches:1];
    imageview.image = [UIImage imageNamed:@"pinimage.png"];
    [imageview addGestureRecognizer:imageviewGesture];
    [imageview setUserInteractionEnabled:YES];
    [self.view addSubview:imageview];
    NSUInteger currentag = [self UniqueTag]; // Assigning a unique integer
    imageview.tag = currentag;
}

- (void)recognizePan:(UIPanGestureRecognizer *)sender {
    [[[(UITapGestureRecognizer *)sender view] layer] removeAllAnimations];
    [self.view bringSubviewToFront:[(UIPanGestureRecognizer *)sender view]];
    CGPoint translatedPoint = [(UIPanGestureRecognizer *)sender translationInView:self.view];

    if([(UIPanGestureRecognizer *)sender state] == UIGestureRecognizerStateBegan) {     
        firstX = [[sender view] center].x;
        firstY = [[sender view] center].y;
    }

    translatedPoint = CGPointMake(firstX + translatedPoint.x,firstY + translatedPoint.y);
    [[sender view] setCenter:translatedPoint];
}

现在,我无法弄清楚当有多个图像层时如何将抽头图层置于前面。似乎[self.view bringSubviewToFront:[(UIPanGestureRecognizer *)sender view]]无效。那么我该如何修改我的代码,以便应用程序将窃听层置于其他顶层?

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

这样做的好方法是创建UIImageView的CustomImageView子类。您将UIPanGestureRecognizer附加到CustomImageView的每个实例,并将该实例设置为它的目标。然后由手势触发的动作方法在视图中实现,以便您可以使用self引用视图:

buttonClicked

    MyImageView* imageview = [[MyImageView alloc] initWithFrame:CGRectMake(100, 0, 300, 22)];
    UIPanGestureRecognizer *imageviewGesture = 
    [[UIPanGestureRecognizer alloc] initWithTarget:imageview 
                                            action:@selector(recognizePan:)];

CustomImageView.m

    - (void)recognizePan:(UIPanGestureRecognizer *)sender {
        [self.layer removeAllAnimations];
        [self.superview bringSubviewToFront:self];
        CGPoint translatedPoint = [sender translationInView:self];
        if([sender state] == UIGestureRecognizerStateBegan) {
            self.firstX = [self center].x;
            self.firstY = [self center].y;
        }
        translatedPoint = CGPointMake(self.firstX + translatedPoint.x,
                                      self.firstY + translatedPoint.y);
        [self setCenter:translatedPoint];
    }

更新

不直接思考 - 您当然可以通过访问gestureRecongnizer的view属性,从viewController中执行此操作。你的错误就在这里:

- (void)recognizePan:(UIPanGestureRecognizer *)sender {
     [[[(UITapGestureRecognizer *)sender view] layer] removeAllAnimations];

您要将发件人类型从UIPanGestureRecognizer更改为UITapGestureRecognizer。实际上,您不需要在方法体中执行任何sender类型转换。