首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义分段中的UIKit动力学

自定义分段中的UIKit动力学
EN

Stack Overflow用户
提问于 2014-04-13 01:54:10
回答 1查看 534关注 0票数 0

我试图将视图fall作为自定义segue转换,但是当在UIStoryboardSegue实现中调用执行方法时,它不会掉下来。我尝试过将视图移到源的视图中,以查看它是否做了任何事情,但它没有。

代码语言:javascript
复制
-(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];
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-13 04:39:47

它不下降的原因是重力行为需要时间,但是一旦执行方法完成,segue本身就会被释放。所以,你需要一种方法来保持海格的生存,至少在运动完成之前。一种方法是在源视图控制器中为segue创建一个强属性,并在prepareForSegue中设置它的值,

代码语言:javascript
复制
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    self.dropSegue = segue;
}

我修改了segue的版本,该版本还添加了冲突行为,并将源视图控制器设置为冲突行为的委托,因此我可以使用委托方法collisionBehavior:endedContactForItem:withBoundaryIdentifier:,将dropSegue属性设置为0(稍微延迟之后),这将导致释放segue,

代码语言:javascript
复制
-(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];
}

这是我对重力下降的解释,

代码语言:javascript
复制
@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");
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23038250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档