个人技术博客站欢迎您
创建一个继承于UIView的视图我们叫他XTActivityView
/// 立方柱的个数
var numberOfRect = 0;
/// 立方柱的颜色
var rectBackgroundColor: UIColor?
/// 立方柱初始化大小
var defaultSize: CGSize?
/// 立方柱之间的间距
var space: CGFloat = 0.0
进行初始化
override init(frame: CGRect) {
super.init(frame: frame)
self.createDefaultAttribute(frame)
}
func createDefaultAttribute(frame: CGRect) -> Void {
numberOfRect = 6;
self.rectBackgroundColor = UIColor.blackColor()
space = 1;
defaultSize = frame.size
}
提供开始和结束接口(即创建和移除)
func startAnimation() {
self.addRect()
}
func stopAnimation() {
self.removeRect()
}
添加矩形
func addRect() -> Void {
self.removeRect()
self.hidden = false
for i in 0...numberOfRect {
let x = (CGFloat)(i) * (5 + space)
let rView = UIView.init(frame: CGRectMake(x, 0, 5, defaultSize!.height))
rView.backgroundColor = rectBackgroundColor
rView.layer.addAnimation(self.addAnimateWithDelay((Double)(i) * 0.2), forKey: "TBRotate")
self.addSubview(rView)
}
}
动画效果
func addAnimateWithDelay(delay: Double) -> CAAnimation {
let animation = CABasicAnimation.init(keyPath: "transform.rotation.x")
animation.repeatCount = MAXFLOAT;
animation.removedOnCompletion = true;
animation.autoreverses = false;
animation.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionLinear)
animation.fromValue = NSNumber.init(float: 0)
animation.toValue = NSNumber.init(double: M_PI)
animation.duration = (Double)(numberOfRect) * 0.2;
animation.beginTime = CACurrentMediaTime() + delay;
return animation;
}
移除矩形
func removeRect() -> Void {
if self.subviews.count > 0 {
self.removeFromSuperview()
}
self.hidden = true
}
在控制器中调用
self.view.backgroundColor = UIColor.whiteColor()
let xt = XTActivityView.init(frame: CGRectMake(0, 10, 80, 15))
xt.startAnimation()
self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: xt)
xt.performSelector(Selector("stopAnimation"), withObject: nil, afterDelay: 5)
各位大大我是demo地址 点赞喔
时间: 2024-10-09 06:01:50