import UIKit
class ButtonViewController: UIViewController {
var flatButton = FlatButton.button()
var errorLabel = UILabel()
var activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
override func viewDidLoad() {
super.viewDidLoad()
addButton()
addLabel()
addActivityIndicatorView()
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
errorLabel.layer.transform = CATransform3DMakeScale(0.5, 0.5, 1)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
添加按钮
*/
func addButton(){
flatButton.backgroundColor = UIColor(red: 52/255.0, green: 152/255.0, blue: 219/255.0, alpha: 1.0)
flatButton.setTitle("Log in", forState: UIControlState.Normal)
flatButton.setTranslatesAutoresizingMaskIntoConstraints(false)
flatButton.addTarget(self, action: "touchUpInside:", forControlEvents: UIControlEvents.TouchUpInside)
view.addSubview(flatButton)
self.view.addConstraint(NSLayoutConstraint(item: flatButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0.0))
self.view.addConstraint(NSLayoutConstraint(item: flatButton, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0.0))
}
添加标签
*/
func addLabel(){
errorLabel.font = UIFont(name: "Avenir-Light", size: 18)
errorLabel.textColor = UIColor(red: 231/255.0, green: 76/255.0, blue: 60/255.0, alpha: 1.0)
errorLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
errorLabel.text = "Just a serious login error."
errorLabel.textAlignment = NSTextAlignment.Center
view.insertSubview(errorLabel, belowSubview: flatButton)
self.view.addConstraint(NSLayoutConstraint(item: errorLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: flatButton, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0.0))
self.view.addConstraint(NSLayoutConstraint(item: errorLabel, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: flatButton, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0.0))
errorLabel.layer.transform = CATransform3DMakeScale(0.5, 0.5, 1)
}
添加活动指示图
*/
func addActivityIndicatorView(){
activityIndicatorView.hidesWhenStopped = true
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: activityIndicatorView)
}
按钮点击时
:param: button 按钮
*/
func touchUpInside(button: FlatButton){
button.userInteractionEnabled = false
activityIndicatorView.startAnimating()
hiddenErrorLabel()
let delayTime = dispatch_time(DISPATCH_TIME_NOW,
Int64(1 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
self.shakeFlatButton()
self.showErrorLabel()
self.activityIndicatorView.stopAnimating()
}
}
摇晃按钮
*/
func shakeFlatButton(){
var shakeSpringAnimation = POPSpringAnimation(propertyNamed: kPOPLayerPositionX)
shakeSpringAnimation.velocity = NSNumber(float: 2000.0)
shakeSpringAnimation.springBounciness = 20.0
shakeSpringAnimation.completionBlock = {(animation, finished) in
self.flatButton.userInteractionEnabled = true
}
flatButton.layer.pop_addAnimation(shakeSpringAnimation, forKey: "shakeSpringAnimation")
}
显示错误提示标签
*/
func showErrorLabel(){
var scaleSpringAnimation = POPSpringAnimation(propertyNamed: kPOPLayerScaleXY)
scaleSpringAnimation.toValue = NSValue(CGPoint: CGPointMake(1, 1))
scaleSpringAnimation.velocity = NSValue(CGPoint: CGPointMake(3, 3))
scaleSpringAnimation.springBounciness = 20.0
errorLabel.layer.pop_addAnimation(scaleSpringAnimation, forKey: "scaleSpringAnimation")
var positionSpringAnimation = POPSpringAnimation(propertyNamed: kPOPLayerPositionY)
positionSpringAnimation.toValue = NSNumber(float: Float(flatButton.layer.position.y + flatButton.intrinsicContentSize().height))
positionSpringAnimation.springBounciness = 20.0
errorLabel.layer.pop_addAnimation(positionSpringAnimation, forKey: "positionSpringAnimation")
}
隐藏错误提示标签
*/
func hiddenErrorLabel(){
var scaleSpringAnimation = POPSpringAnimation(propertyNamed: kPOPLayerScaleXY)
scaleSpringAnimation.toValue = NSValue(CGPoint: CGPointMake(0.5, 0.5))
scaleSpringAnimation.velocity = NSValue(CGPoint: CGPointMake(3, 3))
scaleSpringAnimation.springBounciness = 20.0
errorLabel.layer.pop_addAnimation(scaleSpringAnimation, forKey: "scaleSpringAnimation")
var positionSpringAnimation = POPSpringAnimation(propertyNamed: kPOPLayerPositionY)
positionSpringAnimation.toValue = NSNumber(float: Float(flatButton.layer.position.y))
positionSpringAnimation.springBounciness = 20.0
errorLabel.layer.pop_addAnimation(positionSpringAnimation, forKey: "positionSpringAnimation")
}
}