1

我的代码中有一个标签、切换按钮和一个动画。当我按下切换按钮 -> 动画开始时,标签会发生变化。下面是我的代码示例。

override func viewDidLayoutSubviews() {
println("viewDidLayoutSubviews is called")
// Initial state of my animation.
     }

 @IBAction func Toggled(sender: AnyObject) {
    CallTextChange() 
   // Two different Animations        
     }

 func CallTextChange() { // Change text here}

每次我更改标签中的文本时都会调用 viewDidLayoutSubviews 。有没有办法在我每次更改标签时停止调用它?

4

3 回答 3

1

我找到了我的问题的答案。

当我们从 Xcode 提供的对象中通过拖放创建 UIImage 时,图像会临时放置在静态放置的位置。因此,在调用 viewDidLayoutSubviews 的中间动画时,图像被放置在静态位置。所以 UIImage 必须以编程方式创建。

CreateImage.image = UIImage(named: "Image.png")
CreateImage = UIImageView(frame: CGRect(x: 0, y: 0, width: CreateImage.image!.size.width/6, height: CreateImage.image!.size.height/6))
self.view.addSubview(CreateImage)
于 2015-08-11T12:05:16.090 回答
0

I think when you change text of and UILabel it will change its intrinsic content size that in turn will notify the system to relayout the view hierarchy. So it seems inevitable that viewDidLayoutSubviews will be called.

Either you put a boolean flag to make your initial animation code execute once only or move your code to other place like viewDidLoad() method.

于 2015-03-25T11:14:03.090 回答
0

尝试在函数中传递一个布尔标志值

于 2015-03-25T11:03:34.400 回答