Animate UIView height from bottom to top

前端 未结 6 554
Happy的楠姐
Happy的楠姐 2021-02-02 17:35

I\'m doing a simple animation of UIView height so that it reveals.

By default it seems to be revealing from top to bottom, and I want it to reveal bottom to top.

<
6条回答
  •  一个人的身影
    2021-02-02 18:14

    As requested, this is the code that I'm using... I'm using a CAKeyFrameAnimation, which may be a bit more than what you're looking for. It would probably work the same with a CABasicAnimation, I'm just showing you this code because I already have it written.

    -(id)initWithFrame:(CGRect)frame {
      self = [super initWithFrame:frame];
      if (self) {   
        springLayer = [[CALayer alloc] init];
        springLayer.backgroundColor = [UIColor redColor].CGColor;
        springLayer.anchorPoint = CGPointMake(0, 1);
        springLayer.frame = CGRectMake(125, 285, 100, 115);
        [springLayer setNeedsDisplay];
        [self.layer addSublayer:springLayer];
        [self test];
      }
      return self;
    }
    
    -(void)test {
        CAKeyframeAnimation *heightAnim = [[CAKeyframeAnimation alloc] init];
        heightAnim.duration = 3;
        heightAnim.removedOnCompletion = NO;
        heightAnim.fillMode = kCAFillModeForwards;
    
        heightAnim.beginTime = CACurrentMediaTime() + 0.25;
    
        NSMutableArray *v = [[NSMutableArray alloc] init];
        NSMutableArray *t = [[NSMutableArray alloc] init];
    
        float dest = 250;
        float difference = 135;
    
    
        while (difference > 1.0) {
            [v addObject:[NSNumber numberWithFloat:dest-difference]];
            [t addObject:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
    
            difference *= 0.7;
    
            [v addObject:[NSNumber numberWithFloat:dest+difference]];
            [t addObject:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
    
            difference *= 0.7;
        }
    
        heightAnim.values = v;
        heightAnim.timingFunctions = t;
    
        [springLayer addAnimation:heightAnim forKey:@"bounds.size.height"];
    }
    

提交回复
热议问题