1

I'm making an animated interface in iOS with storyboard. I have a subclass of UIView called AnimatedView with all of the interface elements as its subviews.

I need to be able to change the frame, transform, bounds, alpha etc. of all these subviews. This is turning out to be a problem. When my AnimatedView's layoutSubviews method is called by its parent, it calls some UIView private method called _is_layout, which changes all of my subviews' bound and frame properties back to what they used to be.

If I add the UIViews programmatically this doesn't happen and everything works fine, but I'm working with graphic designers and I don't want them to have to mess with code.

After some trial and error, I've found that removing all the Auto Layout constraints on AnimatedView keeps it from resetting the frames, but it still resets the bounds. Here's what AnimatedView's overridden layoutSubviews method looks like:

-(void)layoutSubviews{
    [self createAnimationsFromPlist: filePath];
    [self removeConstraints: self.constraints];
    [super layoutSubviews];
    //set everything to its initial frame
    [self.animator animate:0];
}

Am I on the right track by removing the constraints? I've read the auto layout guide and I can't figure out why this would help me. I'm not using constraints in the storyboard with AnimatedView, but self.constraints has 20 elements for some reason.

If it helps, here's an example of how I'm animating the views:

    - (void)animateFrame:(NSInteger)time
{
    float angle = angleFrames[time];
    self.view.transform = CGAffineTransformMakeRotation(angle);
}

1 Answer 1

2

You cannot set the frame, center, or bounds properties directly when using Auto Layout, and transforms may not work as expected.

So yes, for this kind of animation, it is best to take the view out of control of Auto Layout.

Sign up to request clarification or add additional context in comments.

3 Comments

Great, thanks! Is there a more complete or better way to take this view out of control of Auto Layout? Or does "removeConstraints" do it? I don't want to disable auto layout for the entire application since I use it for some of the other view elements, but could that be my best bet?
If it's loaded from a nib, you could disable Auto Layout on that nib only, OR you could position it with placeholder constraints, then set translatesAutoresizingMasksIntoConstraints to true and set its autoresizingMask appropriately.
I got it to work! I set the AnimatedView and all it's subviews' translatesAutoresizingMasksIntoConstraints to YES. This was a major headache until you helped me out, thanks for your help Austin!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.