1.分类
extension UIView
{
/**摇摆*/
func shake()
{ let animation = CABasicAnimation(keyPath: "position")
animation.duration = 0.05
animation.repeatCount = 5
animation.autoreverses = true
animation.fromValue = NSValue(CGPoint: CGPointMake(self.center.x - 4.0, self.center.y))
animation.toValue = NSValue(CGPoint: CGPointMake(self.center.x + 4.0, self.center.y))
layer.addAnimation(animation, forKey: "position") }
}
2.通知
// 发通知
NSNotificationCenter.defaultCenter().postNotificationName("testNotification", object: nil)
// 收通知
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(test), name: "testNotification", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(OneVc.test(_:)), name: "testNotification", object: nil)
//NSNotificationCenter.defaultCenter().addObserver(self, selector: "test", name: "testNotification", object: nil)
3.宏
Swift没有#define了,但是可以用let,还能用全局函数
func RGB(r:CGFloat,g:CGFloat,b:CGFloat) -> UIColor {
return UIColor.init(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: 1)
}
4.协议代理
protocol OneVcDelegate :NSObjectProtocol{
func changeColor(color:UIColor)
}
class OneVc: UIViewController,OneVcDelegate {
override func viewDidLoad()
{
super.viewDidLoad()
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let vc = TwoVc()
vc.delegate = self;
self.navigationController?.pushViewController(vc, animated: true)
}
func changeColor(color:UIColor) {
self.view.backgroundColor = color
}
}
}
class TwoVc: UIViewController {
weak var delegate :OneVcDelegate?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.delegate!.changeColor(UIColor.redColor())
self.navigationController?.popViewControllerAnimated(true)
}
}
网友评论