使用固定中心动画UILabel宽度

时间:2015-09-25 07:37:32

标签: swift uiview uilabel uianimation

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

目前,当我这样做时,它并没有从中心发展。

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 个答案:

答案 0 :(得分:2)

这对你有用。 你只需要给出宽度和高度的比例(默认为1,这将没有效果)。这将激活你视图的框架。

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

如果你不想延伸内容,那么你可以试试这个:

        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)