iOS之removeFromSuperview遇到的坑:第二次执行animateWithDuration动画,没有动画效果

直接先附上有问题的效果

101.175515.gif

原因:

我懒加载创建的view,而removeFromSuperview不是真正的移除view,所以view还存在,只是被隐藏了。因此第二次并不会再次懒加载创建view。
所以第二次没有动画是因为,第一次最后停留的位置BBB,和第二次的位置是一模一样的。所以第二次移动之前,利用代码AAA更新一下frame就可以了(将view更新到屏幕底部)

详情代码

// 显示联系客服弹出框
-(void)customerServiceTopTitle:(NSString *)titleStr customerServiceCenterPhone:(NSString *)phoneStr customerServiceBottomCancel:(NSString *)cancelStr{
    
    UIWindow *topWindow = [[[UIApplication sharedApplication] delegate] window];
    // 顶部遮罩
    [topWindow addSubview:self.ContactCustomerServiceMaskV];
    
    [topWindow addSubview:self.ContactCustomerServiceContainTopAndBottomV];
     #warning:AAA 更新view的frame到屏幕底部看不见的位置.这样第二次view开始的位置和第一次最后的位置就不一样了
    _ContactCustomerServiceContainTopAndBottomV.frame = CGRectMake(0, self.view.frame.size.height, APP_SCREEN_WIDTH, autoScaleH(96)+ autoScaleH(8) + autoScaleH(48) + autoScaleH(8));
    
    // 底部取消按钮
    [self.ContactCustomerServiceContainTopAndBottomV addSubview:self.ContactCustomerServiceBottomCancelBtn];
    // 顶部view
    [self.ContactCustomerServiceContainTopAndBottomV addSubview:self.ContactCustomerServiceTopV];
    
    // 顶部文字按钮
    [self.ContactCustomerServiceTopV addSubview:self.ContactCustomerServiceTopWordBtn];
    // 顶部电话按钮
    [self.ContactCustomerServiceTopV addSubview:self.ContactCustomerServiceTopPhoneBtn];
    // 顶部横线view
    [self.ContactCustomerServiceTopV addSubview:self.ContactCustomerServiceTopHLineV];
    
    [self.ContactCustomerServiceTopWordBtn setTitle:titleStr forState:UIControlStateNormal];
    [self.ContactCustomerServiceTopPhoneBtn setTitle:phoneStr forState:UIControlStateNormal];
    [self.ContactCustomerServiceTopPhoneBtn setTitleColor:[UIColor colorWithHexString:@"24b3a3"] forState:UIControlStateNormal];
    
    [self.ContactCustomerServiceBottomCancelBtn setTitle:cancelStr forState:UIControlStateNormal];
    [self.ContactCustomerServiceBottomCancelBtn setTitleColor:[UIColor colorWithHexString:@"24b3a3"] forState:UIControlStateNormal];
    
    [UIView animateWithDuration:0.3 animations:^{
      #warning: BBB 0.3秒后,将view移动到屏幕上。
       self.ContactCustomerServiceContainTopAndBottomV.frame = CGRectMake(0, self.view.frame.size.height  - autoScaleH(96) - autoScaleH(8) - autoScaleH(48) - autoScaleH(8) , APP_SCREEN_WIDTH, CGRectGetHeight(self.ContactCustomerServiceContainTopAndBottomV.frame));
     
    } completion:nil];

    
}

// 隐藏联系客服弹出框
-(void)removeCustomerService{
    
    [self.ContactCustomerServiceMaskV removeFromSuperview];
    [self.ContactCustomerServiceContainTopAndBottomV removeFromSuperview];
    
}
// 点击电话背景遮罩隐藏电话view
-(void)ContactCustomerServiceMaskVTap:(UITapGestureRecognizer *)gesture{
    NSLog(@"%@",gesture);
    [self.ContactCustomerServiceMaskV removeFromSuperview];
    [self.ContactCustomerServiceContainTopAndBottomV removeFromSuperview];
    
}

最终效果

101.175514.gif

你可能感兴趣的:(iOS之removeFromSuperview遇到的坑:第二次执行animateWithDuration动画,没有动画效果)