extension UIView {
public func colorOfPoint(point:CGPoint) -> UIColor
{
var pixel:[CUnsignedChar] = [0,0,0,0]
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue)
let context = CGBitmapContextCreate(&pixel, 1, 1, 8, 4, colorSpace, bitmapInfo.rawValue)
CGContextTranslateCTM(context, -point.x, -point.y)
self.layer.renderInContext(context!)
let red: CGFloat = CGFloat(pixel[0]) / 255.0
let green: CGFloat = CGFloat(pixel[1]) / 255.0
let blue: CGFloat = CGFloat(pixel[2]) / 255.0
let alpha: CGFloat = CGFloat(pixel[3]) / 255.0
return UIColor(red:red, green: green, blue:blue, alpha:alpha)
}
}
class func createExplosionPoints(containerLayer: ExplosionLayer, targetView: UIView, animationType: ExplosionAnimationType) {
let hCount = self.caculatePointHCount(containerLayer.targetSize.width)
let vCount = self.caculatePointVCount(containerLayer.targetSize.height)
for i in 0..<hCount {
for j in 0..<vCount {
let key = String(format: "%d-%d", i, j)
if let rect = containerLayer.frameDict[key], color = containerLayer.colorDict[key] {
let layer = createExplosionPointLayer(rect, bgColor: color, targetViewSize: containerLayer.targetSize)
// animation
layer.explosionAnimation = self.createAnimationWithType(animationType, position: layer.position, targetViewSize: containerLayer.targetSize)
containerLayer.addSublayer(layer)
layer.beginAnimation()
}
}
}
}
protocol ExplosionAnimationProtocol {
// 粒子初始位置
var oldPosition: CGPoint { set get }
// 粒子最终位置
var newPosition: CGPoint { set get }
// 缩放
var scale: CGFloat { set get }
// 动画时长
var duration: CFTimeInterval { set get }
// 动画重复次数
var repeatCount: Float { set get }
// 生成动画
func animation() -> CAAnimation
// 设置动画完之后的属性
func resetLayerProperty(layer: CALayer)
}
let shakeAnimation = CAKeyframeAnimation(keyPath: "position") ...
// 震动效果
private func shake() {
self.createSemaphore()
// 计算位置,色值
self.caculate()
let shakeAnimation = CAKeyframeAnimation(keyPath: "position")
shakeAnimation.values = [NSValue.init(CGPoint: self.position), NSValue.init(CGPoint: CGPointMake(self.position.x, self.position.y + 1)), NSValue.init(CGPoint: CGPointMake(self.position.x + 1, self.position.y - 1)), NSValue.init(CGPoint: CGPointMake(self.position.x - 1, self.position.y + 1))]
shakeAnimation.duration = 0.2
shakeAnimation.repeatCount = 15
shakeAnimation.delegate = self
shakeAnimation.removedOnCompletion = true
self.targetView?.layer.addAnimation(shakeAnimation, forKey: "shake")
}
override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
// wait for caculate
dispatch_semaphore_wait(self.semaphore!, DISPATCH_TIME_FOREVER)
print("shake animation stop")
// begin explode
if let targetView = self.targetView {
self.parentLayer?.addSublayer(self)
ExplosionHelper.createExplosionPoints(self, targetView: targetView, animationType: self.animationType)
self.targetView?.hidden = true
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有