iOS实现音乐播放器图片旋转示例

发布时间:2020-10-26 17:34:42 作者:Leah
来源:亿速云 阅读:237

今天就跟大家聊聊有关iOS实现音乐播放器图片旋转示例,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

通过给继承与 UIImageView 的类 CXGImageView 添加 CABasicAnimation 转动动画,实现播放器图片转动效果.

主要提供三个方法: startRotating, stopRotating,resumeRotate

startRotating

/// 开始动画
func startRotating() {
 let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
 rotateAnimation.isRemovedOnCompletion = false // 避免点击 Home 键返回,动画停止
 rotateAnimation.fromValue = 0.0
 rotateAnimation.toValue = Double.pi * 2
 rotateAnimation.duration = 20
 rotateAnimation.repeatCount = MAXFLOAT
 self.layer.add(rotateAnimation, forKey: nil)
 isRotating = true
}

stopRotating

/// 停止动画
func stopRotating() {
 if !isRotating {
 return
 }
 let pausedTime = self.layer.convertTime(CACurrentMediaTime(), from: nil)
 // 让CALayer的时间停止走动
 self.layer.speed = 0
 // 让CALayer的时间停留在pausedTime这个时刻
 self.layer.timeOffset = pausedTime
 isRotating = false
}

resumeRotate

/// 继续动画
func resumeRotate() {

 if isRotating {
 return
 }

 if self.layer.timeOffset == 0 {
 startRotating()
 return
 }

 let pausedTime = self.layer.timeOffset
 // 1. 让CALayer的时间继续行走
 self.layer.speed = 1.0
 // 2. 取消上次记录的停留时刻
 self.layer.timeOffset = 0.01
 // 3. 取消上次设置的时间
 self.layer.beginTime = 0.0
 // 4. 计算暂停的时间(这里也可以用CACurrentMediaTime()-pausedTime)
 let timeWhenpause = self.layer.convertTime(CACurrentMediaTime(), from: nil) - pausedTime
 // 5. 设置相对于父坐标系的开始时间(往后退timeSincePause)
 self.layer.beginTime = timeWhenpause
 isRotating = true

}

注意: 使用中发现,当点下 Home 键再次返回应用时,图片停止转动,需要将动画 isRemovedOnCompletion 置成 false.

看完上述内容,你们对iOS实现音乐播放器图片旋转示例有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

推荐阅读:
  1. python如何实现可下载音乐的音乐播放器
  2. 使用iOS怎么实现音乐播放器

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ios

上一篇:使用C# / VB.NET 实现在PPT中创建、编辑PPT SmartArt图形

下一篇:在linux上搭建Solr集群的方法有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》