首先先看下动态效果,然后分析下这个过程都需要用到哪些技术以及都需要注意哪些问题。
分析当前效果:两个视图信息:1是最顶端Uber图标的UIimageView视图,2是紧挨的BackView的动画Uiview视图
首先,UIImageView的图层动画动画效果实现
func doAnimation(){
var images:[UIImage] = []
var image:UIImage?
var imageName:String?
for index in 0...67{
imageName = "logo-" + String(format: "%03d", index)
image = UIImage(named: imageName!)
images.insert(image!, atIndex: index)
}
backImageView.animationImages = images
backImageView.animationRepeatCount = 1
backImageView.animationDuration = 5
backImageView.startAnimating()
其次,BackView视图的动画效果,动画效果需要用到AVPlayer这个复杂类,AVPlayer本身是不会播放视频的,需要将视频加载到AVPlayerLayer才能显示出来,其中有几个重要的参数:
AVPlayerItem(URL: url),
AVPlayer(playerItem:self.playerItem),
AVPlayerLayer(player: player)
//获取资源文件
let path = NSBundle.mainBundle().pathForResource("welcome_video", ofType: "mp4")
let url = NSURL.fileURLWithPath(path!)
//初始化playerItem
self.playerItem = AVPlayerItem(URL: url)
//初始化player
self.player = AVPlayer(playerItem:self.playerItem)
//播放视频需要一个layer承载
//加入layer层来放置播放图层
//错误发生在这个地方:let playerLayer = AVPlayerLayer(layer: player)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = UIScreen.mainScreen().bounds
playerLayer.videoGravity = AVLayerVideoGravityResizeAspect
//0表示不覆盖我们的登陆,注册按钮
backView.layer.insertSublayer(playerLayer, atIndex: 0)
backView.alpha = 0.0
这里还要强调一点,因为当前有多个视图,我们的layer是附属于backView视图上面的,在我们的backView上面有两个button也算是两个图层的概念,否则是会被覆盖了得。
//Inserts the specified layer into the receiver’s list of sublayers at the specified index
backView.layer.insertSublayer(playerLayer, atIndex: 0)
再次,为了能够重复的播放AV视频,需要用到通知类,实时观察当前AV视频的状态并回调我们的执行函数
//为了能够循环的播放video,需要加入通知的方式,object是参数
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "didFinishVideo:",
name: AVPlayerItemDidPlayToEndTimeNotification,
object: playerItem)
func didFinishVideo(sender:NSNotification){
let item = sender.object as! AVPlayerItem
//Moves the playback cursor to a given time
item.seekToTime(kCMTimeZero)
self.player.play()
}
这个类非常的强大,需要好好研究研究,多写一些例子