动画两个uiview在一起

时间:2015-11-25 05:25:50

标签: ios objective-c animation

假设主视图的高度为h。

我有两个uivivew高度h / 2(让我们称他们为上下视图)。

我想添加向上和向下滑动手势,以便

- 如果我向上滑动

  1. 如果向下视图和向上视图高度是h / 2并向上滑动,则必须使向下视图高度为h,并且必须使向上视图高度为0。

  2. 如果向上视图高度为h且向下视图高度为0,则必须使向下视图和向上视图高度为h / 2

  3. 我想要制作动画,以便高度增加和减少必须平滑可见。

2 个答案:

答案 0 :(得分:1)

  1. 在主视图中设置向上和向下的垂直约束,使其垂直间距为0,Up to Main的顶部为0,Down to Main的底部也为0.
  2. 为向下视图设置高度约束,常量0,乘数1.将此约束挂钩到IBOutlet。
  3. 滑动时,检查约束常量是否为0,然后设置为h / 2,如果h / 2则设置为h。之后,告诉视图layoutIfNeeded使用新的约束参数进行更新。如果需要,您可以为布局添加动画。

答案 1 :(得分:1)

这是我的回答

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   [self initialization];
 }

-(void)viewDidAppear:(BOOL)animated{
   [super viewDidAppear:animated];
   [self viewDesign];
 }

-(void)initialization{
   viewUpHeight.constant=self.view.frame.size.height/2;
   viewDownHeight.constant=self.view.frame.size.height/2;
 }

-(void)viewDesign{
     UISwipeGestureRecognizer *swipeUpGestureRecognizer =[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeUp:)];
     swipeUpGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
     [self.view  addGestureRecognizer:swipeUpGestureRecognizer];

      UISwipeGestureRecognizer *swipeDownGestureRecognizer =[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDown:)];
      swipeDownGestureRecognizer.direction = UISwipeGestureRecognizerDirectionDown;
      [self.view  addGestureRecognizer:swipeDownGestureRecognizer];
}

-(void)swipeUp:(UISwipeGestureRecognizer *)swipeGes{
   if(viewUp.frame.size.height==self.view.frame.size.height) {

    }else {
    CGRect basketTopFrame = self.viewUp.frame;
    basketTopFrame.origin.y = -basketTopFrame.size.height;
    // basketTopFrame.size.height=0;

    CGRect basketBottomFrame = self.viewDown.frame;
    basketBottomFrame.size.height=self.view.frame.size.height;
    basketBottomFrame.origin.y =0;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationDelay:0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

    self.viewUp.frame = basketTopFrame;
    self.viewDown.frame = basketBottomFrame;

    [UIView commitAnimations];

    }
}

-(void)swipeDown:(UISwipeGestureRecognizer *)swipeGes{

   if(viewDown.frame.size.height==self.view.frame.size.height){
   CGRect basketTopFrame = self.viewUp.frame;

   basketTopFrame.origin.y =0;

   CGRect basketBottomFrame = self.viewDown.frame;
   basketBottomFrame.origin.y =self.view.frame.size.height/2;
   basketBottomFrame.size.height=self.view.frame.size.height/2;

   [UIView beginAnimations:nil context:nil];
   [UIView setAnimationDuration:1];
   [UIView setAnimationDelay:0];
   [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

   self.viewUp.frame = basketTopFrame;
   self.viewDown.frame = basketBottomFrame;

   [UIView commitAnimations];
  }
}