iOS UIDynamicAnimator实例对象为什么不作为property出现的时候不好使??



 - (void)shake
{
    if (!self.animator) {
        self.animator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
    }

    [self.animator removeAllBehaviors];

    CGPoint newPos = CGPointMake(arc4random()%320, arc4random()%568);
    UISnapBehavior *snap = [[UISnapBehavior alloc]initWithItem:_btn snapToPoint: newPos];
    snap.damping = 0.3;
    [self.animator addBehavior:snap];

//    UIDynamicAnimator *animator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
//    
//    [animator removeAllBehaviors];
//    
//    CGPoint newPos = CGPointMake(arc4random()%320, arc4random()%568);
//    UISnapBehavior *snap = [[UISnapBehavior alloc]initWithItem:_btn snapToPoint: newPos];
//    snap.damping = 0.3;
//    [animator addBehavior:snap];
}

注释部分的代码是不作为property出现的,上下两段代码除了一个是property一个不是其它的都一毛一样,为什么只有当animator作为property的时候这个add进去的snap动画才生效呢??看了官方文档也没找到这方面的解释,而且之前忘记是用什么其它的东西也是遇到了这种情况。跪求大神给解释一下ORZ

动画 Animation ios

爱国者之枪 8 years, 10 months ago

嗯…… 我觉得恐怕是这个 animator 不会 retain 自身,所以如果没有人持有它,在这个函数末尾, animator 这个对象就自动释放了。如果作为 property,会持有这个对象,就不会释放了。

可以试试把 property 改成 weak 的,估计还会是不生效。

肝不存在的 answered 8 years, 10 months ago

Your Answer