我试图将视图fall作为自定义segue转换,但是当在UIStoryboardSegue实现中调用执行方法时,它不会掉下来。我尝试过将视图移到源的视图中,以查看它是否做了任何事情,但它没有。
-(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];
}发布于 2014-04-13 04:39:47
它不下降的原因是重力行为需要时间,但是一旦执行方法完成,segue本身就会被释放。所以,你需要一种方法来保持海格的生存,至少在运动完成之前。一种方法是在源视图控制器中为segue创建一个强属性,并在prepareForSegue中设置它的值,
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
self.dropSegue = segue;
}我修改了segue的版本,该版本还添加了冲突行为,并将源视图控制器设置为冲突行为的委托,因此我可以使用委托方法collisionBehavior:endedContactForItem:withBoundaryIdentifier:,将dropSegue属性设置为0(稍微延迟之后),这将导致释放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");
}https://stackoverflow.com/questions/23038250
复制相似问题