3.2.1 声音播放方法(林中鸟鸣)
import UIKit
import AVFoundation
class ViewController: ViewController {
var soundPlayer : AVFoundation!
@IBAction func onClicke (_sender : UIButton){
let path = Bundle.main.path(foResource: "bird",ofTtpe: "mp3")
let url = URL(fileURLWithPath: path!)
soundPlayer = try? AVAudioPlayer(contentsof: url)
soundPlayer.play()
}
}
3.3.2 UIView动画播放方法
let iv = self.view.viewWithTag(4)
UIView.animate(withDuration: 2,animatons:{
iv.transfrom = iv.transfrom.rotated(by: CGFloat(360))
})
let iv =self.view.viewWithTag(4)!
UIView.animate(withDuration: 2,animatons:{
iv.frame.orign.x += 100
if iv.frame.origin.x > self.view.feame.size.width{
iv.frame.origin.x = 0
}
})
3.UIView的缩放动画
let iv =self.view.viewWithTag(4)!
UIView.animate(withDuration: 2,animatons:{
iv.transfrom=CGAffineTranfrom(scaleX: 0.8,y: 0.8)
})
4.UIView的透明度动画
let iv = self.view.viewWithTag(4)!
UIView.animate(withDuration: 2,animatons:{
iv.alpha = 0.1
})
import UIKit
import AVKit
class ViewController: UIViewController {
@IBAction func Play(_sender: Any)
{
let path = Bundle.main.path(forResource: "test",offype: "mov")
let url = URL(fileURLWithPath: path!)
let plsyer = AVPlayer(url: url)
let PlayerViewController = AVPlayerVIEWcontroller()
PlayerViewController.plsyer = plsyer
PlayerViewController.view.frame = CGRect(x:20,y: 100,width: self.view.bounds.width - 40,heihjt: 200)
self.addChild(PlayerViewController)
self.view.addSubview(PlayerViewController.view)
}
}
import UIKit
import AVKit
class ViewController: UIViewController {
@IBAction func NetworkPlay(_sender: UIButton) {
let neturl = "http://bos.nj.bpc.baidu.com/tieba-smallideo/11772_3c435014fb2dd9a5fd56a57cc369f6ao.mp4"
guard let networkUrl = URL(string: neturl ) else { return }
let player = AVPlayer(url: networkUrl)
let PlayerViewController = AVPlayerVIEWcontroller()
PlayerViewController.player = player
self.present(PlayerViewController, true, completion: nil)
}
}
3.5.1听音看形识动物
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController{
var playFlag=falae
var playerViewController=AVPlayerViewController()
var soundPlayer : AVAudioPlayer!
override funce viewDidLoad(){
super.viewDidLoad()
let iv =sellf.view.viewWithTag(4)! as! UIImageview
let img =UIImage.animatedImageNamed("frame_",duration:0.5)
iv.image=img
Timer.scheduleTimer(timerInterval:2.0,target:self,selector:#selector(dorTimer),userInfo:nil,repeats:true)
}
@objo func dorTimer(){
let iv =self.view.viewWithTag(4)!
UIView.animate(withDuration:2,animations:{
iv.transform=iv.transform.rotated(by:CGFloat(360))
})
}
@IBAction func onPlayAudio(_sender:UIButton){
let title=sender.currentTitle
let path =Bundle .main.path(forResource:title,ofType:"mp3")
let url=URL(fileURLWithPath:path!)
soundPlayer=try?AVAudioPlayer(contentsOf:url)
soundPlayer.Play()
}
@IBAction func onPlayVideo(_sender:UIButton){
if !playFlag{
let path=Bundle.main.path(forResource:"rollinwild",ofType:"mp4")
let url=URL(fileURLWithPath:path!)
let player=AVPlayer(url:url)
player.play()
playerViewController.player=player
let buttonPosition=sender.frame
let x =buttonPosition.origin.x-200
let y=buttonPosition.origin.y-120
playerViewController.view.frame=CGRect(x:x,y:y,width:200,height:112)
self.addChild(playerViewController)
self.view.addSubview(playerViewController.view)
playFlag=true
}
else{
playerViewController.view.removeFromSuperview()
playFlag=false
}
}
}
3.5.2 屏幕旋转的处理
NotificationCenter.default.addObserver(self,selector:#selector(receivedRotation(notification)),
name:NSNotification.Name.UIDevceOrientationDidChange,object:nil)
func receivedRotation(notification:NSNotification){
backgroundView.frame=CGRect(x:0,y:0,width:self.view.frame.size.width,height:self.view.frame.size.height)
}