UITextView奇怪的行为

时间:2013-08-21 11:12:49

标签: iphone ios objective-c uitextview ios7

好的,我在这里迷路了。

我们可以将此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”的文本视图和“添加文本”按钮:

enter image description here

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

enter image description here

正如您所看到的那样,只有左侧textView实例文本属性才能更改位置,这是我要添加的第一个。

0 个答案:

没有答案