Rod*_*kes 11 xcode uiimageview uigesturerecognizer ios swift
我现在使用此代码在按钮水龙头上旋转瓶子:
@IBAction func spinButton(sender: AnyObject) {
let rotateView = CABasicAnimation()
let randonAngle = arc4random_uniform(360) + 720
rotateView.fromValue = 0
rotateView.toValue = Float(randonAngle) * Float(M_PI) / 180.0
rotateView.duration = 3
rotateView.delegate = self
rotateView.repeatCount = 0
rotateView.removedOnCompletion = false
rotateView.fillMode = kCAFillModeForwards
rotateView.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
bottleImageView.layer.addAnimation(rotateView, forKey: "transform.rotation.z")
}
Run Code Online (Sandbox Code Playgroud)
但是如何使用手势旋转按钮?因此,我移动手指越硬/越快,瓶子旋转的速度就越快
对此的简单答案是......使用UIScrollView.
从我的问题来看......循环 UIScrollView 但继续减速
将其转换为 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)
}
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我的旋转视图是arrowView. 和scrollView都arrowView应该是视图控制器视图的子视图。scrollView里面不应该有任何东西。
注意这是在浏览器中完成的,因此可能存在一些语法问题。您可能还需要将一些数字转换为CGFloat等等......
此外,能够从 Objective-C 进行翻译对于任何 iOS 开发人员来说都是至关重要的。数以百万计的应用程序是使用 Objective-C 编写的。语法可能略有不同,但所有 API 都是相同的。
学习如何做到这一点是每个 iOS 开发人员都应该做的事情。
| 归档时间: |
|
| 查看次数: |
609 次 |
| 最近记录: |