iOS项目开发实战——制作View的颜色渐变动画

      一个View视图的颜色改变是一个最基本,也最容易引起用户注意的特性。现在我们来学习一下如何改变一个视图的颜色,并以动画的形式展现出来。

(1)在Main.storyboard中拖入一个Label和一个View,事先设置好这两个控件的颜色,然后绑定到代码中。

(2)实现代码如下:

import UIKit

class ColorViewController: UIViewController {

    
    @IBOutlet weak var greenSquare: UIView!
    
    @IBOutlet weak var swiftText: UILabel!
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

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

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        
        func anim(){
        
            self.greenSquare.backgroundColor = UIColor.blackColor()
            self.swiftText.textColor = UIColor.blueColor()
        }
        
        UIView.animateWithDuration(2, animations: anim)
        
    }
    
    
}

(2)运行程序,发现两个控件可以分别进行颜色改变的动画。


github主页:https://github.com/chenyufeng1991  。欢迎大家访问!

你可能感兴趣的:(ios,动画,swift,颜色变化)