如何在滚动 UITableView 期间制作动画
How to make animation during Scrolling UITableView
我有一个问题,是否可以在滚动 UITableView 期间在旋转 UILabel 时制作动画?那我该怎么做呢?当用户开始滚动时,UILabel 将根据滚动开始旋转! Objective-C 和 Swift 都适合我
抱歉我的英语不好:)
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
}
滚动时尝试找出滚动视图的 Y 位置。如果条件满足,那就做你的动画。
我有一个问题,是否可以在滚动 UITableView 期间在旋转 UILabel 时制作动画?那我该怎么做呢?当用户开始滚动时,UILabel 将根据滚动开始旋转! Objective-C 和 Swift 都适合我
抱歉我的英语不好:)
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
}
滚动时尝试找出滚动视图的 Y 位置。如果条件满足,那就做你的动画。