如何在滚动UITableView期间制作动画

时间:2018-07-16 06:30:06

标签: swift uitableview scroll core-animation

我有一个问题,在滚动UITableView期间是否可以在旋转UILabel上制作动画?那我该怎么办呢?当用户开始滚动时,UILabel将开始基于滚动旋转! Objective-C和Swift都适合我

对不起,我的英语不好:)

  

基于我迅速编写的Fogmeister解决方案,效果很好;)

#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 !

1 个答案:

答案 0 :(得分:1)

尝试滚动时找出滚动视图Y的位置。如果条件满足,就制作动画。