//便利中间部分时间的子视图 计算并排不位置
for (int i=0;i UIButton *btn=self.containerView.subviews[i]; int colIndex=i%colums; //列的索引 int rowIndex=i/colums; //行的索引 CGFloat viewX=marginLeft+colIndex*(marginX+width);//视图x的计算 CGFloat viewY=marginTop+rowIndex*(marginY+height);//视图y的计算 [self.btnYArray addObject:@(viewY)];//保存视图真实的y值 //计算视图没执行动画之前的y值 CGFloat hiddenY=viewY+CGRectGetHeight(self.containerView.frame)+self.frame.size.height-CGRectGetMaxY(self.containerView.frame); [self.hiddenBtnYArray addObject:@(hiddenY)];//保存没执行动画之前的y值 btn.frame=CGRectMake(viewX,hiddenY,width,height); //赋值没执行动画之前视图的位置 } //执行动画 [self animationsToBtn]; } //执行显示时动画的方法 -(void)animationsToBtn{ dt=[NSUserDefaults standardUserDefaults]; [dt setObject:@(0) forKey:@"showORno"]; [dt synchronize]; UIButton *btn=self.containerView.subviews[0];//取到视图 按钮 CGFloat showY=[self.btnYArray[0] floatValue]; //取到视图真实的y坐标 dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (NSInteger)(0.02 * NSEC_PER_SEC)); dispatch_after(time, dispatch_get_main_queue(), ^{ //执行动画 并且显示到最终的位置 [UIView animateWithDuration:1.0 animations:^{ btn.frame=CGRectMake(btn.frame.origin.x, showY, btn.frame.size.width, btn.frame.size.height); }]; UIButton *btn1=self.containerView.subviews[1]; CGFloat showY1=[self.btnYArray[1] floatValue]; dispatch_time_t time1 = dispatch_time(DISPATCH_TIME_NOW, (NSInteger)(0.02 * NSEC_PER_SEC)); dispatch_after(time1, dispatch_get_main_queue(), ^{ [UIView animateWithDuration:1.0 animations:^{ btn1.frame=CGRectMake(btn1.frame.origin.x, showY1, btn1.frame.size.width, btn1.frame.size.height); }]; UIButton *btn2=self.containerView.subviews[2]; CGFloat showY2=[self.btnYArray[2] floatValue]; dispatch_time_t time2 = dispatch_time(DISPATCH_TIME_NOW, (NSInteger)(0.02 * NSEC_PER_SEC)); dispatch_after(time2, dispatch_get_main_queue(), ^{ [UIView animateWithDuration:1.0 animations:^{ btn2.frame=CGRectMake(btn2.frame.origin.x, showY2, btn2.frame.size.width, btn2.frame.size.height); }]; UIButton *btn3=self.containerView.subviews[3]; CGFloat showY3=[self.btnYArray[3] floatValue]; dispatch_time_t time3 = dispatch_time(DISPATCH_TIME_NOW, (NSInteger)(0.02 * NSEC_PER_SEC)); dispatch_after(time3, dispatch_get_main_queue(), ^{ [UIView animateWithDuration:1.0 animations:^{ btn3.frame=CGRectMake(btn3.frame.origin.x, showY3, btn3.frame.size.width, btn3.frame.size.height); }]; UIButton *btn4=self.containerView.subviews[4]; CGFloat showY4=[self.btnYArray[4] floatValue]; dispatch_time_t time4 = dispatch_time(DISPATCH_TIME_NOW, (NSInteger)(0.02 * NSEC_PER_SEC)); dispatch_after(time4, dispatch_get_main_queue(), ^{ [UIView animateWithDuration:1.0 animations:^{ btn4.frame=CGRectMake(btn4.frame.origin.x, showY4, btn4.frame.size.width, btn4.frame.size.height); }]; // UIButton *btn5=self.containerView.subviews[5]; // // CGFloat showY5=[self.btnYArray[5] floatValue]; // // dispatch_time_t time5 = dispatch_time(DISPATCH_TIME_NOW, (NSInteger)(0.02 * NSEC_PER_SEC)); // dispatch_after(time5, dispatch_get_main_queue(), ^{ // // [UIView animateWithDuration:1.0 animations:^{ // // btn5.frame=CGRectMake(btn5.frame.origin.x, showY5, btn5.frame.size.width, btn5.frame.size.height); // // }]; // // }); }); }); }); }); }); } //二、调用(在哪个控制器里面需要调用,就写在那个) EG:点击按钮的时候 //得到appdelegate的单列对象 AppDelegate *app=(AppDelegate *)[UIApplication sharedApplication].delegate; //实例化封面对象 并且添加到窗体中去 CoverView *cover=[CoverView coverView]; [app.window addSubview:cover]; //去掉系统的布局 手动布局 cover.translatesAutoresizingMaskIntoConstraints=NO; NSLayoutConstraint *top=[NSLayoutConstraint constraintWithItem:cover attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:app.window attribute:NSLayoutAttributeTop multiplier:1 constant:0]; NSLayoutConstraint *left=[NSLayoutConstraint constraintWithItem:cover attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:app.window attribute:NSLayoutAttributeLeft multiplier:1 constant:0]; NSLayoutConstraint *right=[NSLayoutConstraint constraintWithItem:cover attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:app.window attribute:NSLayoutAttributeRight multiplier:1 constant:0]; NSLayoutConstraint *bottom=[NSLayoutConstraint constraintWithItem:cover attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:app.window attribute:NSLayoutAttributeBottom multiplier:1 constant:0]; top.active=YES; left.active=YES; right.active=YES; bottom.active=YES; 三、以上的举例是添加到系统页面上的、如果是当前控制器、就把 AppDelegate *app=(AppDelegate *)[UIApplication sharedApplication].delegate; 注释、把所有的app.window修改成self.view