【问题标题】:button.setTitle blanks out image viewbutton.setTitle 空白图像视图
【发布时间】:2015-05-27 22:48:29
【问题描述】:

谁能告诉我为什么设置 button.setTitle(string, forState) 函数会使 UIImage 空白?

如果我注释掉使用 button.setTitle 的两行,动画运行良好。当线条存在时,当我按下按钮时动画消失。

var cryButton: UIButton 和函数 UpdateImage 引用同一个按钮,但我尝试添加第二个按钮并引用其中的标题,它具有相同的效果。它使图像空白。

我被难住了:)

import UIKit

class ViewController: UIViewController {

    var counter = 1

    var numImages = 8

    var toggle = true

    var timer = NSTimer()


    @IBOutlet weak var babyImage: UIImageView!

    @IBOutlet weak var cryButton: UIButton!

    @IBAction func UpdateImage(sender: AnyObject) {

        if toggle == true {
            timer = NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: Selector("doanimation"), userInfo: nil, repeats: true)
            cryButton.setTitle("Stop crying", forState: UIControlState.Normal)
            toggle = false

        } else {
            timer.invalidate()
            babyImage.image = UIImage(named: "WaWa1.png")
            cryButton.setTitle("Wa-Waaaaa!", forState: UIControlState.Normal)
            toggle = true
        }

    }

    func doanimation() {

        babyImage.image = UIImage(named: "WaWa\(counter).png")

        if (counter == (numImages)) {
            counter = 1
        } else {
            ++counter
        }
    }


    override func viewDidLayoutSubviews() {
        babyImage.center = CGPointMake(babyImage.center.x - 400, babyImage.center.y)
    }

    override func viewDidAppear(animated: Bool) {
        UIView.animateWithDuration(2, animations: { () -> Void in
            self.babyImage.center = CGPointMake(self.babyImage.center.x + 400, self.babyImage.center.y)
        })

    }


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

【问题讨论】:

    标签: ios swift uiview uiimage


    【解决方案1】:

    我终于找到问题了。不是定时器引起的,定时器完全没问题。问题是由设置按钮标题时调用viewDidLayoutSubviews 引起的。您正在使用自动布局,并且您正在使用按钮的内在内容大小,对吗?而且由于两个标题的长度不同,所以当你设置标题时,按钮的框架会改变,因此会再次布局,然后调用viewDidLayoutSubviews。并且您在viewDidLayoutSubviews 中设置imageView 的中心值,所以基本上当您设置标题时,图像将向左移动400 点到它的原始位置,我猜它在屏幕之外。请注意:如果图像大小不同,当您调用imageView.image = UIImage(named: "image.png") 时也会调用viewDidLayoutSubviews。我认为您使用的所有图像都具有相同的大小,这就是为什么当计时器运行时viewDidLayoutSubviews 没有被调用。这是一个很好的answer,它解释了何时调用viewDidLayoutSubviews。我会让你自己找出正确的解决方案,好吗? :)

    【讨论】:

    • 哇!这是一种教育,我很感激。我在 viewDidLayoutSubviews 中注释掉了代码,现在按钮和图像工作正常。无论如何,真的不需要做那个初始动画。
    【解决方案2】:

    实际上,有一个更好的方法可以让UIImageView 带有一组图像,你可以试试这个并删除计时器。

    babyImage.animationImages = @[image1,image2,image3,image4,image5,image6,image7,image8]
    babyImage.animationDuration = 1.6 
    // There are 8 images, so each image occupies 1.6 / 8 = 0.2 secs    
    babyImage.startAnimating()
    // Use babyImage.stopAnimating() to stop the animation
    

    【讨论】:

    • 感谢埃里克。我将尝试替换该代码,看看是否仍然存在图像消隐问题。您认为问题出在计时器上,还是我正在更改 button.title 而不是 button.image?
    • @Bendrix 你必须使用 button.titlelabel 属性而不是 button.title 属性。
    • 嗨 Rahul,button.setTitle 是一个函数,我正在尝试更改按钮的标题。 button.tltleLabel 是一个变量。我不认为我可以从快速程序行中设置它。我想我可以得到它,但不能设置它。当我输入 button.titleLabel = "text" 行时,编译器说不能分配给这个表达式的结果。你有什么建议?
    猜你喜欢
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    相关资源
    最近更新 更多