UIView transform.scaleBy动画无效

时间:2018-06-10 07:01:16

标签: ios swift animation uiview

我在Objective C中为iOS做了一些业余爱好者编程,但是在Apple过渡到Swift的时候就退出了。最近开始尝试学习Swift,并拼凑一个非常简单的应用程序,只是为了开始理解它。

我有一个以UIView开头的屏幕和三个按钮,如图所示。 "成长"按钮旨在使视图(redBox)向上扩展,并且"收缩它"按钮应该相反。 "改变颜色"按钮将redBox的背景颜色更改为随机颜色。所有更改都旨在使用UIView.animate(withDuration: 2, animations:

进行动画处理

颜色变化有效,但缩放不起作用。希望有人能告诉我哪里出错了。

这里是代码,所有帮助都表示赞赏:

import UIKit
import CoreGraphics

public extension UIColor {
    public static var random: UIColor {
        let max = CGFloat(UInt32.max)
        let red = CGFloat(arc4random()) / max
        let green = CGFloat(arc4random()) / max
        let blue = CGFloat(arc4random()) / max

        return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
    }
}

class ViewController: UIViewController {

    @IBOutlet weak var redBox: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



    @IBAction func growBox(_ sender: UIButton) {
        UIView.animate(withDuration: 2, animations: {
        self.redBox.transform.scaledBy(x: 1.1, y: 1.1)
    },completion: nil)
    }



    @IBAction func shrinkIt(_ sender: UIButton) {
        UIView.animate(withDuration: 2, animations: {
            self.redBox.transform.scaledBy(x: 0.9, y: 0.9)
        },completion: nil)

    }



    @IBAction func changeColor(_ sender: UIButton) {
        UIView.animate(withDuration: 2, animations: {
            self.redBox.backgroundColor = UIColor.random
    }, completion: nil)
}

}

编辑1:

下面的答案,我将转换代码更改为:

@IBAction func growBox(_ sender: UIButton) {
    UIView.animate(withDuration: 2, animations: {
    self.redBox.transform = self.redBox.transform.scaledBy(x: 1.05, y: 1.05)
},completion: nil)
}



@IBAction func shrinkIt(_ sender: UIButton) {
    UIView.animate(withDuration: 2, animations: {
        self.redBox.transform = self.redBox.transform.scaledBy(x: 0.95238, y: 0.95238)
    },completion: nil)

}

虽然这似乎有效,但"收缩"变换留下了一些痕迹,如下:

enter image description here

有人知道这意味着什么吗?

4 个答案:

答案 0 :(得分:1)

您在此处犯的错误是您认为if (vDay == "Monday" ||vDay == "Tuesday" ||vDay == "Wednesday" ||vDay == "Thursday" ||vDay == "Friday") { Console.WriteLine("is not a valid day of the week"); } 会更改scaledBy属性。

它没有。 Swift中的方法名称实际上可以告诉您是否会改变调用它们的对象!查看它如何命名为transform(使用scaledBy)而不是d。这表明此方法将返回一个缩放的新变换。因为用英语,你会说这样的话:

  

我希望这种观点的转变能够成为这种特殊的转变,但是要将这个转变为2倍

在代码中你可以这样写:

scaleBy

这就是thisView.transform = thisParticularTransformation.scaledBy(x: 2, y: 2) 的使用方式。

如果您想将视图的转换更改为绝对转换(与其他转换无关),则需要创建新的scaledBy

CGAffineTransform

答案 1 :(得分:0)

你可以试试这个:

@IBAction func growBox(_ sender: UIButton) {
    UIView.animate(withDuration: 2, animations: {
        self.redBox.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
    }, completion: nil)
}

答案 2 :(得分:0)

你只是没有改变转换,这就是它无法正常工作的原因

您应该创建新的AffineTransform对象并分配到其中。

替换

self.redBox.transform.scaledBy(x: 1.1, y: 1.1)

self.redBox.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)

答案 3 :(得分:-1)

@IBAction func growBox(_ sender: Any) {
    UIView.animate(withDuration: 2, animations: {
        self.redView.transform = self.redView.transform.scaledBy(x: 1.1, y: 1.1)
    },completion: nil)
}