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

ios – 如何将自定义视图设置为自定义视图的动画,第1张

概述我正在构建一个类似flashcard的应用程序,我正在使用一个库KolodaView: https://github.com/Yalantis/Koloda 我有这个扩展,它返回一个UIView作为闪卡的前面: extension StudyViewController: KolodaViewDataSource {func kolodaNumberOfCards(koloda:KolodaV 我正在构建一个类似flashcard的应用程序,我正在使用一个库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,completion: nil)        showingFront = false    }}

过渡是成功的,但在完成后,闪卡将从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}

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

UIVIEw.TransitionFromVIEw(frontVIEw,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()})

湾在datasource方法中返回自定义视图,并在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,completion: nil)        } else {            UIVIEw.TransitionFromVIEw(backVIEw,completion: nil)        }        showingFront = !showingFront    }    func makeFrontVIEw() -> UIImageVIEw {        return UIImageVIEw()    }    func makeBackVIEw() -> UIImageVIEw {        return UIImageVIEw()    }}
总结

以上是内存溢出为你收集整理的ios – 如何将自定义视图设置为自定义视图的动画全部内容,希望文章能够帮你解决ios – 如何将自定义视图设置为自定义视图的动画所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1030735.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存