开始之前
创建基本动画
这效果是从MOV文件转成GIF的,而且CSDN不支持大于2M的图片上传,优酷地址
创建一个Single View Application工程,再创建一个Swift文件,我创建的叫“PulsingRadarView”,目前结构为:
在ViewController里面持有一个Optional的PulsingRadarView的属性,表示可以为nil,然后在viewDidLoad里做一个简单的初始化工作:
class ViewController: UIViewController {
var radarView: PulsingRadarView!
override func viewDidLoad() {
super.viewDidLoad()
let radarSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.width)
radarView = PulsingRadarView(frame: CGRectMake(0,(self.view.bounds.size.height-radarSize.height)/2,
radarSize.width,radarSize.height))
self.view.addSubview(radarView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
雷达是圆形的,所以宽高都是self.view.bounds.size.width。
PulsingRadarView里面现在应该是空的,我们首先导入QuartzCore,因为后面动画部分会用到CALayer,然后重写drawRect方法:
override func drawRect(rect: CGRect) {
UIColor.whiteColor().setFill()
UIRectFill(rect)
let pulsingCount = 6
let animationDuration: Double = 4
var animationLayer = CALayer()
for var i = 0; i < pulsingCount; i++ {
var pulsingLayer = CALayer()
pulsingLayer.frame = CGRectMake(0, 0, rect.size.width, rect.size.height)
pulsingLayer.borderColor = UIColor.grayColor().CGColor
pulsingLayer.borderWidth = 1
pulsingLayer.cornerRadius = rect.size.height / 2