-1.9 C
New York
Tuesday, December 24, 2024

ios – repair imageview X transfer animation?


I’ve an app like a reader. After I contact the proper fringe of the display screen, I am going to the following web page. After I contact the left fringe of the display screen, I am going to the earlier web page. My pages comprise photographs. The picture is full display screen measurement and 20pt extra from one of many edges of the display screen. And after I present the web page, I would like the picture transfer to the proper from 0pt to 20pt or left from 0pt to -20pt relying on imageDirection property from json file. I exploit this code to do that:

reader container:

class ReaderController: UIViewController {
        
    var pagesData = [PageData]()
    var imageArray: [UIImage] = []
    var index = Int()
    var pageIndex: Int = -1
    
    let pageContainer: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()

    let pageViews: [PageLayout] = {
        let view = [PageLayout(), PageLayout()]
        view[0].translatesAutoresizingMaskIntoConstraints = false
        view[1].translatesAutoresizingMaskIntoConstraints = false
        return view
    }()
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()

        loadImages()
        setupViews()
        setupConstraints()

        pageViews[0].index = index
        pageViews[0].imageArray = imageArray
        pageViews[1].index = index
        pageViews[1].imageArray = imageArray
        pageViews[0].pageIndex = pageIndex
        pageViews[1].pageIndex = pageIndex

        pageTransition(animated: false, course: "fromRight")
    }
    
    
    func setupViews() {
        pageContainer.addSubview(pageViews[0])
        pageContainer.addSubview(pageViews[1])
        view.addSubview(pageContainer)
    }
    
    
    func setupConstraints() {
        pageContainer.topAnchor.constraint(equalTo: view.topAnchor, fixed: 0.0).isActive = true
        pageContainer.bottomAnchor.constraint(equalTo: view.bottomAnchor, fixed: 0.0).isActive = true
        pageContainer.leadingAnchor.constraint(equalTo: view.leadingAnchor, fixed: 0.0).isActive = true
        pageContainer.trailingAnchor.constraint(equalTo: view.trailingAnchor, fixed: 0.0).isActive = true

        pageViews[0].topAnchor.constraint(equalTo: pageContainer.topAnchor).isActive = true
        pageViews[0].bottomAnchor.constraint(equalTo: pageContainer.bottomAnchor).isActive = true
        pageViews[0].leadingAnchor.constraint(equalTo: pageContainer.leadingAnchor).isActive = true
        pageViews[0].trailingAnchor.constraint(equalTo: pageContainer.trailingAnchor).isActive = true

        pageViews[1].topAnchor.constraint(equalTo: pageContainer.topAnchor).isActive = true
        pageViews[1].bottomAnchor.constraint(equalTo: pageContainer.bottomAnchor).isActive = true
        pageViews[1].leadingAnchor.constraint(equalTo: pageContainer.leadingAnchor).isActive = true
        pageViews[1].trailingAnchor.constraint(equalTo: pageContainer.trailingAnchor).isActive = true
    }
    
    func loadData(fileName: Any) -> PagesData {
        var url = NSURL()
        url = Bundle.foremost.url(forResource: "textual content", withExtension: "json")! as NSURL
        let knowledge = strive! Information(contentsOf: url as URL)
        let particular person = strive! JSONDecoder().decode(PagesData.self, from: knowledge)
        return particular person
    }
    
    func loadImages() {
        for imageIndex in 1...18 { imageArray.append(UIImage(named: "web page(imageIndex)")!) }
    }
        
    override func touchesBegan(_ touches: Set, with occasion: UIEvent?) {
        for contact in touches {
            if(contact.view == pageViews[0] || contact.view == pageViews[1]) {
                let location = contact.location(in: view.self)
                if view.safeAreaInsets.left > 30 {
                    if (location.x > self.view.body.measurement.width - (view.safeAreaInsets.left * 1.5)) {
                        pageTransition(animated: true, course: "fromRight")
                    } else if (location.x < (view.safeAreaInsets.left * 1.5)) {
                        pageTransition(animated: true, course: "fromLeft")
                    }
                }
                else {
                    if (location.x > self.view.body.measurement.width - 40) {
                        pageTransition(animated: true, course: "fromRight")
                    } else if (location.x < 40) {
                        pageTransition(animated: true, course: "fromLeft")
                    }
                }
            }
        }
    }
    
    func pageTransition(animated: Bool, course: String) {
        let end result = loadData(fileName: pagesData)

        change course {
        case "fromRight":
            pageIndex += 1
        case "fromLeft":
            pageIndex -= 1
        default: break
        }

        pageViews[0].pageIndex = pageIndex
        pageViews[1].pageIndex = pageIndex

        if pageIndex <= -1 {
            pageIndex = 0
        } else if pageIndex >= end result.pagesData.rely {
            pageIndex = end result.pagesData.rely - 1
            finishReading()
        } else {

            let fromView = pageViews[0].isHidden ? pageViews[1] : pageViews[0]
            let toView = pageViews[0].isHidden ? pageViews[0] : pageViews[1]
            toView.configure(theData: end result.pagesData[pageIndex])

            if animated {
                toView.isHidden = false
                let animation = CATransition()
                animation.length = 0
                animation.timingFunction = CAMediaTimingFunction(title: kCAMediaTimingFunctionEaseInEaseOut)
                animation.isRemovedOnCompletion = true
                animation.subtype = course

                let tapAnimationType = "fade"
                change tapAnimationType {
                case "pageCurl": animation.sort = "pageCurl"
                case "fade": animation.sort = "fade"
                case "moveIn": animation.sort = "moveIn"
                case "push": animation.sort = "push"
                case "reveal": animation.sort = "reveal"
                default: animation.sort = "fade" }

                self.pageContainer.layer.add(animation, forKey: "pageAnimation")
                self.pageContainer.bringSubview(toFront: toView)
            }
            else {
                fromView.isHidden = true
                toView.isHidden = false
            }
        }
    }
    
}

web page format:

class PageLayout: UIView {
        
    var imageArray: [UIImage] = []
    var index = Int()
    var pageIndex = Int()
    
    var imageDirection = ""
    
    var imageViewTopConstraint = NSLayoutConstraint()
    var imageViewBottomConstraint = NSLayoutConstraint()
    var imageViewLeadingConstraint = NSLayoutConstraint()
    var imageViewTrailingConstraint = NSLayoutConstraint()
    
    non-public let imageView: UIImageView = {
        let picture = UIImageView()
        picture.contentMode = .scaleAspectFill
        picture.translatesAutoresizingMaskIntoConstraints = false
        return picture
    }()
    
    non-public let textLabel: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()
    
    let screenWidth = UIScreen.foremost.bounds.measurement.width
    
    override init(body: CGRect) {
        tremendous.init(body: body)
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("Not taking place")
    }
    
    func setupViews() {
        addSubview(imageView)
        addSubview(textLabel)
    }
    
    func setupConstraints() {
        
        let scenes = UIApplication.shared.connectedScenes
        let windowScene = scenes.first as? UIWindowScene
        let window = windowScene?.home windows.first
        let safeArea = window?.safeAreaInsets
        
        textLabel.leadingAnchor.constraint(equalTo: leadingAnchor, fixed: safeArea!.left+16.0).isActive = true
        textLabel.trailingAnchor.constraint(equalTo: trailingAnchor, fixed: -safeArea!.right-16.0).isActive = true
        textLabel.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
        textLabel.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        
        removeConstraints([imageViewTopConstraint, imageViewBottomConstraint, imageViewLeadingConstraint,
                           imageViewTrailingConstraint])
        
        change imageDirection {
            
        case "left":
            
            imageViewTopConstraint = imageView.topAnchor.constraint(equalTo: topAnchor, fixed: 0.0)
            imageViewBottomConstraint = imageView.bottomAnchor.constraint(equalTo: bottomAnchor, fixed: 0.0)
            imageViewLeadingConstraint = imageView.leadingAnchor.constraint(equalTo: leadingAnchor, fixed: -20.0)
            imageViewTrailingConstraint = imageView.trailingAnchor.constraint(equalTo: trailingAnchor, fixed: 0.0)
            
            addConstraints([imageViewTopConstraint, imageViewBottomConstraint, imageViewLeadingConstraint, imageViewTrailingConstraint])
            
            imageView.layer.removeAllAnimations()
            imageView.remodel = CGAffineTransformTranslate(.identification, 0, 0)
            UIView.animate(withDuration: 4.0, delay: 0, animations: {
                self.imageView.remodel = CGAffineTransformTranslate(.identification, 20, 0)
            }, completion: nil)
            
        case "proper":
            
            imageViewTopConstraint = imageView.topAnchor.constraint(equalTo: topAnchor, fixed: 0.0)
            imageViewBottomConstraint = imageView.bottomAnchor.constraint(equalTo: bottomAnchor, fixed: 0.0)
            imageViewLeadingConstraint = imageView.leadingAnchor.constraint(equalTo: leadingAnchor, fixed: 0.0)
            imageViewTrailingConstraint = imageView.trailingAnchor.constraint(equalTo: trailingAnchor, fixed: 20.0)
            
            addConstraints([imageViewTopConstraint, imageViewBottomConstraint, imageViewLeadingConstraint, imageViewTrailingConstraint])
            
            imageView.layer.removeAllAnimations()
            imageView.remodel = CGAffineTransformTranslate(.identification, 0, 0)
            UIView.animate(withDuration: 4.0, delay: 0, animations: {
                self.imageView.remodel = CGAffineTransformTranslate(.identification, -20, 0)
            }, completion: nil)
            
        default: break
        }
    }
        
    func configure(theData: PageData) {
        textLabel.textual content = theData.textData
        imageDirection = theData.imageDirection
        imageView.picture = imageArray[pageIndex]
        setupConstraints()
    }
    
}

different:

struct PagesData: Decodable {
    var pagesData: [PageData]
}

struct PageData: Decodable {
    let textData, imageDirection: String
}

JSON:

{
    "pagesData" : [
        
        {
            "textData" : "1",
            "imagePosition" : "left",
        },
        
        {
            "textData" : "2",
            "imagePosition" : "left",
        },

        {
            "textData" : "3",
            "imagePosition" : "right",
        },
        
        {
            "textData" : "4",
            "imagePosition" : "right",
        },
    ]
}

What I’ve:

All works wonderful after I flip the web page and await the animation completed. However after I not await the animation completed and go to subsequent web page I see that present web page with picture jumps rapidly to 0 and subsequent I see animation for subsequent web page.

What I would like:

after I not await the animation of present web page completed and go to subsequent web page I can not see the present web page jumps to 0. I need to see easily flip

Video to breed the issue:

At this video I at all times go to subsequent web page not await the animation of present web page completed. For first, second and third pages all works wonderful however for subsequent photographs I’ve a issues

https://drive.google.com/file/d/1cms3G_o8W8bdGj9t5gAnItPvzFN7Z3mG/view?usp=sharing

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles