iOS Spin UIImageView带有在任意位置的减速运动

问题描述 投票:0回答:1

[我试图创建一个在点击时旋转的旋转轮,它旋转一定的时间并以某个随机的圆角停止。

import UIKit

class MasterViewController: UIViewController {

    lazy var imageView: UIImageView = {
        let bounds = self.view.bounds
        let v = UIImageView()
        v.backgroundColor = .red
        v.frame = CGRect(x: 0, y: 0,
                         width: bounds.width - 100,
                         height: bounds.width - 100)
        v.center = self.view.center
        return v
    }()

    lazy var subView: UIView = {
        let v = UIView()
        v.backgroundColor = .black
        v.frame = CGRect(x: 0, y: 0,
                         width: 30,
                         height: 30)
        return v
    }()

    var dateTouchesEnded: Date?
    var dateTouchesStarted: Date?

    var deltaAngle = CGFloat(0)
    var startTransform: CGAffineTransform?
    var touchPointStart: CGPoint?


    override func viewDidLoad() {
        super.viewDidLoad()
        self.imageView.addSubview(self.subView)
        self.view.addSubview(self.imageView)
        self.imageView.isUserInteractionEnabled = true
        self.setupGesture()
    }

    private func setupGesture() {
        let gesture = UITapGestureRecognizer(target: self,
                                             action: #selector(handleGesture(_:)))
        self.view.addGestureRecognizer(gesture)
    }

    @objc
    func handleGesture(_ sender: UITapGestureRecognizer) {
        var timeDelta = 1.0
        let _ = Timer.scheduledTimer(withTimeInterval: 0.2,
                                         repeats: true) { (timer) in
            if timeDelta < 0 {
                timer.invalidate()
            } else {
                timeDelta -= 0.03
                self.spinImage(timeDelta: timeDelta)
            }
        }
    }

    func spinImage(timeDelta: Double) {
        print("TIME DELTA:", timeDelta)

        let direction: Double = 1

        let rotation: Double = 1

        UIView.animate(withDuration: 5,
                       delay: 0,
                       options: .curveEaseOut,
                       animations: {

                        let transform = self.imageView.transform.rotated(
                            by: CGFloat(direction) * CGFloat(rotation) * CGFloat(Double.pi)
                        )
                        self.imageView.transform = transform

        }, completion: nil)
    }

}

我通过上面的代码尝试过,但始终在初始位置停止

即初始和最终转换是相同的。

我希望它每次都是随机的。

ios swift core-animation
1个回答
1
投票

您可以做的一件事情是在指定范围内创建一个带有随机数的计数器。时间到时,调用spinImage,然后递减计数器。继续这样做,直到计数器达到零为止。这个随机数会给您带来一定的可变性,因此您每次都不会得到相同的结果。

@objc func handleGesture(_ sender: UITapGestureRecognizer) {
    var counter = Int.random(in: 30...33)
    let _ = Timer.scheduledTimer(withTimeInterval: 0.2, repeats: true) { (timer) in
        if counter < 0 {
            timer.invalidate()
        } else {
            counter -= 1
            self.spinImage()
        }
    }
}

spinImage中,而不是旋转CGFloat.pi,而是旋转CGFloat.pi / 2,以便您有四个可能的结果,而不是两个。

func spinImage() {
    UIView.animate(withDuration: 2.5,
                   delay: 0,
                   options: .curveEaseOut,
                   animations: {
                    let transform = self.imageView.transform.rotated(
                        by: CGFloat.pi / 2
                    )
                    self.imageView.transform = transform

    }, completion: nil)
}

您可能想要弄乱计数器值,计时器间隔和动画持续时间以获得所需的效果。我在这里选择的值有些随意。

© www.soinside.com 2019 - 2024. All rights reserved.