对此的简单答案是......使用UIScrollView
.
从我这里的问题来看...循环 UIScrollView 但继续减速 https://stackoverflow.com/questions/14632754/loop-uiscrollview-but-continue-decelerating
将其转换为 Swift 很简单,但这里是......
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//make the content size really big so that the targetOffset of the deceleration will never be met.
scrollView.contentSize = CGSize(width: CGRectGetWidth(scrollView.frame) * 100.0, height: CGRectGetHeight(scrollView.frame))
//set the contentOffset of the scroll view to a point in the center of the contentSize.
scrollView.setContentOffset(CGPoint(CGRectGetWidth(scrollView.frame) * 50, 0), animated: false)
}
func rotateImageView() {
//Calculate the percentage of one "frame" that is the current offset.
// each percentage of a "frame" equates to a percentage of 2 PI Rads to rotate
let minOffset = CGRectGetWidth(scrollView.frame) * 50.0
let maxOffset = CGRectGetWidth(scrollView.frame) * 51.0
let offsetDiff = maxOffset - minOffset
let currentOffset = scrollView.contentOffset.x - minOffset
let percentage = currentOffset / offsetDiff
arrowView.transform = CGAffineTransformMakeRotation(M_PI * 2 * percentage)
}
func scrollViewDidScroll(scrollView: UIScrollView) {
//the scrollView moved so update the rotation of the image
rotateImageView()
}
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
//the scrollview stopped moving.
//set the content offset back to be in the middle
//but make sure to keep the percentage (used above) the same
//this ensures the arrow is pointing in the same direction as it ended decelerating
let diffOffset = scrollView.contentOffset.x
while diffOffset >= CGRectGetWidth(scrollView.frame) {
diffOffset = diffOffset - CGRectGetWidth(scrollView.frame)
}
scrollView.setContentOffset(CGPoint(x: CGRectGetWidth(scrollView.frame) * 50 + diffOffset, y: 0), animated:false)
}
在这个例子中,我的旋转视图是arrowView
. The scrollView
and arrowView
都应该是视图控制器视图的子视图。这scrollView
里面不应该有任何东西。
注意:这是在浏览器中完成的,因此可能存在一些语法问题。您可能还需要将一些数字转换为CGFloat
etc...
此外,能够从 Objective-C 进行翻译对于任何 iOS 开发人员来说都是至关重要的。数以百万计的应用程序是使用 Objective-C 编写的。语法可能略有不同,但所有 API 都是相同的。
学习如何做到这一点是每个 iOS 开发人员都应该做的事情。