ios - 使用 Switch 语句从 Xcassets 加载 imageView

标签 ios swift uiimageview switch-statement

我在 Xcassets 中有 @1 @2 和 @3 的图像,并尝试使用下面的代码将图像加载到 ScrollView 页面上,该代码根据图像的年龄来切换图像。由位置决定页面,并在viewWillLoad函数中调用switch语句。图像未加载,但声音正常,所以我知道问题出在图像加载上。你能帮忙吗?

override func viewDidDisappear(animated: Bool) {
    self.imageView.removeFromSuperview()
    self.imageView.image = nil
}



override func viewWillAppear(animated: Bool) {

    showImageView()

    let tapGestureImage = UITapGestureRecognizer(target: self, action: Selector("handleTapGestureImage:"))
    imageView.addGestureRecognizer(tapGestureImage)
    imageView.userInteractionEnabled = true
}




// MARK: BWWalkthroughPage Implementation

func walkthroughDidScroll(position: CGFloat, offset: CGFloat) {

    // getting the page number from the scroll position. First page loads as nil so want it to be zero.

    screenPage  = Int(((position / view.bounds.size.width) + 1) - 1)
}



func showImageView() {

    // change imageView in bundle depending on which scrollview page you are.
    switch screenPage {

    case 0:

        self.imageView.image = UIImage(named: "Aligator", inBundle: NSBundle.mainBundle(), compatibleWithTraitCollection: self.traitCollection)
        self.view.addSubview(imageView)

    case 1:

        self.imageView.image = UIImage(named: "Bear", inBundle: NSBundle.mainBundle(), compatibleWithTraitCollection: self.traitCollection)
        self.view.addSubview(imageView)

    default:
        self.imageView.image = nil
    }
}

最佳答案

您需要了解一些重要事项。 您不需要一遍又一遍地从 View 层次结构中添加/删除 ImageView 。只需调用 imageView.image = nil 就足够了。

第二件事是您不需要使用完整方法 UIImage(named:inBundle:compatibleWithTraitCollection:)。您应该能够简单地使用 UIImage(named:)。如果您发现自己不得不使用前者,那么在您职业生涯的这个阶段,您可能会承受更大的负担。

第三件事是,当你覆盖一个方法时,99% 的情况下你必须调用 super。

这是一个应该有效的例子:

import UIKit

class MyViewController: UIViewController {

    override fun viewDidLoad() {
        super.viewDidLoad()
        let gestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("handleTapGestureImage:"))
        imageView.addGestureRecognizer(gestureRecognizer)
        imageView.userInteractionEnabled = true
    }

    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated: animated)
        self.imageView.image = nil
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated: animated)
        showImageView()
    }

    func walkthroughDidScroll(position: CGFloat, offset: CGFloat) {
        screenPage  = Int(((position / view.bounds.size.width) + 1) - 1)
    }

    func showImageView() {
        switch screenPage {
        case 0:
            imageView.image = UIImage(named: "Aligator")
        case 1:
            imageView.image = UIImage(named: "Bear")
        default:
            imageView.image = nil
        }
    }
}

关于ios - 使用 Switch 语句从 Xcassets 加载 imageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31306038/

相关文章:

ios - 对多关系作为 sectionNameKeyPath

ios - 如何在 Swift 中使用 SKPSMTPMessageDelegate 协议(protocol)?

swift - 将 AWS key 保存在 github 之外

ios - 如何获取 AFNetworking 的 UIImageView 类别存储的缓存图像?

objective-c - 将 UIImageView 添加到自定义 UITableViewCell

ios - 当应用程序进入后台时,约束会重置 - iOS 13

ios - 如何快速创建依赖的通用协议(protocol)

ios - 嵌套容器 nil 值自定义解码器 Swift

ios - 如何在 didSelectRowAtIndexPath 函数之外更改标签的颜色?

ios - 如何以编程方式调整 UIImage 的大小,就像设置scaleAspectFill 内容模式一样?