07---关于动态创建和销毁带动画的UIVew

在我们做开发的过程中经常会遇到在你触发了某个动作之后,需要动态添加一个带动画的UIView,执行完以后就销毁这个UIView

#pragma mark 展示最新微博的数目
- (void)showNewStatusCount:(int)count
{
    // 1.创建按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.enabled = NO; // 设置按钮禁用
    btn.adjustsImageWhenDisabled = NO; // 当按钮禁用的时候,图像会被画得深一点,设置NO可以取消设置
    
    [btn setBackgroundImage:[UIImage resizableImage:@"timeline_new_status_background.png"] forState:UIControlStateNormal];
    btn.alpha = 0.9; // 透明度0.9
    CGFloat w = self.view.frame.size.width; 
    CGFloat h = 35;
    btn.frame = CGRectMake(0, 8, w, h);
    NSString *title = count?[NSString stringWithFormat:@"共有%d条新的微博", count]:@"没有新的微博";
    [btn setTitle:title forState:UIControlStateNormal];
    [self.navigationController.view insertSubview:btn belowSubview:self.navigationController.navigationBar]; // 插入按钮在navigationBar的下面 就是说navigaionBar在外侧,按钮在内测// 2.开始执行动画
    CGFloat duration = 0.5;
    
    [UIView animateWithDuration:duration 
   animations:
^{ // 下来 btn.transform = CGAffineTransformMakeTranslation(0, h); // Y坐标在原有的位置再向下移动h个像素点 }
   completion:
^(BOOL finished) { // 下来完成以后执行 [UIView animateWithDuration:duration delay:1.0 options:UIViewAnimationOptionCurveLinear animations:^{// 上去 btn.transform = CGAffineTransformIdentity; }
  completion:
^(BOOL finished) { // 上去的动画完成以后执行 [btn removeFromSuperview]; // 从父视图中移除 }]; }]; }

以后遇到需要动态添加一个带动画的UIView,执行完以后就销毁这个UIView,我就这么干!!!

原文地址:https://www.cnblogs.com/lszwhb/p/3881545.html