Last active
November 5, 2018 08:32
-
-
Save caiyue1993/5ef6f50fb0b8ab4ea793509944a56edb to your computer and use it in GitHub Desktop.
Main code of BubblePop button ( Latest Swift 3 version, post link: http://www.jianshu.com/p/f9ac729758e0)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BubblePop: UIButton { | |
var color: UIColor | |
init(color: UIColor) { | |
self.color = color | |
super.init(frame: .zero) | |
layer.shadowOpacity = 0.3 | |
layer.shadowOffset = .zero | |
layer.shadowRadius = 15 | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
override func draw(_ rect: CGRect) { | |
super.draw(rect) | |
let path = UIBezierPath(ovalIn: rect) | |
color.setFill() | |
path.fill() | |
} | |
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { | |
super.touchesBegan(touches, with: event) | |
color = .white | |
setNeedsDisplay() | |
UIView.animate(withDuration: 0.2, animations: { | |
self.transform = CGAffineTransform(scaleX: 0.9, y: 0.9) | |
}) | |
} | |
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { | |
super.touchesEnded(touches, with: event) | |
color = .orange | |
setNeedsDisplay() | |
UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: [], animations: { | |
self.transform = CGAffineTransform.identity | |
}, completion: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment