ios - 如何将自定义 View 动画化为自定义 View

标签 ios swift uiview koloda

我正在构建类似抽认卡的应用程序,并且正在使用库 KolodaView:https://github.com/Yalantis/Koloda

我有这个扩展,它返回一个 UIView 作为抽认卡的前面:

extension StudyViewController: KolodaViewDataSource {

func kolodaNumberOfCards(koloda:KolodaView) -> UInt {
    return UInt(memories.count)
}

func koloda(koloda: KolodaView, viewForCardAtIndex index: UInt) -> UIView {
    return UIImageView(image: memories[Int(index)].frontImage)
}

func koloda(koloda: KolodaView, viewForCardOverlayAtIndex index: UInt) -> OverlayView? {

    return NSBundle.mainBundle().loadNibNamed("OverlayView",owner: self, options: nil)[0] as? OverlayView
}
}

我希望能够通过点击将前 View 转换为后 View :

func koloda(koloda: KolodaView, didSelectCardAtIndex index: UInt) {

    let frontView: UIView = koloda as KolodaView
    let backView: UIView = UIImageView(image: memories[Int(index)].backImage)

    if showingFront == true {
        UIView.transitionFromView(frontView, toView: backView, duration: 1, options: UIViewAnimationOptions.TransitionCrossDissolve, completion: nil)
    } else {
        UIView.transitionFromView(backView, toView: frontView, duration: 1, options: UIViewAnimationOptions.TransitionCrossDissolve, completion: nil)
        showingFront = false
    }
}

转换成功,但完成后,flashcard 将从一个 KolodaView 变成一个巨大的 UIView

如何将 KolodaView 转换为另一个 KolodaView

最佳答案

我没有使用过KolodaView,所以我的回答对手头的问题更通用。

两种方法

一个。您当前方法的变体(Hackish 方式)

由于库不公开对显示的 imageView 的引用,您首先需要维护内部的 imageView 集以备后用。

private var myImageViews: [Int: UIImageView] = [:]
func koloda(koloda: KolodaView, viewForCardAtIndex index: UInt) -> UIView {
    let imageView = UIImageView(image: memories[Int(index)].frontImage)
    myImageViews[Int(index)] = imageView
    return imageView
}

使用您当前的方法对 View 进行动画处理,并使用完成 block 更新原始图像的最终状态。

UIView.transitionFromView(frontView,
                          toView: backView,
                          duration: duration,
                          options: [.TransitionCrossDissolve, .ShowHideTransitionViews],
                          completion: { [weak self] (finished: Bool) in
        let imageView = myImageViews[Int(index)]
        imageView.image = memories[Int(index)].frontImage
        frontView.hidden = false
        backView.hidden = true
        backView.removeFromSuperview()
})

b。在数据源方法中返回自定义 View ,并在 didSelectCardAtIndex 中切换其内部状态

private var myCustomViews: [Int: MyCustomView] = [:]
func koloda(koloda: KolodaView, viewForCardAtIndex index: UInt) -> UIView {
    let customView = MyCustomView(frontImage: memories[Int(index)].frontImage, 
                                  backImage: memories[Int(index)].backImage)
    myCustomViews[Int(index)] = customView
    return customView
}

func koloda(koloda: KolodaView, didSelectCardAtIndex index: UInt) {
    let customView = myCustomViews[Int(index)]
    customView.toggleFrontBackState()
}

在这种情况下,toggleFrontBackState 的实现将包含初始实现的代码(customView.showingFront、transitionFromView)。如果您需要更多说明,请告诉我。

编辑

class MyCustomView: UIView {
    private(set) lazy var frontView: UIImageView = self.makeFrontView()
    private(set) lazy var backView: UIImageView = self.makeBackView()
    var showingFront: Bool = false

    init(frontImage: UIImage?, backImage: UIImage?){
        super.init(frame: CGRectZero)
        frontView.image = frontImage
        backView.image = backImage
        backView.hidden = true
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func toggleFrontBackState() {
        if showingFront {
            UIView.transitionFromView(frontView, toView: backView, duration: 1, 
                                      options: [.TransitionCrossDissolve, .ShowHideTransitionViews], completion: nil)
        } else {
            UIView.transitionFromView(backView, toView: frontView, duration: 1, 
                                      options: [.TransitionCrossDissolve, .ShowHideTransitionViews] , completion: nil)
        }
        showingFront = !showingFront
    }

    func makeFrontView() -> UIImageView {
        return UIImageView()
    }

    func makeBackView() -> UIImageView {
        return UIImageView()
    }
}

关于ios - 如何将自定义 View 动画化为自定义 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38849595/

相关文章:

ios - 快速限制阵列容量会更有效吗?

ios - 从 PHAsset 加载图像非常慢

ios - 为什么新建UIView类时,不能一起创建XIB?

ios - 自定义动画属性返回 NSNull?

ios - 苹果开发推送通知服务器地址

iphone - POST 请求中的 & 符号造成严重破坏

ios - 无法将类型 'UITableViewCell' (0x10c111c68) 的值转换为子类

swift - MLDataTable 中 MLDataType 的值

欧洲日期的正则表达式/正则表达式 ("j. F Y")

iPhone iOS 大型 UIView 在显示时被裁剪为 320x460。如何预防?