首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么在UIViewPropertyAnimator中使用“无主”

为什么在UIViewPropertyAnimator中使用“无主”
EN

Stack Overflow用户
提问于 2016-12-16 19:42:24
回答 1查看 808关注 0票数 3

所以我读了一些关于UIViewPropertyAnimator的文章,在我看过的例子中,他们做了这样的事情:

代码语言:javascript
复制
animator = UIViewPropertyAnimator(duration: 2.0, curve: .easeInOut, animations: { 
        [unowned self, redBox] in
        redBox.center.x = self.view.frame.width
        redBox.transform = CGAffineTransform(rotationAngle: CGFloat.pi).scaledBy(x: 0.001, y: 0.001)
    })

我不明白其中的“无我,redBox”的部分。有人能解释一下我们用它做什么吗?

我知道,非所有权通常用于决定引用计数是如何确定的,并且它不能设置为零,因为引用将不存在而另一个不存在(作为弱的替代),但我不理解这里的用法,我也不理解括号部分。在我看来,它是一个项目的数组,它的动画和视图的位置?

完整代码如下:

代码语言:javascript
复制
import UIKit

class ViewController: UIViewController {

    var animator: UIViewPropertyAnimator!

    override func viewDidLoad() {
        super.viewDidLoad()

        //redBox

        let redBox = UIView(frame: CGRect(x: 10, y: 100, width: 100, height: 100))
        redBox.translatesAutoresizingMaskIntoConstraints = false// lar oss redigere posisjon og sånn selv, uten at xcode setter posisjon/størrelse i stein.
        redBox.backgroundColor = .red
        redBox.center.y = view.center.y

        view.addSubview(redBox)

        animator = UIViewPropertyAnimator(duration: 2.0, curve: .easeInOut, animations: { 
            [unowned self, redBox] in
            redBox.center.x = self.view.frame.width
            redBox.transform = CGAffineTransform(rotationAngle: CGFloat.pi).scaledBy(x: 0.001, y: 0.001)
        })

        // slider

        let slider = UISlider()
        slider.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(slider)
        slider.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        slider.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
        slider.addTarget(self, action: #selector(sliderChanged), for: .valueChanged)

    }
    func sliderChanged(_ sender: UISlider){
        animator.fractionComplete = CGFloat(sender.value)
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-16 19:49:19

  1. 我们需要使用weakunowned,否则将创建所有权(引用)循环(self => animator => animations => self)。
  2. 我们可以使用unowned而不是weak,因为我们可以确保selfanimator一起被销毁,当self被解除分配时,动画将不再运行。
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41191437

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档