iOS开发之音乐播放器专辑图片旋转动画

来源:互联网 发布:dota2快速模式知乎 编辑:程序博客网 时间:2024/04/30 14:20

仿网易云音乐专辑图片的旋转动画



用过网易云音乐的人都知道,在网易云音乐的控制界面有一张旋转的,类似唱片一样的图片,这个效果其实是两张图片合成的,一张是黑色胶片,中间一张是专辑图片。这里不说怎么合成一张图片,只说说专辑图片的旋转动画。废话不多说,看代码:


objective-c版:

SCImageView.h

////  SCImageView.h//  BaiduMusic////  Created by 凌       陈 on 8/22/17.//  Copyright © 2017 凌       陈. All rights reserved.//#import <UIKit/UIKit.h>@interface SCImageView : UIImageView-(void) startRotating;-(void) stopRotating;-(void) resumeRotate;@end


SCImageView.m

////  SCImageView.m//  BaiduMusic////  Created by 凌       陈 on 8/22/17.//  Copyright © 2017 凌       陈. All rights reserved.//#import "SCImageView.h"@implementation SCImageView// 开始旋转-(void) startRotating {    CABasicAnimation* rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];    rotateAnimation.fromValue = [NSNumber numberWithFloat:0.0];    rotateAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2];   // 旋转一周    rotateAnimation.duration = 20.0;                                 // 旋转时间20秒    rotateAnimation.repeatCount = MAXFLOAT;                          // 重复次数,这里用最大次数    [self.layer addAnimation:rotateAnimation forKey:nil];  }// 停止旋转-(void) stopRotating {        CFTimeInterval pausedTime = [self.layer convertTime:CACurrentMediaTime() fromLayer:nil];    self.layer.speed = 0.0;                                          // 停止旋转    self.layer.timeOffset = pausedTime;                              // 保存时间,恢复旋转需要用到}// 恢复旋转-(void) resumeRotate {        if (self.layer.timeOffset == 0) {        [self startRotating];        return;    }        CFTimeInterval pausedTime = self.layer.timeOffset;    self.layer.speed = 1.0;                                         // 开始旋转    self.layer.timeOffset = 0.0;    self.layer.beginTime = 0.0;    CFTimeInterval timeSincePause = [self.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;                                             // 恢复时间    self.layer.beginTime = timeSincePause;                          // 从暂停的时间点开始旋转}@end

swift版:

////  SCImageView.swift//  SC Player////  Created by 凌       陈 on 7/9/17.//  Copyright © 2017 凌       陈. All rights reserved.//import UIKitclass SCImageView: UIImageView {    var ablumView: UIImageView?        required init?(coder aDecoder: NSCoder) {        super.init(coder: aDecoder)                self.ablumView = UIImageView(frame: CGRect(x: self.frame.size.width / 2 - 80, y: self.frame.size.height / 2 - 80, width: 160, height: 160))        self.ablumView?.clipsToBounds = true        self.ablumView?.layer.cornerRadius = 80                self.addSubview(ablumView!)    }        func startRotating() {        let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")        rotateAnimation.fromValue = 0.0        rotateAnimation.toValue = Double.pi * 2        rotateAnimation.duration = 20.0        rotateAnimation.repeatCount = MAXFLOAT                self.layer.add(rotateAnimation, forKey: nil)    }        func stopRotating() {                let pausedTime = self.layer.convertTime(CACurrentMediaTime(), from: nil)        self.layer.speed = 0.0        self.layer.timeOffset = pausedTime    }        func resumeRotate() {                if self.layer.timeOffset == 0 {            startRotating()            return        }                let pausedTime = self.layer.timeOffset        self.layer.speed = 1.0        self.layer.timeOffset = 0.0        self.layer.beginTime = 0.0        let timeWhenpause = self.layer.convertTime(CACurrentMediaTime(), from: nil) - pausedTime        self.layer.beginTime = timeWhenpause    }}
swift版中有合成专辑图片,其实就是图中图,UIImageView中再定义一个UIImageView。

阅读全文
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 25号社保不交怎么办欠费 会计从业停考了怎么办 黑龙江龙育黄了档案怎么办 科目四档案丢了怎么办 从上海调档案到杭州怎么办 户口迁移身份证变更护照怎么办 有中国签证的孩子怎么办户口 大学生户口在学校怎么办签证 户口在南方人才市场怎么办签证 报警电话接到说方言的怎么办 学生去新加坡旅游签证怎么办 出入境的受理编号不见了怎么办 职工下岗档案单位丢失怎么办退休 大学毕业档案在自己手里怎么办 青岛李沧区房产证加名字怎么办手续 公积金迟交了1天怎么办 科一预约不上怎么办 科目一考试没去怎么办 科目一成绩单丢了怎么办 考科目二身份证丢了怎么办 考科目三身份证丢了怎么办 考科目一身份证丢了怎么办 滴滴顺风车被永久封禁怎么办 三岁宝宝智力发育迟缓怎么办 学车体检听力没过怎么办 学车体检听力不过关怎么办 离职体检发现听力有问题怎么办 买的二手房土地证没过户怎么办 b2扣了9分怎么办 上海网约车车证怎么办 b2驾照累计12分怎么办 c1本扣满12分怎么办 太原公共自行车丢了怎么办 在太原出租车丢了东西怎么办 太原社保卡丢了怎么办 太原铁路医保卡丢了怎么办 学车体检视力不合格怎么办 换驾照色弱被拒怎么办 在上海考驾照贵怎么办 驾驶证有效期过了半年怎么办 资格证脱审2年了怎么办