Lor*_*esh 3 swift uiviewpropertyanimator
所以我一直在阅读一些有关 UIViewPropertyAnimator 的内容,在我看过的示例中,他们做了这样的事情:
\n\nanimator = UIViewPropertyAnimator(duration: 2.0, curve: .easeInOut, animations: { \n [unowned self, redBox] in\n redBox.center.x = self.view.frame.width\n redBox.transform = CGAffineTransform(rotationAngle: CGFloat.pi).scaledBy(x: 0.001, y: 0.001)\n })\nRun Code Online (Sandbox Code Playgroud)\n\n我不明白其中的“[unowned self, redBox]”部分。谁能解释一下我们用它做什么?
\n\n我知道 unowned 通常用于决定如何确定引用计数,并且不能将其设置为 nil,因为引用将不存在而没有另一个(作为弱的替代),但我不明白其用途在这里,我不明白括号部分。在我看来,它是一个动画项目及其所在视图的数组?
\n\n完整代码如下:
\n\nimport UIKit\n\nclass ViewController: UIViewController {\n\n var animator: UIViewPropertyAnimator!\n\n override func viewDidLoad() {\n super.viewDidLoad()\n\n //redBox\n\n let redBox = UIView(frame: CGRect(x: 10, y: 100, width: 100, height: 100))\n redBox.translatesAutoresizingMaskIntoConstraints = false// lar oss redigere posisjon og s\xc3\xa5nn selv, uten at xcode setter posisjon/st\xc3\xb8rrelse i stein.\n redBox.backgroundColor = .red\n redBox.center.y = view.center.y\n\n view.addSubview(redBox)\n\n animator = UIViewPropertyAnimator(duration: 2.0, curve: .easeInOut, animations: { \n [unowned self, redBox] in\n redBox.center.x = self.view.frame.width\n redBox.transform = CGAffineTransform(rotationAngle: CGFloat.pi).scaledBy(x: 0.001, y: 0.001)\n })\n\n // slider\n\n let slider = UISlider()\n slider.translatesAutoresizingMaskIntoConstraints = false\n view.addSubview(slider)\n slider.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true\n slider.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true\n slider.addTarget(self, action: #selector(sliderChanged), for: .valueChanged)\n\n }\n func sliderChanged(_ sender: UISlider){\n animator.fractionComplete = CGFloat(sender.value)\n }\n\n}\nRun Code Online (Sandbox Code Playgroud)\n
我们需要使用其中之一weak,unowned否则将创建所有权(引用)循环(self=> animator=> animations=> self)。
我们可以使用unowned代替,weak因为我们可以确定self和animator一起被销毁,并且当self被释放时,动画将不再运行。
| 归档时间: |
|
| 查看次数: |
872 次 |
| 最近记录: |