我正在尝试使 View 作为自定义 segue 转换下降,但是当在 UIStoryboardSegue 实现中调用 perform 方法时,它不会下降。我曾尝试将 View 移动到源 View 中以查看它是否执行任何操作,但它没有执行任何操作。
-(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];
}
最佳答案
它没有下降的原因是重力行为需要时间,但是 segue 本身会在 perform 方法完成后立即释放。因此,您需要一种方法来至少在移动完成之前让 segue 保持事件状态。一种方法是在源 View Controller 中为 segue 创建一个强大的属性,并在 prepareForSegue 中设置它的值,
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
self.dropSegue = segue;
}
我对你的 segue 做了一个修改版本,它也添加了一个碰撞行为,并将源 View Controller 设置为碰撞行为的委托(delegate),所以我可以使用委托(delegate)方法 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];
}
这是我的重力下降转场版本,
@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");
}
关于ios - 自定义 Segue 中的 UIKit 动态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23038250/