1

好吧,我迷路了。

所以有了这个UIViewController,我们可以称之为我为每次单击添加按钮viewController1添加一个新的可编辑项。UITextView

然后我使用UIPanGestureRecognizer这样你就可以将它们拖到任何你想要的地方。这工作得很好。现在我想去viewController2[self.navigationController pushViewController:viewController2 animated:YES];但是当我按下后退按钮返回到viewController1第一个UITextView创建时,viewController1它发生了变化。它没有消失,但 textview 中的 text 属性已经移动。

这仅发生在我创建的 UITextView 的第一个实例中,这里的任何想法都将不胜感激。这是我创建文本的方法:

-(void)addTextFieldToScreen
{
    UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 80, 140, 120)];
    textView.text = @"TEXT";
    textView.backgroundColor = [UIColor clearColor];
    textView.textColor = [UIColor purpleColor];
    textView.font = [UIFont fontWithName:@"AmericanTypewriter" size:30];
    textView.userInteractionEnabled = YES;

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveText:)];
    panRecognizer.minimumNumberOfTouches = 1;
    panRecognizer.maximumNumberOfTouches = 1;
    panRecognizer.delegate = self;
    [textView addGestureRecognizer:panRecognizer];
    [self.view addSubview:textView];
}

这是使我的 textView 可拖动的方法:

-(void)moveText:(id)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];

    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {

        CGFloat finalX = translatedPoint.x + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].x);
        CGFloat finalY = translatedPoint.y + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y);
        CGRect screenRect = [[UIScreen mainScreen] bounds];
        CGFloat workingWidth = screenRect.size.width - 40;
        CGFloat workingHeight = screenRect.size.height - 80;

        if(finalX < 50) {

            finalX = 50;
        }

        else if(finalX > workingWidth ) {

            finalX = workingWidth;
        }

        if(finalY < 80) {

            finalY = 80;
        }

        else if(finalY > workingHeight) {

            finalY = workingHeight;
        }


        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        [UIView setAnimationDuration:.20];
        [UIView commitAnimations];
    }
}

编辑:

好的,这是怎么回事,我用 viewController1 右上角的“添加文本”按钮添加了两个文本视图,内容为“TEXT”:

在此处输入图像描述

然后当我从 viewController2 回到 viewController1 时,这就是我所看到的:

在此处输入图像描述

如您所见,只有左侧的 textView 实例文本属性会改变位置,这是我要添加的第一个。

4

0 回答 0