用UIGestureRecognizer旋转瓶子

我现在使用这个代码来旋转button上的瓶子:

@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") } 

但是我怎样才能使用手势旋转button? 所以越快越快我的手指,瓶子旋转得越快

简单的答案是…使用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) } 

在这个例子中,我的旋转视图是arrowView视图。 scrollViewarrowView都应该是视图控制器视图的子视图。 scrollView不应该有任何内容。

注意这是在浏览器中完成的,所以可能会有一些语法问题。 你也可能需要将一些数字转换成CGFloat等等。

而且,能够从Objective-C进行翻译对于任何iOS开发者都是至关重要的。 数以百万计的应用程序是使用Objective-C编写的。 语法可能略有不同,但所有的API都是一样的。

学习如何做到这一点是每个iOS开发者应该做的。

我已经创build了一个示例应用程序,它有一个中心视图,您可以使用UIRotationGestureRecognizer进行旋转,并且旋转速度将根据旋转速度受到影响。 我已经使用了UIDynamics

 class ViewController: UIViewController { @IBOutlet weak var sampleView: UIView! var animator: UIDynamicAnimator? override func viewDidLoad() { super.viewDidLoad() setupRotationGesture() } override func viewDidAppear(animated: Bool) { animator = UIDynamicAnimator(referenceView: self.view) let sampleViewBehavior = UIDynamicItemBehavior(items: [self.sampleView]) sampleViewBehavior.allowsRotation = true // view can rotate animator?.addBehavior(sampleViewBehavior) let anchoredSuperViewBehavior = UIDynamicItemBehavior(items: [self.view]) anchoredSuperViewBehavior.anchored = true animator?.addBehavior(anchoredSuperViewBehavior) // Attachment between the sample view and super view at center anchor point. let attachment = UIAttachmentBehavior.pinAttachmentWithItem(self.sampleView, attachedToItem: self.view, attachmentAnchor: CGPointMake(self.view.center.x + 1, self.view.center.y + 1)) animator?.addBehavior(attachment) } // MARK: - Rotation Gesture - func setupRotationGesture(){ let rotationGesture = UIRotationGestureRecognizer(target: self, action: #selector(handleRotationGesture(_:))) self.sampleView.addGestureRecognizer(rotationGesture) } func handleRotationGesture(gesture: UIRotationGestureRecognizer){ if gesture.state == .Ended { let push = UIPushBehavior(items: [self.sampleView], mode: .Instantaneous) push.magnitude = abs(50.5 * gesture.velocity) let transform = self.sampleView.transform push.angle = atan2(transform.b, transform.a); animator?.addBehavior(push) } } } 

当你运行你将能够旋转视图的旋转速度的手势。

使用UIPanGestureRecognizer 。 创build一个panGestureRecognizer并将其添加到你的瓶子的超级视图,并实现它的委托。 它具有诸如velocityInViewtranslationInViewvariables。 当用户完成平移时,使用它们来计算旋转的angular度,速度和持续时间。