-
Notifications
You must be signed in to change notification settings - Fork 34
/
UIView+Gank.swift
42 lines (33 loc) · 1.38 KB
/
UIView+Gank.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//
// UIView+Gank.swift
// Gank
//
// Created by 叶帆 on 2017/7/3.
// Copyright © 2017年 Suzhou Coryphaei Information&Technology Co., Ltd. All rights reserved.
//
import UIKit
extension UIView {
public class func loadFromNib(_ nibNamed: String, bundle : Bundle? = nil) -> UIView? {
return UINib(
nibName: nibNamed,
bundle: bundle
).instantiate(withOwner: nil, options: nil)[0] as? UIView
}
public func startShimmering() {
let light = UIColor.white.withAlphaComponent(0.3).cgColor
let dark = UIColor.black.cgColor
let gradient: CAGradientLayer = CAGradientLayer.init(layer: (Any).self)
gradient.colors = [dark, light, dark]
gradient.frame = CGRect(x: -self.bounds.size.width, y: 0, width: 3*self.bounds.size.width, height: self.bounds.size.height)
gradient.startPoint = CGPoint(x: 0.0, y: 0.5)
gradient.endPoint = CGPoint(x: 1.0, y: 0.5)
gradient.locations = [0.0, 0.5, 1.0]
self.layer.mask = gradient
let animation: CABasicAnimation = CABasicAnimation.init(keyPath: "locations")
animation.fromValue = [0.0, 0.1, 0.2]
animation.toValue = [0.8, 0.9, 1.0]
animation.duration = 2
animation.repeatCount = Float.infinity
gradient.add(animation, forKey: "shimmer")
}
}