Search code examples
swiftuitableviewscrollcore-animation

How to make animation during Scrolling UITableView


i have a question that is it possible to make animation on rotating a UILabel during Scrolling UITableView ? then how i can do it ? when user begin to scroll the UILabel will begin rotate based on scroll ! Objective-C and Swift are both ok for me

sorry for my bad english :)

based on Fogmeister solution i wrote in swift and it works Well ;)

#Swift 4

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
      let maximumHorizontalOffset: CGFloat = scrollView.contentSize.width - scrollView.frame.width
      let currentHorizontalOffset: CGFloat = scrollView.contentOffset.x
      let maximumVerticalOffset : CGFloat = scrollView.contentSize.height - scrollView.frame.height;
      let currentVerticalOffset : CGFloat = scrollView.contentOffset.y;
      let percentageVerticalOffset : CGFloat = currentVerticalOffset / maximumVerticalOffset;
      let percentageHorizontalOffset: CGFloat = currentHorizontalOffset / maximumHorizontalOffset;
      scrollMyView(scrollView, didScrollToPercentageOffset: CGPoint(x: percentageHorizontalOffset, y: percentageVerticalOffset))

    }

    func scrollMyView(_ scrollView: UIScrollView?, didScrollToPercentageOffset percentageOffset: CGPoint) {
       UIView.animate(withDuration: 0.5, animations: {
         self.myImageView.transform = CGAffineTransform(rotationAngle: (self.rotate(forOffsetPercentage: percentageOffset.y) * .pi) / 1)
       })
     }


    func rotate(forOffsetPercentage percentage: CGFloat) -> CGFloat {

      let min: CGFloat = 1.0
      let max: CGFloat = 360.0

      let value : CGFloat = (max - min) * percentage + min

    return value
}

You can watch the animation Here !


Solution

  • Try to find out the scrollview Y position, when you scroll. And if condition meets, then do your animation.