我没有使用普通按钮,而是将UIControl
因为我需要向它添加渐变。我还有一种方法可以添加阴影和活动指示器(在下图中不可见)作为有状态按钮,以在(例如)进行 API 调用时阻止用户敲击按钮。
尝试UIControl
让它旋转真的很棘手,为了能够做到这一点,我将阴影作为单独的视图添加到包含UIControl
以便可以添加阴影。
现在的问题是控件的行为不像旋转视图 - 让我向您展示一个屏幕抓取的上下文:
这是中间旋转,但肉眼几乎可以看到 -图像显示渐变是图像中蓝色长度的 75% UIView
。
https://github.com/stevencurtis/statefulbutton
为了执行这种旋转,我删除了shadowview
渐变框架,然后将渐变框架的框架更改为其边界,这就是问题所在。
func viewRotated() {
CATransaction.setDisableActions(true)
shadowView!.removeFromSuperview()
shadowView!.frame = self.frame
shadowView!.layer.masksToBounds = false
shadowView!.layer.shadowOffset = CGSize(width: 0, height: 3)
shadowView!.layer.shadowRadius = 3
shadowView!.layer.shadowOpacity = 0.3
shadowView!.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: 20, height: 20)).cgPath
shadowView!.layer.shouldRasterize = true
shadowView!.layer.rasterizationScale = UIScreen.main.scale
self.gradientViewLayer.frame = self.bounds
self.selectedViewLayer.frame = self.bounds
CATransaction.commit()
self.insertSubview(shadowView!, at: 0)
}
所以这个旋转方法是通过父视图控制器调用的:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
coordinator.animate(alongsideTransition: { context in
context.viewController(forKey: UITransitionContextViewControllerKey.from)
//inform the loginButton that it is being rotated
self.loginButton.viewRotated()
}, completion: { context in
// can call here when completed the transition
})
}
我知道这就是问题所在,而且我想这不是在完全正确的时间以与UIView
. 现在的问题是我已经尝试了很多方法来让它发挥作用,而我最好的解决方案(上图)并不完全存在。
建议使用UIButton
, 来使用渐变图像(请不要建议使用渐变图像作为 UIButton 的背景,我已经尝试过)或第三方库是没有帮助的。这是我的工作,它可以工作,但对我来说不能接受,我想让它像通常的视图一样工作(或者至少知道为什么不工作)。我也尝试过上面的其他解决方案,并选择了我自己的UIControl
. 我知道如果有 API 调用,我可以锁定视图,或者使用其他方式来阻止用户多次按下按钮。我正在尝试修复我的解决方案,而不是发明解决此问题的方法CAGradientLayer
。
问题:我需要使a 作为背景以UIControlView
与 aCAGradientLayer
相同的方式旋转UIView
,而不是出现上图所示的问题。