Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 使用固定中心设置UILabel宽度的动画_Swift_Uiview_Uilabel_Uianimation - Fatal编程技术网

Swift 使用固定中心设置UILabel宽度的动画

Swift 使用固定中心设置UILabel宽度的动画,swift,uiview,uilabel,uianimation,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.te

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

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

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,这将不起作用)。这将为视图的帧设置动画

    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)

@埃里克。对不起,这里是objective-c代码。用Swift代码片段更新了我的答案。谢谢@EricD。我不想拉伸内容,特别是如果我有文本在里面。有什么办法可以改变宽度吗?@bvalellunga希望这对你有所帮助@如果合适的话,请接受这个答案。它可以帮助其他人选择最好的一个。但是,在Obj C中,您不能将某些内容分配给结构的某个部分。分配给
size
将给您一个错误。那么在Obj C中你是如何做到这一点的呢?