قالب وردپرس درنا توس
Home / IOS Development / ios – UIScrollview animation transition from view to view

ios – UIScrollview animation transition from view to view



I try to perform a transition animation every time a user scrolls on a paginated view, ie: From Page 1 to Page 2 .

  Main Representation

Unfortunately, I haven't been able to recreate it.

Attachments below are what I've done:

  class OnboardingParallaxImageView: BaseUIView, UIScrollViewDelegate {

let allImages = [#imageLiteral(resourceName: "onboarding_handshake_icon"), #imageLiteral(resourceName: "onboarding_paylinc_icon")]

var activeCurrentPage = 1

la bgView: UIImageView = {
let image = #imageLiteral (resource name: "onboard_bg_gradient")
let view = UIImageView (image: image)
view.translatesAutoresizingMaskIntoConstraints = false
return view
} ()

la firstImageView: UIImageView = {
let view = UIImageView ()
view.translatesAutoresizingMaskIntoConstraints = false
return view
} ()

la secondImageView: UIImageView = {
let view = UIImageView ()
view.is Hidden = true
view.translatesAutoresizingMaskIntoConstraints = false
return view
} ()

was firstImageHeightAnchor: NSLayoutConstraint?
was firstImageWidthAnchor: NSLayoutConstraint?
was secondImageHeightAnchor: NSLayoutConstraint?
was secondImageWidthAnchor: NSLayoutConstraint?

override func setupViews () {
super.setupViews ()
addSubview (bgView)
addSubview (firstImageView)
addSubview (secondImageView)

bgView.widthAnchor.constraint (likeTo: widthAnchor) .isActive = true
bgView.heightAnchor.constraint (likeTo: heightAnchor) .isActive = true
bgView.centerXAnchor.constraint (likeTo: centerXAnchor) .isActive = true
bgView.centerYAnchor.constraint (likeTo: centerYAnchor) .isActive = true

firstImageWidthAnchor = firstImageView.widthAnchor.constraint (likeTo: widthAnchor)
first image width anchor? .isActive = true
firstImageHeightAnchor = firstImageView.heightAnchor.constraint (likeTo: heightAnchor)
firstImageHeightAnchor? .isActive = true
firstImageView.centerYAnchor.constraint (likeTo: centerYAnchor) .isActive = true
firstImageView.centerXAnchor.constraint (likeTo: centerXAnchor) .isActive = true
firstImageView.image = allImages [0]

secondImageWidthAnchor = secondImageView.widthAnchor.constraint (likeTo: widthAnchor)
second image width anchor? .isActive = true
secondImageHeightAnchor = secondImageView.heightAnchor.constraint (likeTo: heightAnchor)
secondImageHeightAnchor? .isActive = true
secondImageView.centerYAnchor.constraint (likeTo: centerYAnchor) .isActive = true
secondImageView.centerXAnchor.constraint (likeTo: centerXAnchor) .isActive = true
secondImageView.image = allImages [1
] } override func layoutSubviews () { super.layoutSubviews () la frameWidth = frame.size.width secondImageHeightAnchor? .constant = - (frameWidth - 32) second image width anchor? .constant = - (frameWidth - 32) } func scrollViewDidScroll (_ scrollView: UIScrollView) { let offSet = scrollView.contentOffset.x la frameWidth = frame.size.width / 2 la toUseConstant = (CGFloat (abs (offSet)) / frameWidth) if activeCurrentPage == 1 { if offSet <= 0 { firstImageHeightAnchor? .constant = 0 first image width anchor? .constant = 0 firstImageView.isHidden = false secondImageView.isHidden = true } else { firstImageHeightAnchor? .constant + = - (toUseConstant) first image width anchor? .constant + = - (toUseConstant) firstImageView.isHidden = false secondImageHeightAnchor? .constant + = - (toUseConstant) second image width anchor? .constant + = - (toUseConstant) secondImageView.isHidden = false secondImageView.alpha = toUseConstant } } UIView.animate (withDuration: 0.5) { self.layoutSubviews () } } func scrollViewDidEndDecelerating (_ scrollView: UIScrollView) { self.activeCurrentPage = scrollView.currentPage } }

This is the result of what I have managed to achieve:

 Failed Representation

How can I go from A to B without having fun.

Thanks


Source link