2016-01-02 83 views
1

我试图在集合视图滚动时为桌面高度设置动画。我从scrollViewWillBeginDragging调用动画函数。然后我执行一个动画块:UITableView的动画高度

func scrollViewWillBeginDragging(scrollView: UIScrollView) { 
    if expanded == false && scrollView == IBcalendarCollectionView { 
     expanded = true 
     expandCollectionView() 
    } 
} 

    func expandCollectionView() { 

     let screenSize: CGRect = UIScreen.mainScreen().bounds 

    UIView.animateWithDuration(3, delay: 0, options: UIViewAnimationOptions.TransitionNone, animations: {() -> Void in 

     self.IBcalendarTableView.frame = CGRectMake(self.IBcalendarTableView.frame.origin.x, screenSize.height/2, self.IBcalendarTableView.frame.width, screenSize.height/2) 

     }) { (success) -> Void in 
      print(self.IBcalendarTableView.frame.origin.y) 
    } 
} 

发生什么事是TableView动画到状态栏,然后返回到它的原始位置。我想让它动画缩小到屏幕尺寸的一半。动画块只执行一次,因为我在调用动画函数时设置了布尔值。

CollectionView and TableView

回答

0

问题出在自动布局。您还必须设置tableview的高度约束,以便将tableview保持在新的高度/原点。

对于其他人谁可能有这个问题:

Dynamic UITableView height

这个答案帮我解决这个问题。我的固定代码如下所示:

func expandCollectionView() { 
    let screenSize: CGRect = UIScreen.mainScreen().bounds 

    var frame = self.IBcalendarTableView.frame 
    frame.origin.y = screenSize.height/2 
    frame.size.height = frame.origin.y 


    UIView.animateWithDuration(0.5) {() -> Void in 
     self.IBcalendarTableView.frame = frame 
     self.IBtableViewHeightConstraint.constant = screenSize.height/2 
    } 

} 
+0

很高兴你能得到答案。 – dsieczko

0

你说你设置一个布尔值当动画块被调用,是这样的动画没有被一次又一次的叫什么?我也没有看到你的代码。

这应该很好。

func scrollViewWillBeginDragging(scrollView: UIScrollView) { 
    let mainScreen = UIScreen.mainScreen().bounds 

    if (self.IBcalendarTableView.frame.origin.y != mainScreen.height/2) { 
     var frame       = self.IBcalendarTableView.frame 
      frame.origin.y     = mainScreen.height/2 
      frame.size.height  = frame.origin.y 

     UIView.animateWithDuration(0.5) {() -> Void in 
      self.IBcalendarTableView.frame = frame 
     } 
    } 
} 

我设置一个较低的动画速度,3秒后似乎想了很多......我还内置了框架不同,我只是做,因为我觉得它更容易阅读。

请让我知道,如果有什么我可以做的帮助。

+0

谢谢。我编辑了代码以显示布尔值。另外,我在3秒钟的时间里看到它,这样我就可以看到动画中发生了什么。使用你的代码动画的作品,但它仍然有奇怪的行为。动画完成后,我滚动tableview,tableview返回到原来的高度。你认为这可能是一个自动布局问题? – brl214

+0

我测试它没有任何限制,仍然有同样的问题。如果collectionview继续滚动,或者我滚动tableview,tableview将返回到其原始大小/来源 – brl214