iView动画在iOS 8中无法正常工作?
我想创建如下动画,我是在iOS 11.2中完成的。iView动画在iOS 8中无法正常工作?,ios,swift,animation,uiview,autolayout,Ios,Swift,Animation,Uiview,Autolayout,我想创建如下动画,我是在iOS 11.2中完成的。 我在iOS 8.4和9.2中对此进行了测试-break 故事板 我用的是autoLaout 圆形阴影按钮 我将UIButton子类化。添加了圆角和DropShadow协议。它包含一个关心动画的animateButton函数 class RoundedShadowButton: UIButton, RoundedCorner, DropShadow { var originalSize: CGRect? func anim
我在iOS 8.4和9.2中对此进行了测试-
break
故事板 我用的是autoLaout 圆形阴影按钮 我将UIButton子类化。添加了圆角和DropShadow协议。它包含一个关心动画的animateButton函数
class RoundedShadowButton: UIButton, RoundedCorner, DropShadow {
var originalSize: CGRect?
func animateButton(shouldLoad: Bool, withMessage message: String?) {
let spinner = UIActivityIndicatorView()
spinner.activityIndicatorViewStyle = .whiteLarge
spinner.color = .darkGray
spinner.alpha = 0.0
spinner.hidesWhenStopped = true
spinner.tag = 21
if shouldLoad {
self.addSubview(spinner)
self.setTitle("", for: .normal)
UIView.animate(withDuration: 0.2, animations: {
self.setRoundedCorners(radius: self.frame.height / 2)
self.frame = CGRect(x: self.frame.midX - (self.frame.height / 2), y: self.frame.origin.y, width: self.frame.height, height: self.frame.height)
}, completion: { (finished) in
if finished {
spinner.startAnimating()
spinner.center = CGPoint(x: self.frame.width / 2 + 1, y: self.frame.width / 2 + 1)
spinner.fadeTo(alphaValue: 1.0, withDuration: 0.2)
}
})
self.isUserInteractionEnabled = false
} else {
self.isUserInteractionEnabled = true
// remove spinner
for subView in self.subviews {
if subView.tag == 21 {
subView.removeFromSuperview()
}
}
// return back to original button
UIView.animate(withDuration: 0.2, animations: {
if let size = self.originalSize {
self.setRoundedCorners(radius: 8)
self.frame = size
self.setTitle(message, for: .normal)
}
})
}
}
}
圆角
滤镜
HomeVC
在HomeVC的ViewDidDisplay中,我设置了角半径、阴影和帧大小。在buttons iAction中,我调用了animate函数
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
actionBtn.originalSize = actionBtn.frame
actionBtn.setRoundedCorners(radius: 8)
actionBtn.setShadow(opacity: 0.3, radius: 10.0, color: .darkGray)
}
@IBAction func actionBtnTapped(_ sender: Any) {
self.actionBtn.animateButton(shouldLoad: true, withMessage: nil)
}
如何解决此问题?当使用约束设置动画时,您还需要使用约束。虽然它有时可能奏效,但它不是你可以依赖的东西。你可以这样解决它
- 删除
和前导
约束尾随
- 添加宽度约束,使其与
减去superview
的宽度相同。将其连接到名为40
的类型为widthConstraint
的类变量。设置优先级NSLayoutConstraint
750
- 添加另一个宽度约束,该约束将
设置为与height相同,使其成为一个圆。将优先级设置为width
500
- 将约束添加到
中的superview
center
self.frame = CGRect(x: self.frame.midX - (self.frame.height / 2), y: self.frame.origin.y, width: self.frame.height, height: self.frame.height)
用这个。通过更改优先级,您可以选择应生效的约束。由于宽约束为750,它将战胜窄约束。将优先级更改为250时,狭义约束将获胜
self.widthConstraint.priority = 250
self.layoutIfNeeded()
使用约束设置动画时,还需要使用约束。虽然它有时可能奏效,但它不是你可以依赖的东西。你可以这样解决它
- 删除
和前导
约束尾随
- 添加宽度约束,使其与
减去superview
的宽度相同。将其连接到名为40
的类型为widthConstraint
的类变量。设置优先级NSLayoutConstraint
750
- 添加另一个宽度约束,该约束将
设置为与height相同,使其成为一个圆。将优先级设置为width
500
- 将约束添加到
中的superview
center
self.frame = CGRect(x: self.frame.midX - (self.frame.height / 2), y: self.frame.origin.y, width: self.frame.height, height: self.frame.height)
用这个。通过更改优先级,您可以选择应生效的约束。由于宽约束为750,它将战胜窄约束。将优先级更改为250时,狭义约束将获胜
self.widthConstraint.priority = 250
self.layoutIfNeeded()
尝试此项功能在我的终端工作只需禁用此功能:
self.view.translatesAutoresizingMaskIntoConstraints = false
尝试此项功能在我的终端工作只需禁用此功能:
self.view.translatesAutoresizingMaskIntoConstraints = false
最有可能的是,您的前导/尾随约束会阻止它调整大小。尽管它起了作用,但它不是你可以依赖的东西。使用约束时,您还需要使用约束调整项目的大小。@Sh_worked@LGP我把优先级改为999,没有帮助。你有什么想法请看下面我的解决方案。只需检查它,不带任何约束,并手动处理(意味着删除所有约束)。它很可能会起作用,因为前导/尾随约束阻止它调整大小。尽管它起了作用,但它不是你可以依赖的东西。使用约束时,您还需要使用约束调整项目的大小。@Sh_worked@LGP我把优先级改为999,没有帮助。你有什么想法请看下面我的解决方案。只需检查它,不带任何约束,并手动处理(意味着删除所有约束)。它将在我创建宽度约束的位置工作?我在故事板中创建了它,但是,如何更改它的常量值呢?因为RoundedShadowButton类中的动画函数为Yes,所以可以像序列图像板中的任何其他约束一样创建它。如果双击约束,您将获得该约束的属性页,在那里您可以将其连接到变量。但是HomeVC中的变量,如何在RoundedShadowButton类中访问它将其作为参数传递到
animateButton
或(可能更好)将其设置为RoundedShadowButton
对象上的弱属性。展开?不知道怎么会这样。是否删除了按钮的前导/尾随约束?在何处创建宽度约束?我在故事板中创建了它,但是,如何更改它的常量值呢?因为RoundedShadowButton类中的动画函数为Yes,所以可以像序列图像板中的任何其他约束一样创建它。如果双击约束,您将获得该约束的属性页,在那里您可以将其连接到变量。但是HomeVC中的变量,如何在RoundedShadowButton类中访问它将其作为参数传递到animateButton
或(可能更好)将其设置为RoundedShadowButton
对象上的弱属性。展开?不知道怎么会这样。是否删除了按钮的前导/尾随约束?