怪异的UIDynamicAnimator / UIGravityBehavior动画

时间:2014-05-20 15:31:16

标签: objective-c ios7 uidynamicbehavior uidynamicanimator

视图具有重力效果时的奇怪动画。

CGFloat t = self.contentView.frame.origin.x;
CGFloat centerEdge = self.view.frame.size.width/2.0f;
CGPoint velocity = [gesture velocityInView:self.view];

CGPoint point = CGPointMake(movingView.frame.origin.x + translation.x + velocity.x, movingView.frame.origin.y);

collisionBehaviour = [[UICollisionBehavior alloc] initWithItems:@[self.contentView]];
collisionBehaviour.translatesReferenceBoundsIntoBoundary = NO;
collisionBehaviour.collisionDelegate = self;

UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.size.width+self.menuSize, self.view.bounds.size.height)];
[collisionBehaviour addBoundaryWithIdentifier:@"rect" forPath:path];

pushBehavior = [[UIPushBehavior alloc] initWithItems:@[self.contentView] mode:UIPushBehaviorModeContinuous];
pushBehavior.pushDirection = CGVectorMake(velocity.x, 0.0f);
pushBehavior.active = YES;

elasticityBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[self.contentView]];
elasticityBehavior.elasticity = 0.4f;
elasticityBehavior.allowsRotation = NO;

gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[self.contentView]];
if (t < centerEdge) {
    gravityBehavior.gravityDirection = CGVectorMake(-1.0f, 0.0f);
} else {
    gravityBehavior.gravityDirection = CGVectorMake(1.0f, 0.0f);
}

[animator addBehavior:collisionBehaviour];
[animator addBehavior:pushBehavior];
[animator addBehavior:elasticityBehavior];
[animator addBehavior:gravityBehavior];

example

  1. 菜单已打开。
  2. 很奇怪(据说是UIGravityBehavior)动画。 (0.5-1秒白色条纹留在左侧
  3. 白色条纹消失在:
  4. - (void)dynamicAnimatorDidPause:(UIDynamicAnimator *)a{
        if ([animator.behaviors count] > 0) {
            [self removeAnimation];
    
            CGFloat t = self.contentView.frame.origin.x;
            CGFloat centerEdge = self.view.frame.size.width/2.0f;
    
            if (t < centerEdge) {
                [self slideInAnimated:YES completion:nil]; // Change origin to CGPointZero
            } else {
                [self slideToSideAnimated:YES completion:nil];
            }
        }
    }
    

0 个答案:

没有答案