如果按下按钮,我希望通过从左到右缩放来显示UIView。如果我再次按下按钮,视图应该从右向左缩放。
我找到了从左到右的解释,但即使这个解决方案只能工作一次。如果我隐藏视图并再次播放动画,它会再次从中心缩放。 从右到左缩放它也不起作用,因为它在没有任何动画的情况下消失。
到目前为止,这是我的代码:
if(!addQuestionaryView.isHidden)
{
//Reset input if view is hidden
addQuestionaryInput.text = ""
self.addQuestionaryView.isHidden = false
let frame = addQuestionaryView.frame;
let rect = CGRect(x: frame.origin.x, y: frame.origin.y, width: frame.width, height: frame.height)
let leftCenter = CGPoint(x: rect.minX, y: rect.minY)
addQuestionaryView.layer.anchorPoint = CGPoint(x:1,y: 0.5)
addQuestionaryView.layer.position = leftCenter
UIView.animate(withDuration: 0.6,
animations: {
self.addQuestionaryView.transform = CGAffineTransform(scaleX: 0, y: 1)
},
completion: { _ in
self.addQuestionaryView.isHidden = true
})
}
else
{
self.addQuestionaryView.isHidden = false
let frame = addQuestionaryView.frame;
let rect = CGRect(x: frame.origin.x, y: frame.origin.y, width: frame.width, height: frame.height)
let leftCenter = CGPoint(x: rect.minX, y: rect.midY)
addQuestionaryView.layer.anchorPoint = CGPoint(x:0,y: 0.5);
addQuestionaryView.layer.position = leftCenter
addQuestionaryView.transform = CGAffineTransform(scaleX: 0, y: 1)
UIView.animate(withDuration: 0.6,
animations: {
self.addQuestionaryView.transform = CGAffineTransform(scaleX: 1, y: 1)
},
completion: nil)
}
如上所述,出现的部分工作一次然后再从中心开始。消失的部分根本不起作用。
答案 0 :(得分:1)
我不知道是什么"要扩展它,所以它有点看起来像Android揭示动画"意味着(我希望我永远不会这样做;我从来没有见过Android手机,我从不希望看到它)。
但你的意思是这样吗?这是通过在视图前面设置 mask 的动画来完成的,从而显示然后隐藏它:
以下是该示例中使用的代码:
class MyMask : UIView {
override func draw(_ rect: CGRect) {
let r = CGRect(x: 0, y: 0, width: rect.width/2.0, height: rect.height)
UIColor.black.setFill()
UIGraphicsGetCurrentContext()?.fill(r)
}
}
class ViewController: UIViewController {
@IBOutlet weak var lab: UILabel!
var labMaskOrigin = CGPoint.zero
var didAddMask = false
override func viewDidLayoutSubviews() {
if didAddMask {return}
didAddMask = true
let mask = MyMask()
mask.isOpaque = false
let w = self.lab.bounds.width
let h = self.lab.bounds.height
mask.frame = CGRect(x: -w, y: 0, width: w*2, height: h)
self.lab.mask = mask
self.labMaskOrigin = mask.frame.origin
}
var labVisible = false
func toggleLabVisibility() {
labVisible = !labVisible
UIView.animate(withDuration: 5) {
self.lab.mask!.frame.origin = self.labVisible ?
.zero : self.labMaskOrigin
}
}
}
答案 1 :(得分:0)
删除它:
addQuestionaryView.layer.anchorPoint = CGPoint(x:0,y: 0.5);
addQuestionaryView.layer.position = leftCenter
addQuestionaryView.transform = CGAffineTransform(scaleX: 0, y: 1)
然后在带有持续时间段的动画中使用下面的代码而不是CGAfflineTransform
addQuestionaryView.frame = CGRect(0,0,500,100)
将500宽度替换为您想要的任何宽度。
Ans将其关闭备份:
addQuestionaryView.frame = CGRect(0,0,0,100);