圆形 UIView 上的 CGAffineTransformMakeScale 动画为正方形
CGAffineTransformMakeScale on circular UIView animate as a square
PopUpMask
是一个循环UIView
。我用它来掩盖菜单。当用户点击按钮时,菜单 "pop" 又变成一个更大的圆圈。当用户关闭它时,菜单会缩小到一个更小的圆圈。当我放大它时动画有效,但当我想缩小它时它会在动画期间转换为正方形。怎么缩小成圆形?
这里是 UIView
缩放:
class PopUpMask: UIView {
override init(frame: CGRect) {
super.init(frame: CGRectMake(0, 0, 40, 40))
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func drawRect(rect: CGRect) {
self.layer.cornerRadius = 20
}
}
这些是调用的方法:
func unmaskToolbarBackground() {
UIView.animateWithDuration(0.3, delay: 0.3, options: .CurveEaseOut, animations: {
self.popUpMask.transform = CGAffineTransformMakeScale(12, 12)
self.toolbarView.maskView? = self.popUpMask
}, completion: nil)
}
func maskToolbarBackground() {
UIView.animateWithDuration(0.4, delay: 0, options: .CurveEaseOut, animations: {
self.popUpMask.transform = CGAffineTransformMakeScale(0.02, 0.02)
self.toolbarView.maskView? = self.popUpMask
}, completion: nil)
}
如果我没看错,你应该修改一下:
override func drawRect(rect: CGRect) {
self.layer.cornerRadius = 20
}
变成这样:
override func drawRect(rect: CGRect) {
self.layer.cornerRadius = 20
self.layer.masksToBounds = true
}
P.S。有点不合时宜:您不再需要使用 self
来访问
"own" Obj-C 方式的方法,除非你有名称冲突。
PopUpMask
是一个循环UIView
。我用它来掩盖菜单。当用户点击按钮时,菜单 "pop" 又变成一个更大的圆圈。当用户关闭它时,菜单会缩小到一个更小的圆圈。当我放大它时动画有效,但当我想缩小它时它会在动画期间转换为正方形。怎么缩小成圆形?
这里是 UIView
缩放:
class PopUpMask: UIView {
override init(frame: CGRect) {
super.init(frame: CGRectMake(0, 0, 40, 40))
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func drawRect(rect: CGRect) {
self.layer.cornerRadius = 20
}
}
这些是调用的方法:
func unmaskToolbarBackground() {
UIView.animateWithDuration(0.3, delay: 0.3, options: .CurveEaseOut, animations: {
self.popUpMask.transform = CGAffineTransformMakeScale(12, 12)
self.toolbarView.maskView? = self.popUpMask
}, completion: nil)
}
func maskToolbarBackground() {
UIView.animateWithDuration(0.4, delay: 0, options: .CurveEaseOut, animations: {
self.popUpMask.transform = CGAffineTransformMakeScale(0.02, 0.02)
self.toolbarView.maskView? = self.popUpMask
}, completion: nil)
}
如果我没看错,你应该修改一下:
override func drawRect(rect: CGRect) {
self.layer.cornerRadius = 20
}
变成这样:
override func drawRect(rect: CGRect) {
self.layer.cornerRadius = 20
self.layer.masksToBounds = true
}
P.S。有点不合时宜:您不再需要使用 self
来访问
"own" Obj-C 方式的方法,除非你有名称冲突。