Git Product home page Git Product logo

swiftui-parallax-views's Introduction

Layer Explosion (SwiftUI)

This is a SwiftUI library that adds a pseudo-3d effect to your views, imitating the View Debugger for Xcode and UIKit.

parallax.mp4

Installing

It can be installed with Swift Package Manager:

https://github.com/louis1001/layer-explosion

Usage

Parallax gestures

To create a "parallax context" you use .parallaxTouchGesture() as a view modifier. This works like other gestures, getting drag events withing the view bounds, but it also makes that view a parallax layer which means it will be rotated on the gesture.

var body: some View {
    VStack {
        Text("Child View").padding()
    }
    .parallaxTouchGesture() // Dragging within this view will rotate it
}

Additionally, you can use .parallaxMotionGesture() to control the effect using the device's rotation.

var body: some View {
    VStack {
        Text("Child View").padding()
    }
    .parallaxMotionGesture(strength: 0.2) // Dragging within this view will rotate it
}

To control how sensitive the effect is, you can call the modifiers with strength: <Double>, 1 being the normal sensitivity. This works for both gesture modifiers.

Parallax Layer

After using parallaxTouchGesture, you can add .parallaxLayer() to any child view, which separates a view from it's parent. That also moves the view along the "z-axis", but because of the way it's rendered it actually doesn't change size on the screen.

var body: some View {
    VStack {
        VStack {
            Text("Nested Layer")
                .padding()
                .parallaxLayer() // This is the 3rd layer
        }
        .padding()
        .parallaxLayer() // This would be the 2nd layer (after the one from gesture)
    }
    .parallaxTouchGesture(strength: 0.2)
}

In the case of ZStack you need to separate the views some other way, because nesting alone doesn't tell you how the views are rendered. So you can add a zIndex: <Int> parameter to the call; that sets a relative layer separation between sibling views.

var body: some View {
    ZStack {
        Color.blue // Background
            .parallaxLayer(zIndex: 0) // This is the same as `.parallaxLayer()`
            
        Text("Content") // Foreground
            .padding()
            .parallaxLayer(zIndex: 1) // This would mean `Text` is a child of `Color.blue`
    }
    .parallaxTouchGesture(strength: 0.2)
}
parallax_usage.mp4

Current Problems

A few. I'll update this section later.

swiftui-parallax-views's People

Contributors

louis1001 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.