1

I have to animate a couple of UIViews which I have added in a UIViewController inside the UIStoryboard. I have attached them with proper constraints so that they will always visible in a way I am looking. This is fine.

I am using https://github.com/satoshin21/Anima library to animate those views as per my need.

But the problem is they don't work as expected means, they are not animating in a direction or position it should be. I believe this is because of the constraints I have applied.

What is the best way to achieve this even if the constraints applied?

Setting, myView.translatesAutoresizingMaskIntoConstraints = true is coming up with lots of warning messages in console.

P.S. I am aware of taking references to the constraints in form of NSLayoutConstraints but this is not I am looking at as the above library is simply providing good chaining functions though we can do it without having references to the constraints.

Hemang
  • 26,840
  • 19
  • 119
  • 186
  • 1
    Could you provide some code and give more detailed descriptions than _not animating in a direction or position it should be_? – clemens Aug 03 '17 at 05:19
  • 1
    @macmoonshine, I am writing experimenting codes for trial and error for the animation. That's fine. I can rewrite the code for that but the answer I am looking is what I am doing right now is correct or not, if not then what should be the right way of doing it. I don't need a code. I need an idea. *And yes, I am and I will always be a beginner as if I always eagers to learn new things.* My reputations are what I know already. – Hemang Aug 03 '17 at 05:45

3 Answers3

0

The problem here, is that NSLayoutConstraint toggling works like properties in the sense that they are nothing but values which can be switched on/off, and alternated by playing with this toggling and other references to other possible values they can have. There's no real way of going around this that I know of unfortunately, and in fact i myself have built a small library similar to Anima, and it works rather well if you respect the NSLayoutConstraints' nature.

The proof of this is that under the hood of this Anima library, it's simply storing the animation points declared inside of the chain (inside Enum values in fact), and applying them as the animation moves along. Regardless, you should never re-set translatesAutoResizingMaskIntoConstraints to true when working with NSLayoutConstraints. The second reason for this is that Constraints are the basis for all iOS frame operations, including .frame, and animations (which is why Anima works so well from the looks of it). I wrote a post on this recently, but as I explain by referencing Apple:

Your problem is that when translatesAutoresizingMaskIntoConstraints is called, methods like .frame or .frame.size are ignored/overriden (depending on when you use them, before or after translatesAutoresizingMaskIntoConstraints). As described by Apple:

Note that the autoresizing mask constraints fully specify the view’s size and position; therefore, you cannot add additional constraints to modify this size or position without introducing conflicts. If you want to use Auto Layout to dynamically calculate the size and position of your view, you must set this property to false, and then provide a non ambiguous, nonconflicting set of constraints for the view.

UPDATED Otherwise, try not to set translatesAutoResizingMaskIntoConstraints to true with these views, by doing that you basically tell your controller to ignore your constraints, and to try to apply constraints based on the .frame or .frame.size or position values set on the UIView. Thus, making your custom constraints obsolete. If by stopping this, you still get the issue, it's probably a constraint value issue, of which i can't give you much more advice without any code unfortunately.

jlmurph
  • 1,050
  • 8
  • 17
0

First, you shouldn't set translatesAutoresizingMaskIntoConstraints to true if you have set suitable constraints on a view already. Setting this property to true will add more constraints to the view which leads to conflicts.

The general code for animation with constraints is

aConstraint.constant = 1234
anotherConstraint.isActive = false
thirdConstraint.isActive = true // thirdConstraint replaces anotherConstraint
UIView.animatewithDuration: 0.25) {
    self.view.layoutIfNeeded()
}

Hope this helps. ;)

clemens
  • 16,716
  • 11
  • 50
  • 65
0

My solution is to remove constraints for that UIViewController and set the frame programmatically as per my needs. This works fine and no need to do this patchy thing with the usage of AutoLayout.

Hemang
  • 26,840
  • 19
  • 119
  • 186