编辑代码

//听音看形识动物(第三章)

//3.3.2UIView动画播放方法

    //UIView的旋转动画
    //旋转组件360度
iet iv = self.view.viewwithTag (4)!
UIView.animate (withDuration : 2, animations: {
    iv.transform = iv.transform.rotated(by: CGFloat (360))
}

    //UIView的平移动画
let iv = self.view. viewwithTag (4)!
UIView.animate (withDuration: 2, animations: {
    iv.frame.origin.x += 100
    if iv.frame.origin.x > self.view.frame.size.width {
        iv.frame.origin.x = 
    }
})

    //UIView的缩放动画
let iv = self.view.viewNithTag (4)!
UIView.animate (withDurat ion: 2, animations:{
    iv.transform = CGAffineTransform (scalex: 0.8,y:0.8)
})

    //UIView的透明度动画
let iv = self.view. viewwithTag (4) !
UIView.animate (withDuration: 2, animations: {
    iv.alpha = 0.1
})

//视频播放
import UIKit 
import AVKit

class ViewController: UIViewController {

    @IBAction func Play (_ sender : Any)
    {
        //打开在沙盒中的视频文件 test.mov
        let path = Bundle.main. path (forResource: "test", ofType: "mov")
        //转化为 URL
        let url = URL (fileURLWithPath: path!)
        //打开AVPlaver
        let player = AVPlayer (url: url)
        //AVPlaverViewcontroller 实例化
        let playerViewController = AVPlayerViewController ()
        //设置播放器
        playerViewcontroller.player = plaver
        //添加播放器的 View 大小和范国
        playerViewController.view.frame = CGRect (x: 20, y: 100, width:self.view.bounds.width - 40, height: 200)
        self.addChild(playerViewController)
        self.view.addSubview(playerViewController.view)
    }
}

//HTTP请求的许可使用设置

import UIKit
import AVKit

class ViewController: UIViewController {

    @IBAction func NetworkPlay(_ sender: UIButton) {
        let neturl = "http://bos.nj.bpc.baidu.com/tieba-smallvideo/117723c435014fb2dd9a5fd56a57cc369£6a0.mp4"
        guard let networkurl = URL(string: neturl ) else { return }
        //播放网络上的视频
        let player = AVPlayer (url: networkUrl)
        let playerViewController = AVPlayerViewController ()
        playerViewController.player = player
    //弾出新窗ロ
    self.present (playerViewController,animated: true,completion:nil)
    }
}


//3.5.1听音看形识动物

import UIKit
//导入视频支持库
import AVKit
//导入音频支持库
import AVFoundation

classViewController: UIViewcontroller {
    //是否播放视频标记
    var playflag = false
    //视频播放控制器
    var playerViewController = AVPlayerViewController ()
    //音频播放器变量
    var soundPlayer : AVAudioPlayer!

    override func viewDidLoad () {
        super.viewDidLoad ()
        // Do any additional setup after loading the view.
        let iv = self.view.viewwithTag (4)!asUIImageView
        //UIImage的一个方法animatedImageNamed可以调入一系列图片
        //毎个圏片出珋的同隔吋同力0.5秒
        let img = UI Image. animatedImageNamed (" Erame-", duration: 0.5)
        //设置动画
        iv.image = img

        //开启定时器,自动调用相关函数
        Timer.scheduledTimer (timeInterval: 2.0, target: self, selector:#selector(doTimer), userInfo: nil, repeats: true)
    }

    @objc func doTimer () {
        let iv = self. view.viewwithTag (4) !
        UIView.animate (withDuration: 2, animations: {
            iv.transform = iv.transform. rotated (by: CGFloat (360))
        }
    })

    @IBAction func onPlayAudio senderUIButton) {
        //获取当前按钮的title
        let title = sender.currentTitle
        //从沙盒中读取文件所在路径,文件名为bird.mp3
        let path = Bundle.main.path(forResource: title, ofType: "mp3")
        //将路径转换为URL
        let url = URL (fileURLWithPath: path!)
        //调用AVAudioPlayer函数,生成播放器
        soundPlaver = try? AVAudioPlayer (contentsof: url)
        //播放器播放声音,默认是循环播放方式
        soundPlayer.play()
    }
    @IBAction func onPlayVideo (_ sender: UIButton) {
        if !playFlag {
            //打开在沙盒中的视频文件 test.mov
            let path = Bunale.main.path (forResource: "rollinwd Id", ofType: "m4a"
            //转化为 URI
            let url = URL (fileURLWithPath: path!)
            //打开 AVPlayer
            let player = AvPlayer (url: url)
            //AVPlayerViewcontroller 实例化
            player.play ()
            //设置播放器
            playerViewcontroller.player = player
            //获得当前按钮的位置和尺寸
            let buttonPosition = sender.frame
            //视频播放的位置x
            let x = buttonPosition .origin.x-200
            //视频播放的位置y
            let y = buttonPosition.origin.y-120
            // 添加播放器的 View 大小和范国
            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屏幕旋转的处理

UIDeviceOrientationDidChangeNotification

    //1.在控制器的 viewDidLoad () 适当的地方注册通知监听者
        //设置 UIDevice 方向变化监听的通知
            NotificationCenter.default.addobserver(self,selector: #selector(receivedRotation(notification:)),name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
    //2.处理旋转过程中需要的操作
        //UIDevice 旋转方向通知监听触发的方法
            func receivedRotation(notification :NSNotification){
                backgroundView.frame = CGRect(x: 0,y: 0,width: self.view.frame.size.width,height: self.view.frame.size.height)
            }