旋转瓶与UIGestureRecognizer [英] Spin bottle with UIGestureRecognizer

查看:76
本文介绍了旋转瓶与UIGestureRecognizer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在使用此代码在按钮上旋转瓶子:

I am using this code now to spin bottle on button tap:

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

但是如何使用手势旋转按钮?因此,我移动手指越难/越快,瓶子旋转得越快

But how can I rotate the button using gesture? So the harder/faster I move my finger, the faster the bottle will spin

推荐答案

对此的简单回答是......使用 UIScrollView

The simple answer to this is... use a UIScrollView.

从我的问题... 循环UIScrollView但继续减速

将它翻译成Swift是微不足道的,但这里是...

Translating it to Swift is trivial but here goes...

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 scrollView arrowView 都应该是视图控制器视图的子视图。 scrollView 中不应包含任何内容。

In this example my spinning view is the arrowView. The scrollView and arrowView should both be subviews of the view controller's view. The scrollView should not have anything in it.

N.B。这是在浏览器中完成的,因此可能存在一些语法问题。您可能还需要将一些数字转换为 CGFloat 等...

N.B. This is done in the browser so there may be some syntax problems. You may also have to cast some of the numbers to CGFloat etc...

另外,能够翻译来自Objective-C对于任何iOS开发都是必不可少的。使用Objective-C编写了数百万个应用程序。语法可能略有不同,但所有API都是相同的。

Also, being able to translate from Objective-C is essential for any iOS dev. Millions of apps are written using Objective-C. The syntax may be slightly different but all of the APIs are the same.

学习如何做到这一点是每个iOS开发人员应该做的事情。

Learning how to do this is something that every iOS dev should be doing.

这篇关于旋转瓶与UIGestureRecognizer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆