0

我有这段代码可以快速使用加速度计,但是当条件为真时我需要动画视图,但不是动画。

我可以做些什么来制作动画。谢谢

    import CoreMotion

lazy var motionManager = CMMotionManager()


override func viewDidAppear(animated: Bool) {
    super.viewDidLoad()

    if motionManager.accelerometerAvailable{
        let queue = NSOperationQueue()
        motionManager.startAccelerometerUpdatesToQueue(queue, withHandler:
            {(data: CMAccelerometerData!, error: NSError!) in

                if data.acceleration.x <= -0.22 && data.acceleration.x >= -0.24 {

                         UIView.animateWithDuration(1 ,delay: 0, options: .CurveEaseInOut  | .AllowUserInteraction,
                            animations: {

                                self.text.frame = CGRectMake(100, 0, 42,21)
                        },
                        completion:{ finished in

                    })

                }

            }
        )
    } else {
        println("Accelerometer is not available")
    }

}
4

2 回答 2

0
 import CoreMotion

lazy var manager = CMMotionManager()


if manager.accelerometerAvailable {
        manager.accelerometerUpdateInterval = 0.2
        manager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue()) {
            [weak self] (data: CMAccelerometerData!, error: NSError!) in

            if data.acceleration.x >= 0.1 {

                var  petalo1 = UIImageView()

                petalo1.image = UIImage(named: "petalo1.png")
                petalo1.frame = CGRectMake (20, 75, 75, 75)
                self?.view.addSubview(petalo1)


                UIView.animateWithDuration(4 ,delay: 0, options: .CurveEaseInOut  | .AllowUserInteraction,
                    animations: {
                        petalo1.frame = CGRectMake (20, 811, 75, 75)
                    },
                    completion:{ finished in
                        petalo1.removeFromSuperview()
                })

            }
        }
    }else {
                println("Accelerometer is not available")
    }
于 2015-03-09T05:09:27.163 回答
0

斯威夫特 3 语法

lazy var motionManager = CMMotionManager()
if motionManager.isAccelerometerAvailable {

        motionManager.accelerometerUpdateInterval = 0.2

        motionManager.startAccelerometerUpdates(to: OperationQueue.current!) { (data, error) in
            if let myData = data
            {
                if myData.acceleration.x >= 0.1 {

                    let petalo1 = UIImageView()

                    petalo1.image = UIImage(named: "petalo1.png")
                    petalo1.frame = CGRect(x: 20, y: 75, width: 75, height: 75)
                    self.view.addSubview(petalo1)


                    UIView.animate(withDuration: 4 ,delay: 0, options: [.curveEaseOut, .allowUserInteraction], animations: {
                        petalo1.frame = CGRect(x: 20, y: 811, width: 75, height: 75)
                    }, completion:{ finished in
                        petalo1.removeFromSuperview()
                    })
                }
            }
        }
    }
于 2017-03-21T19:40:26.193 回答