自定义Segue中的UIKit动态

时间:2014-04-13 01:54:10

标签: ios objective-c uikit transitions

我正在尝试将视图作为自定义segue转换,但是当在UIStoryboardSegue实现中调用perform方法时,它不会下降。我已经尝试将视图移动到源视图中以查看它是否执行任何操作,但事实并非如此。

-(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 个答案:

答案 0 :(得分:2)

它不丢弃的原因是因为重力行为需要时间,但是一旦执行方法完成就会释放segue本身。所以,你需要一种方法来保持segue活着至少直到运动完成。一种方法是在源视图控制器中为segue创建一个强大的属性,并在prepareForSegue中设置它的值,

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

我做了一个修改后的segue版本,它还添加了一个碰撞行为,并将源视图控制器设置为碰撞行为的委托,因此我可以使用委托方法collisionBehavior:endedContactForItem:withBoundaryIdentifier:来设置dropSegue属性为nil(稍微延迟后)导致segue被释放,

-(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];
}

这是我的重力下降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");
}