3

I read here that iOS 7 offers a simple way to get parallax motion offsets. But it also shows you have to apply it to a UIView.

I need it for something different (a drawing algorithm). What would you do to obtain the pure CGPoint parallax motion offset if you would not want to create a hidden view just to obtain the value?

And how could I get this parallax motion offset so it also runs with iOS 6?

Community
  • 1
  • 1
openfrog
  • 40,201
  • 65
  • 225
  • 373
  • 1
    YOU CAN USE THIS https://github.com/Przytua/UIView-MWParallax – Kunal Balani Nov 24 '13 at 15:37
  • it runs for iOS 6 as well – Kunal Balani Nov 24 '13 at 15:37
  • Thank you @KunalBalani but I need somthing that does NOT require to setup a UIView. I simply want the raw parallax offset value for a different purpose. Think of a controller. UIView-MWParallax is a category on UIView and only works with UIView. To get the parallax offset I would have to create a dummy view and then query its position. – openfrog Nov 24 '13 at 16:16
  • 2
    Converting the code @KunalBalani references from a `UIView` category to an independent object looks pretty trivial. Just change it to its own class, see what fails to compile, and cleanup a little. The one major change would be to replace the `transform` settings to a point in `motionsUpdated`, but again, that's pretty trivial. It certainly looks like a good starting point. – Rob Napier Nov 24 '13 at 18:31
  • Use the Accelerometer, or Motion API, and utilize CATransform3D on a **rasterised** layer according to the updates fired by the sensor/s? – topLayoutGuide Jan 08 '14 at 13:16

1 Answers1

1

This answer is 3 years too late.

But, you could subclass UIMotionEffect and override keyPathsAndRelativeValues(forViewerOffset:) and implement whatever effect you want to apply to the view based on the viewer's viewing position.

The parameter viewerOffset provides an instance of UIOffset which will give you the viewing offset of the viewer. You need to return a dictionary with keyPaths as keys and the corresponding values that you compute based on the viewerOffset as the values for any of the animatable properties of the view.

import UIKit

class CustomMotionEffect: UIMotionEffect {

    override func keyPathsAndRelativeValues(forViewerOffset viewerOffset: UIOffset) -> [String : Any]? {
        var motionEffectDictionary = [String : Any]()

        //Horizontal
        if viewerOffset.horizontal > 0 { //Device tilted right

        } else if viewerOffset.horizontal < 0 { //Device tilted leftß

        } else {//Device horizontally perpendicular

        }

        //Vertical
        if viewerOffset.vertical > 0 { //Device tilted down

        } else if viewerOffset.vertical < 0 { //Device tilted up

        } else { //Device vertically perpendicular

        }
        return motionEffectDictionary
    }

}

Doing this will ensure you have control over how your effect should behave. But, this won't work on iOS6.