gpt4 book ai didi

swift - 固定中心的动画 UILabel 宽度

转载 作者:可可西里 更新时间:2023-11-01 01:38:56 24 4
gpt4 key购买 nike

如何在保持 View 居中的同时为宽度的变化设置动画?

目前,当我这样做时,它不会从中心生长。

self.textLabel = UILabel()
self.textLabel.frame = CGRectMake(0, 0, Globals.voterTextLabel, Globals.voterTextLabel)
self.textLabel.center = CGPointMake(self.view.frame.width/2, self.view.frame.height/2)
self.textLabel.text = "VS"
self.textLabel.layer.cornerRadius = Globals.voterTextLabel/2
self.textLabel.layer.masksToBounds = true
self.textLabel.clipsToBounds = true
self.textLabel.backgroundColor = UIColor.whiteColor()
self.textLabel.textColor = Colors.voterTextLabel
self.textLabel.textAlignment = .Center
self.textLabel.autoresizingMask = .FlexibleWidth
self.view.addSubview(self.textLabel)

UIView.animateWithDuration(2, animations: {
self.textLabel.frame.size.width = 150
self.textLabel.center.x = self.view.frame.width/2
}, completion: nil)

最佳答案

这对你有用。你只需要给出宽度和高度的比例(默认为 1,这不会有任何影响)。这将为您的 View 框架设置动画。

    UIView.animateWithDuration(2, delay: 0, options: UIViewAnimationOptions.BeginFromCurrentState, animations: { () -> Void in
self.textLabel.transform=CGAffineTransformMakeScale(3, 1);
}, completion: nil)

如果你不想拉伸(stretch)内容,你可以试试这个:

        UIView.animateWithDuration(2, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in

var newWidth:CGFloat = 200.0;

var oldWidth:CGFloat = self.textLabel.frame.width;

var totalChangeWidth:CGFloat = newWidth - oldWidth;

var newHeight:CGFloat = 200.0;

var oldHeight:CGFloat = self.textLabel.frame.width;

var totalChangeHeight:CGFloat = newHeight - oldHeight;

self.textLabel.bounds.size = CGSizeMake(self.textLabel.frame.size.width + totalChangeWidth, self.textLabel.frame.size.height + totalChangeHeight);

self.textLabel.center = CGPointMake(self.view.frame.width/2, self.view.frame.height/2)

}, completion:nil)

关于swift - 固定中心的动画 UILabel 宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32777134/

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