UIDynamics and Autolayout

I recently used UIDynamics to animate an image. However, due to the fact that the auto-tuning limit of y-pos was disconnected from the screen, when navigating the screen and then returning to it, my image was again placed on the screen. The animation took about 3 seconds, so after three seconds I just limited reset. This is a bit hacked.

So my question is this: what is the right way to simultaneously manage startup and UIDynamics?

+4
source share
2 answers

This is not a dynamic problem. Autolayout is not compatible with any kind of animation or with any manual frame settings: when the layout arrives, these are the restrictions that will be met. It is up to you if you somehow move the view manually to update the constraints to fit its new position / size / independently.

Having said that: with UIKit Dynamics, when the animation ends, the animator pauses and the animator delegate is notified:

https://developer.apple.com/library/ios/documentation/uikit/reference/UIDynamicAnimatorDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIDynamicAnimatorDelegate/dynamicAnimatorDidPause :

So this is the moment to update the restrictions.

+5
source

, Geppy Parziale .

, UIDynamicItem:

@interface DynamicHub : NSObject <UIDynamicItem>
    @property(nonatomic, readonly) CGRect bounds;
    @property(nonatomic, readwrite) CGPoint center;
    @property(nonatomic, readwrite) CGAffineTransform transform;
@end

, :

- (id)init {
    self = [super init];
    if (self) {
        _bounds = CGRectMake(0, 0, 100, 100);
    }
    return self;
}

UIDynamics :

DynamicHub *dynamicHub = [[DynamicHub alloc] init];

UISnapBehavior *snapBehavior = [[UISnapBehavior alloc] initWithItem:dynamicHub
                                           snapToPoint:CGPointMake(50.0, 150.0)];
[snapBehavior setDamping:.1];

snapBehavior.action = ^{
     self.firstConstraint.constant = [dynamicHub center].y;
     self.secondConstraint.constant = [dynamicHub center].x;
};

[self.animator addBehavior:snapBehavior];
+3

Source: https://habr.com/ru/post/1532704/


All Articles