简易动画:
iview.animationImages = {
var images: [UIImage] = []
for i in 0...40 {
if let image = UIImage(named: "") {
images.append(image)
}
}
return images
} ()
animationImageView.animationDuration = 0.5;//设置动画时间
animationImageView.animationRepeatCount = 0;//设置动画次数 0 表示无限
[animationImageView startAnimating];//开始播放动画
该方法只有stop没有暂停功能,不适用于多个image;
多image实现方式:
let myAnimatedTimer = NSTimer.scheduledTimerWithTimeInterval(0.04, target: self, selector: "setNextImage", userInfo: nil, repeats: true)
func setNextImage() {
iview.image = UIImage(named: "image(nextImage).png")
}
简易放大缩小动画:
targetAnimation = CABasicAnimation(keyPath: "transform.scale")
targetAnimation?.duration = 1.0
targetAnimation?.repeatCount = Float.infinity //不停重复
targetAnimation?.autoreverses = true
targetAnimation?.fromValue = NSNumber(float: 1.0)
targetAnimation?.toValue = NSNumber(float: 1.2)
targetL?.layer.addAnimation(targetAnimation!, forKey: "scale-layer")
//暂停layer上的动画
func pauseLayer(layer: CALayer) {
//Absolute Time(绝对时间)的概念,可以通过CACurrentMediaTime()获得
let pausedTime = layer.convertTime(CACurrentMediaTime(), fromLayer: nil) //图层当前的时间
layer.speed = 0.0 //当前对象的时间流相对于父级对象时间流的流逝速度
//timeOffset则是active local time的偏移量.将一个动画看作一个环,timeOffset改变的其实是动画在环内的起点
layer.timeOffset = pausedTime
}
//继续layer上的动画
func resumeLayer(layer: CALayer) {
let pausedTime = layer.timeOffset
layer.speed = 1.0
layer.timeOffset = 0.0
layer.beginTime = 0.0
let timeSincePause = layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pausedTime
layer.beginTime = timeSincePause
}
截取scrollView上的区域
static func captureScrollView(scrollV: UIScrollView, atFrame: CGRect? = nil) -> UIImage{
var shareImg = UIImage()
let savedFrame = scrollV.frame
scrollV.frame.size.height = atFrame?.height ?? 0
UIGraphicsBeginImageContextWithOptions(scrollV.frame.size, false, UIScreen.mainScreen().scale)
scrollV.setContentOffset(CGPointZero, animated: false)
let context: CGContextRef = UIGraphicsGetCurrentContext()!
let savedContentOffset = scrollV.contentOffset
if atFrame != nil {
CGContextSaveGState(context)
UIRectClip(atFrame!)
}
scrollV.layer.renderInContext(context) //将当前layer的呈现拷贝到context上
let img = UIGraphicsGetImageFromCurrentImageContext()
if img != nil {
shareImg = img
}
scrollV.setContentOffset(savedContentOffset, animated: false)
scrollV.frame = savedFrame
UIGraphicsEndImageContext()
return shareImg
}
环形进度条:
class HoopVateView: UIView {
struct Constant {
//进度条宽度
static let lineWidth: CGFloat = 6
//进度条颜色
static let progressColoar = HistoryTool.HistoryBottomLightColor
}
//进度条
let progressLayer = CAShapeLayer()
//进度条路径(整个圆圈)
let path = UIBezierPath()
//当前进度
@IBInspectable var progress: Double = 0 {
didSet {
if progress > 100 {
progress = 100
}else if progress < 0 {
progress = 0
}
}
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = CC.transparent
}
//进度槽
let trackLayer = CAShapeLayer()
override func drawRect(rect: CGRect) {
//获取整个进度条圆圈路径
path.addArcWithCenter(CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds)),
radius: bounds.size.width/2 - Constant.lineWidth / 2,
startAngle: angleToRadian(-90), endAngle: angleToRadian(270), clockwise: true)
//绘制进度条
progressLayer.frame = bounds
progressLayer.fillColor = UIColor.clearColor().CGColor
progressLayer.strokeColor = Constant.progressColoar.CGColor
progressLayer.lineWidth = Constant.lineWidth
progressLayer.lineCap = kCALineCapRound; //起点样式
progressLayer.lineJoin = kCALineJoinRound; //终点样式
progressLayer.path = path.CGPath
progressLayer.strokeStart = 0
print(progress)
progressLayer.strokeEnd = CGFloat(progress)
layer.addSublayer(progressLayer)
}
//设置进度(可以设置是否播放动画)
func setProgress(pro: Double,animated anim: Bool) {
// setProgress(pro, animated: anim, withDuration: 0.55)
progress = pro
progressLayer.strokeEnd = CGFloat(progress)/100.0
}
}