NSImageView的连续旋转(所以它似乎是动画的)

时间:2016-04-17 06:03:33

标签: swift macos

SWIFT - OSX

我在Main.storyboard中设置了一堆imageViews。当应用程序启动时,我试图让它们旋转,我希望它们无限期。我遇到了roateByAngle(角度:CGFloat),但这并没有给它制作动画,而只是跳到了新的角度。

我想创建两个函数,spinClockwise()和spinAntiClockwise(),所以我可以在viewDidLoad中调用它们,它们将继续转动。

我一直在玩CATransform3DMakeRotation,但似乎无法获得我想要的结果

        let width = myImg.frame.width / 2
        let height = myImg.frame.height / 2
        myImg.layer?.transform = CATransform3DMakeRotation(180,  width, height, 1)

让我知道我是否可以更具体。 感谢

2 个答案:

答案 0 :(得分:2)

您可以像这样添加UIView或UIImageView的扩展名:

extension UIView {

    ///The less is the timeToRotate, the more fast the animation is !
    func spinClockwise(timeToRotate: Double) {
        startRotate(CGFloat(M_PI_2), timeToRotate: timeToRotate)
    }

    ///The less is the timeToRotate, the more fast the animation is !
    func spinAntiClockwise(timeToRotate: Double) {
        startRotate(CGFloat(-M_PI_2), timeToRotate: timeToRotate)
    }

    func startRotate(angle: CGFloat, timeToRotate: Double) {
        UIView.animateWithDuration(timeToRotate, delay: 0.0, options:[UIViewAnimationOptions.CurveLinear, UIViewAnimationOptions.Repeat], animations: {
            self.transform = CGAffineTransformMakeRotation(angle)
            }, completion: nil)
        print("Start rotating")
    }

    func stopAnimations() {
        self.layer.removeAllAnimations()
        print("Stop rotating")
    }
}

因此,当您想要轮换myImg时,您只需致电:

myImg.spinClockwise(3)

当你想要阻止它时:

myImg.stopAnimations()

注意: 我添加了一个playground,以便您可以对其进行测试;)

干杯!

编辑:

我的不好,以下是NSView的示例:

extension NSView {

    ///The less is the timeToRotate, the more fast the animation is !
    func spinClockwise(timeToRotate: Double) {
        startRotate(CGFloat(-1 * M_PI * 2.0), timeToRotate: timeToRotate)
    }

    ///The less is the timeToRotate, the more fast the animation is !
    func spinAntiClockwise(timeToRotate: Double) {
        startRotate(CGFloat(M_PI * 2.0), timeToRotate: timeToRotate)
    }

    func startRotate(angle: CGFloat, timeToRotate: Double) {

        let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
        rotateAnimation.fromValue = 0.0
        rotateAnimation.toValue = angle
        rotateAnimation.duration = timeToRotate
        rotateAnimation.repeatCount = .infinity

        self.layer?.addAnimation(rotateAnimation, forKey: nil)

        Swift.print("Start rotating")
    }

    func stopAnimations() {
        self.layer?.removeAllAnimations()
        Swift.print("Stop rotating")
    }
}

重要说明:现在,在我的测试之后,我注意到您必须在中间设置NSView的锚点,以便它可以围绕其中心旋转:

view.layer?.anchorPoint = CGPointMake(0.5, 0.5)

我添加了一个带有OSX example

的新游乐场

答案 1 :(得分:0)

对我来说,我无法改变锚点。它在左下方旋转(0,0)。我将锚点移动到(0.5,0.5),但仍然没有运气。然后我遇到了this answer。我修改了下面的代码,它开始围绕自己旋转。我观察到一个缺点,视图的位置以某种方式移动了,但它可以通过反复试验来解决,试图将它移到正确的位置。

extension NSView {

    func startRotating(duration: Double = 1) {

        let kAnimationKey = "rotation"

        //self.wantsLayer = true
        if layer?.animation(forKey: kAnimationKey) == nil {
            var oldFrame = self.frame
            self.layer?.anchorPoint = CGPoint(x: 1, y: 1)
            self.frame = oldFrame

            let animate = CABasicAnimation(keyPath: "transform.rotation")
            animate.duration = duration
            animate.repeatCount = Float.infinity
            animate.fromValue = 0.0
            animate.toValue = Double.pi * 2.0
            self.layer?.add(animate, forKey: kAnimationKey)

            oldFrame = self.frame
            self.layer?.anchorPoint = CGPoint(x: 0.5, y: 0.5)
            self.frame = oldFrame
        }


     }
}