23.2 C
Canberra
Wednesday, November 13, 2024

ios – Is there a solution to repair the delay earlier than the picture seems?


I am utilizing this code to create easy reader. In my reader I’ve picture view to point out pictures and textual content label to point out textual content on display screen. All the things works tremendous, however once I launch the app and begin turning pages, I expertise a slight delay of some seconds earlier than the following web page seems. But when I scroll ahead via all of the pages. Then I will scroll again all of the pages. And once more I’ll begin flipping via the pages ahead. Then this delay of some seconds will disappear. Is there a solution to repair this delay? Perhaps I have to by some means pre-load the pictures?

ReaderController

class ReaderController: UIViewController {
    
    var pagesData = [PageData]()
    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()

        setupViews()
        setupConstraints()

        pageViews[0].index = index
        pageViews[1].index = index
        pageViews[0].pageIndex = pageIndex
        pageViews[1].pageIndex = pageIndex
        
        pageTransition(animated: false, route: "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! Knowledge(contentsOf: url as URL)
        let individual = strive! JSONDecoder().decode(PagesData.self, from: knowledge)
        return individual
    }
    
    override func touchesBegan(_ touches: Set, with occasion: UIEvent?) {
        for contact in touches {
            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, route: "fromRight")
                } else if (location.x < (view.safeAreaInsets.left * 1.5)) {
                    pageTransition(animated: true, route: "fromLeft")
                }
            }

            else {
                if (location.x > self.view.body.measurement.width - 40) {
                    pageTransition(animated: true, route: "fromRight")
                } else if (location.x < 40) {
                    pageTransition(animated: true, route: "fromLeft")
                }
            }
        }
    }
    
    func pageTransition(animated: Bool, route: String) {
        let consequence = loadData(fileName: pagesData)

        swap route {
        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 >= consequence.pagesData.rely {
            pageIndex = consequence.pagesData.rely - 1
        } else {

            let fromView = pageViews[0].isHidden ? pageViews[1] : pageViews[0]
            let toView = pageViews[0].isHidden ? pageViews[0] : pageViews[1]
            toView.configure(theData: consequence.pagesData[pageIndex])
            fromView.isHidden = true
            toView.isHidden = false
        }
    }
    
}

PageLayout

class PageLayout: UIView {
    
    var index = Int()
    var pageIndex = Int()
    
    non-public let imageView: UIImageView = {
        let picture = UIImageView()
        picture.contentMode = .scaleAspectFill
        picture.translatesAutoresizingMaskIntoConstraints = false
        return picture
    }()

    var imageViewTopConstraint = NSLayoutConstraint()
    var imageViewBottomConstraint = NSLayoutConstraint()
    var imageViewLeadingConstraint = NSLayoutConstraint()
    var imageViewTrailingConstraint = NSLayoutConstraint()
    
    non-public let textLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont(title: "ComicSansMS-Daring", measurement: 19)
        label.textColor = .white
        label.textAlignment = .heart
        label.numberOfLines = 0
        label.translatesAutoresizingMaskIntoConstraints = false
        label.layer.shadowColor = UIColor.black.cgColor
        label.layer.shadowOffset = CGSize.zero
        label.layer.shadowOpacity = 1.0
        label.layer.shadowRadius = 4
        return label
    }()

    var textLabelTopConstraint = NSLayoutConstraint()
    var textLabelBottomConstraint = NSLayoutConstraint()
    var textLabelLeadingConstraint = NSLayoutConstraint()
    var textLabelTrailingConstraint = NSLayoutConstraint()
    
    override init(body: CGRect) {
        tremendous.init(body: body)
        addSubview(imageView)
        addSubview(textLabel)
        setupConstraints()
    }
    
    required init?(coder: NSCoder) {
        fatalError("Not taking place")
    }
    
    func setupConstraints() {
        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: 0.0)
        addConstraints([imageViewTopConstraint, imageViewBottomConstraint, imageViewLeadingConstraint, imageViewTrailingConstraint])
        
        textLabelTopConstraint = imageView.topAnchor.constraint(equalTo: topAnchor, fixed: 0.0)
        textLabelBottomConstraint = imageView.bottomAnchor.constraint(equalTo: bottomAnchor, fixed: 0.0)
        textLabelLeadingConstraint = imageView.leadingAnchor.constraint(equalTo: leadingAnchor, fixed: 0.0)
        textLabelTrailingConstraint = imageView.trailingAnchor.constraint(equalTo: trailingAnchor, fixed: 0.0)
        addConstraints([textLabelTopConstraint, textLabelBottomConstraint, textLabelLeadingConstraint, textLabelTrailingConstraint])
    }
        
    func configure(theData: PageData) {
        textLabel.textual content = theData.textData
        imageView.picture = UIImage(named: "web page(pageIndex+1)")
    }
    
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles