今天的内容比较简单,我也就不做详细的文字介绍了,直接上代码,希望对大家有所帮助
var audioPlayer = AVAudioPlayer()
//梯度配色
let gradientLayer = CAGradientLayer()
var timer:Timer?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let redValue = CGFloat(drand48())
let blueValue = CGFloat(drand48())
let greenValue = CGFloat(drand48())
self.view.backgroundColor = UIColor(red: redValue, green: greenValue, blue: blueValue, alpha: 1.0)
//graditent color,梯度颜色
gradientLayer.frame = view.bounds
let color1 = UIColor(white: 0.5, alpha: 0.2).cgColor
let color2 = UIColor(red: 1.0, green: 0, blue: 0, alpha: 0.4).cgColor
let color3 = UIColor(red: 0, green: 1, blue: 0, alpha: 0.3).cgColor
let color4 = UIColor(red: 0, green: 0, blue: 1, alpha: 0.3).cgColor
let color5 = UIColor(white: 0.4, alpha: 0.2).cgColor
gradientLayer.colors = [color1, color2, color3, color4, color5]
gradientLayer.locations = [0.10, 0.30, 0.50, 0.70, 0.90]
gradientLayer.startPoint = CGPoint.init(x: 0, y: 0)
gradientLayer.endPoint = CGPoint.init(x: 1, y: 1)
self.view.layer.addSublayer(gradientLayer)
let playBtn = UIButton.init(frame: CGRect.init(x: (UIScreen.main.bounds.size.width-100)/2, y: (UIScreen.main.bounds.size.height-100)/2, width: 100, height: 100))
playBtn.setBackgroundImage(UIImage.init(named: "music play"), for: .normal)
playBtn.layer.cornerRadius = 50
playBtn.layer.borderWidth = 1
playBtn.layer.borderColor = UIColor.lightGray.cgColor
playBtn.addTarget(self, action: #selector(playBtnClick), for: .touchUpInside)
self.view.addSubview(playBtn)
}
//MARK:-------按钮点击
func playBtnClick(btn:UIButton) {
//play bg music
let bgMusic = NSURL(fileURLWithPath: Bundle.main.path(forResource: "Ecstasy", ofType: "mp3")!)
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
try audioPlayer = AVAudioPlayer(contentsOf: bgMusic as URL)
audioPlayer.prepareToPlay()
audioPlayer.play()
}
catch let audioError as NSError {
print(audioError)
}
if (timer == nil) {
timer = Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(ViewController.randomColor), userInfo: nil, repeats: true)
}
}
//定时器方法
func randomColor() {
let redValue = CGFloat(drand48())
let blueValue = CGFloat(drand48())
let greenValue = CGFloat(drand48())
self.view.backgroundColor = UIColor(red: redValue, green: greenValue, blue: blueValue, alpha: 1.0)
}
效果图:(源码下载:https://github.com/hbblzjy/SwiftRandomColorizationDemo)