如何将自定义视图设置为自定义视图的动画

时间:2016-08-09 11:34:16

标签: 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
}
}

我希望能够通过点击将视图转换为后视图:

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
    }
}

转换成功但在完成后,闪卡将从KolodaView变为巨人UIView

如何将KolodaView转换为花药KolodaView

1 个答案:

答案 0 :(得分:1)

我没有使用data.frame所以我的答案对于手头的问题更为通用。

两种方法

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

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

KolodaView

使用当前方法设置动画视图,并使用完成块更新原始图像的最终状态。

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
}

<强>湾返回数据源方法中的自定义视图,并在didSelectCardAtIndex中切换其内部状态

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()
})

在这种情况下,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() } 的实现将包含初始实现的代码(customView.showingFront,transitionFromView)。如果您需要更多说明,请告诉我。

修改

toggleFrontBackState