iOS项目开发实战——设置视图的透明度改变动画

    在iOS中 ,透明度的改变可以让View视图以一种渐变的效果动态的出现或者消退,非常有意思。这里我们将会对一个View控件的色块执行透明度改变的动画。关于其他的动画效果可以参考我的其他几篇博客《iOS项目开发实战——制作视图的平移动画以及解决移动异常问题》,《iOS项目开发实战——多个视图的平移动画与闭包函数的声明与调用》。

(1)在Main.storyboard中拖入一个View,并且绑定到代码中。

(2)实现代码如下:

import UIKit

class OpacityViewController: UIViewController {

    @IBOutlet weak var greenSquare: UIView!
    
    
    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.alpha = 0.2   //改变透明度到0.2
            
        }
        
        UIView.animateWithDuration(2, animations: anim)//时常为2s;
        
        
    }
    

}

(3)运行程序,发现色块可以动态的改变透明度Alpha。


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

你可能感兴趣的:(ios,动画,透明度,swift)