UIKit Dynamics in a Custom Segue

こ雲淡風輕ζ 提交于 2020-01-16 19:14:27

问题


I'm trying to make a view fall as a custom segue transition, but when the perform method is called in the UIStoryboardSegue implementation, it does not fall. I have tried moving the view to be dropped into the source's view to see if it does anything, but it doesn't.

-(void)perform {
    UIViewController *src  = (UIViewController *)self.sourceViewController;
    UIViewController *dest = (UIViewController *)self.destinationViewController;

    UIView *viewdrop = [dest.view snapshotViewAfterScreenUpdates:YES];
    viewdrop.frame = CGRectMake(0, -src.view.frame.size.height, dest.view.frame.size.width, dest.view.frame.size.height);
    [src.view addSubview:viewdrop];

    animator = [[UIDynamicAnimator alloc] initWithReferenceView:src.view];

    UIGravityBehavior* gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[viewdrop]];
    [animator addBehavior:gravityBehavior];
}

回答1:


The reason it doesn't drop is because the gravity behavior takes time, but the segue itself is deallocated as soon as the perform method finishes. So, you need a way to keep the segue alive at least until the movement is complete. One way to do this is to make a strong property for the segue in the source view controller, and set its value in prepareForSegue,

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    self.dropSegue = segue;
}

I made a modified version of your segue that also adds a collision behavior, and sets the source view controller as the delegate of the collision behavior, so I can use the delegate method, collisionBehavior:endedContactForItem:withBoundaryIdentifier:, to set the dropSegue property to nil (after a slight delay) which causes the segue to be deallocated,

-(void)collisionBehavior:(UICollisionBehavior *)behavior endedContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying>)identifier {
    NSLog(@"collision ended with %@", identifier);
    [self performSelector:@selector(setDropSegue:) withObject:nil afterDelay:1];
}

Here is my version of the gravity drop segue,

@interface RDSegue ()
@property (strong, nonatomic) UIDynamicAnimator *animator;
@end

@implementation RDSegue

-(id)initWithIdentifier:(NSString *)identifier source:(UIViewController *)source destination:(UIViewController *)destination {

    if (self = [super initWithIdentifier:identifier source:source destination:destination]) {
        UIViewController *src  = self.sourceViewController;
        UIViewController *dest = self.destinationViewController;
        self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:src.view];
        [src addChildViewController:dest];
        [dest didMoveToParentViewController:src];
        dest.view.frame = CGRectMake(0, -src.view.bounds.size.height, src.view.bounds.size.width, src.view.bounds.size.height);
        [src.view addSubview:dest.view];
    }
    return self;
}

-(void)perform {
    UIGravityBehavior* gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[[self.destinationViewController view]]];
    UICollisionBehavior *collide = [[UICollisionBehavior alloc] initWithItems:@[[self.destinationViewController view]]];
    CGPoint left = CGPointMake(self.animator.referenceView.bounds.origin.x, self.animator.referenceView.bounds.origin.y + self.animator.referenceView.bounds.size.height);
    CGPoint right = CGPointMake(self.animator.referenceView.bounds.origin.x + self.animator.referenceView.bounds.size.width, self.animator.referenceView.bounds.origin.y + self.animator.referenceView.bounds.size.height);
    [collide addBoundaryWithIdentifier:@"bottom" fromPoint:left toPoint:right];
    [collide setCollisionDelegate:self.sourceViewController];
    [self.animator addBehavior:gravityBehavior];
    [self.animator addBehavior:collide];
}

-(void)dealloc {
    NSLog(@"In dealloc");
}


来源:https://stackoverflow.com/questions/23038250/uikit-dynamics-in-a-custom-segue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!