Swift 方法變動(dòng)(筆記)

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.通知

// 發(fā)通知
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沒(méi)有#define了,但是可以用let,還能用全局函數(shù)

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.協(xié)議代理

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)
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容