10W8K/swifty

MUIButton

10W8K opened this issue · 1 comments

MUIButton

  • 背景渐变
let gradientLayer:CAGradientLayer = CAGradientLayer()
...

self.gradientLayer.colors = [
            UIColor(hex:0xff0000).CGColor,
            UIColor(hex:0xff6600).CGColor,
            UIColor(hex:0xff0000).CGColor
        ]

self.gradientLayer.startPoint = CGPointMake(0.0, 0.0);
self.gradientLayer.endPoint = CGPointMake(0.0, 2.0);

self.gradientLayer.removeFromSuperlayer()
self.layer.insertSublayer(self.gradientLayer,atIndex:0)

通过CAGradientLayer创建渐变层,并作为子层的形式插入到UIButton的layer下
这里使用insertSublayer(self.gradientLayer,atIndex:0)
不要使用addSublayer, 可能是因为使用addSublayer,会使index增长而覆盖掉title


给按钮设置渐变层的时候发生与按钮偏移的现象
解决方法是

self.gradientLayer.anchorPoint = CGPointMake(0.0, 0.0);