Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
715 views
in Technique[技术] by (71.8m points)

objective c - With autolayout, label position is reset when I change label text

I am developing a very simple application in Objective-c.
In the app, the user can change a position of the label by dragging the screen.

Tap & drag the screen, the label moves up and down to match the position of the finger.
When you release your finger, the coordinates information of the label is set to its text.

But the label position is reset at the moment its text is changed.

// IBOutlet UILabel *label

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint touch = [[touches anyObject] locationInView:self.view];
    label.center = CGPointMake(label.center.x, touch.y);
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint touch = [[touches anyObject] locationInView:self.view];
    label.center = CGPointMake(label.center.x, touch.y);
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint touch = [[touches anyObject] locationInView:self.view];
    label.text = [NSString stringWithFormat:@"y = %f", touch.y];
}

In touchesEnded event, just only change the text of the label,
but the position of it has reset.

I tried to change touchesEnded event like following but it haven't solved the problem.

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint touch = [[touches anyObject] locationInView:self.view];
    label.text = [NSString stringWithFormat:@"y = %f", touch.y];
    label.center = CGPointMake(label.center.x, touch.y);    // add this line
}

I want to resolve this weird behavior without uncheck "Use auto layout".
I'd like to keep using auto layout.

I have 4 screenshots for my app.

4 Screenshots

  1. The first image is a Storyboard.
    I have a label with auto layout constraints.
  2. The second image is a screenshot right after launching app.
  3. The third image is when a user drag the screen,
    and the label move down to match the finger.
  4. The forth image is right after release your finger
    and the label text has changed.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You cannot use auto layout and change the center / frame of things; those are opposites. Do one or the other - not both.

So, you don't have to turn off auto layout, but if you don't, then you must use auto layout, and auto layout only, to position things.

When you move the label, do not change its center - change its constraints. Or at least, having changed its center, change its constraints to match.

Otherwise, when layout occurs, the constraints will put it back where you have told them to put it. And layout does occur when you change the text, and at many other times.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...