使用Swift将UIView的坐标移动到LLO

时间:2014-07-19 22:36:55

标签: ios uiview swift cgaffinetransform

我希望这段代码能够将我的newLabel从左上角(ULO)移动到左下角(LLO),但事实并非如此!显然我需要有人在UIView设置(0,0)到LLO的过程中牵手。我已经浏览了文档,并在此论坛上发帖后阅读帖子,但似乎无法实现。免责声明:我试图在xCode 6 beta 2的游乐场中完成此操作,因此它可能是一个错误或不受支持。

var newLabel = UILabel(frame: CGRectMake(0, 0, 150, 50))
newLabel.text = "This is a test"
newLabel.layer.borderWidth = 2
newLabel.textAlignment = NSTextAlignment.Center
newLabel.backgroundColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.8)

var myView = UIView(frame: CGRectMake(0, 0, 200, 100))
myView.layer.borderWidth = 2
myView.addSubview(newLabel)
myView.transform = CGAffineTransformMakeScale(1, -1)
myView.backgroundColor = UIColor(red: 0.8, green: 0, blue: 0, alpha: 0.8)

enter image description here

2 个答案:

答案 0 :(得分:1)

好像你没有在主视图中添加Myview尝试此代码: -

     var newLabel = UILabel(frame: CGRectMake(0, 0, 150, 50))
    newLabel.text = "This is a test"
    newLabel.layer.borderWidth = 2
    newLabel.textAlignment = NSTextAlignment.Center
    newLabel.backgroundColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.8)

    var myView = UIView(frame: CGRectMake(0, 0, 200, 100))
    myView.layer.borderWidth = 2
    myView.addSubview(newLabel)
    myView.transform = CGAffineTransformMakeScale(1, -1)
    myView.backgroundColor = UIColor(red: 0.8, green: 0, blue: 0, alpha: 0.8)
    self.view.addSubview(myView);

答案 1 :(得分:0)

要翻转UIView,它必须是另一个视图的子视图...我的问题是我试图翻转显然无法完成的mainView或baseView。这样就可以了,因为它需要一个能够绘制的图形空间。以下是实现我所寻找的代码。同样不是我也必须“翻转”我的标签以使其恢复到直立状态(这也是合乎逻辑的)。

var newLabel = UILabel(frame: CGRectMake(0, 0, 150, 50))
newLabel.transform = CGAffineTransformMakeScale(1, -1)
newLabel.text = "This is a test"
newLabel.layer.borderWidth = 2
newLabel.textAlignment = NSTextAlignment.Center
newLabel.backgroundColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.8)

var subView = UIView(frame: CGRectMake(50, 50, 200, 100))
subView.transform = CGAffineTransformMakeScale(1, -1)
subView.layer.borderWidth = 2
subView.addSubview(newLabel)
subView.backgroundColor = UIColor(red: 0.8, green: 0, blue: 0, alpha: 0.8)

var myView = UIView()
myView = UIView(frame: CGRectMake(0, 0, 300, 200))
//myView.transform = CGAffineTransformMakeScale(1, -1)
myView.layer.borderWidth = 2
myView.addSubview(subView)
myView.backgroundColor = UIColor(red: 0, green: 0.8, blue: 0, alpha: 0.8)

enter image description here

感谢@Mohit帮助我找到解决方案。