gpt4 book ai didi

ios - 如何从左到右和向后缩放 UIView?

转载 作者:行者123 更新时间:2023-11-28 15:55:55 24 4
gpt4 key购买 nike

如果我点击一个按钮,我希望通过从左向右缩放来显示一个 UIView。如果我再次点击按钮, View 应该从右向左缩小。

我找到了如何从左到右执行此操作的说明,但即使是此解决方案也只能使用一次。如果我隐藏 View 并再次播放动画,它会再次从中心缩放它。从右向左缩放它也不起作用,因为它会立即消失而没有任何动画。

到目前为止,这是我的代码:

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)
}

如前所述,出现的部分工作一次,然后再次从中心开始。消失部分根本不起作用。

最佳答案

我不知道“缩放它,使其看起来有点像 Android 显示动画”是什么意思(我希望我永远不会这样做;我从未见过 Android 手机,我从不希望看到一部)。

但是你的意思是这样的吗?这是通过为 View 前面的 mask 设置动画来完成的,从而显示然后隐藏它:

enter image description here

这是该示例中使用的代码:

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
}
}
}

关于ios - 如何从左到右和向后缩放 UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41748077/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com