【问题标题】:Animate two uiview together动画两个uiview一起
【发布时间】:2015-11-25 17:38:03
【问题描述】:

假设主视图的高度为h。

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

我想添加上下滑动手势,这样

-如果我向上滑动

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

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

我想为事物设置动画,以便高度的增加和减少必须平滑可见。

【问题讨论】:

  • 你想让动画过渡平滑吗?或者更确切地说是连续或跟踪你的动作?或者只是一个小小的俯卧撑就会开始动画?
  • 你尝试了什么?也请发布您的代码。
  • 为这个动画创建两个单独的时间间隔

标签: ios objective-c animation


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

【讨论】:

    【解决方案2】:

    这是我的答案

    - (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];
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 2011-02-01
      • 1970-01-01
      相关资源
      最近更新 更多