1

I am trying to animate the tableviews height when the collection view scrolls. I call the animation function from scrollViewWillBeginDragging. Then I execute an animation block:

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)
    }
}

Whats happening is the TableView animates up to the status bar then returns to its original position. I want it to animate down to half the size of the screen. The animation block is only executed once because I set a boolean when the animation function is called.

CollectionView and TableView

brl214
  • 527
  • 1
  • 6
  • 16

2 Answers2

0

you say you set a boolean when the animation block is called, is that so the animation is not being called again and again? I also don't see that in your code.

This should work fine.

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
       }
    }
}

I set a lower animation speed, 3 seconds seems like a lot... I also built the frame differently, I just do it because I find it easier to read.

Please let me know if there is anything else I can do to help.

brl214
  • 527
  • 1
  • 6
  • 16
dsieczko
  • 403
  • 3
  • 11
  • Thanks. I edited the code to show the boolean. Also, I had it at 3 seconds just so I can see what was happening in the animation. With your code the animation works but it still has strange behavior. After the animation completes and I scroll the tableview, the tableview returns back to its original height. Do you think it could be an autolayout issue? – brl214 Jan 02 '16 at 22:26
  • I tested it without any constraints and still have the same issue. If the collectionview keeps scrolling or I scroll the tableview, the tableview will return to its original size/origin – brl214 Jan 03 '16 at 01:29
0

The issue is with autolayout. You have to also set the height constraint of the tableview in order to keep the tableview at the new height/origin.

For anyone else who may have this issue:

Dynamic UITableView height

This answer helped me solve this problem. My fixed code looks like this:

    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
    }

}
Community
  • 1
  • 1
brl214
  • 527
  • 1
  • 6
  • 16