Skip to content

Commit 5a8f042

Browse files
committed
Merge pull request #22 from rakaramos/source
Add UIMotionEffects hint
2 parents 4841979 + 8e57e06 commit 5a8f042

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
layout: post
3+
title: "Don't forget about UIMottionEffect"
4+
author: rafa
5+
date: 2015-08-24 20:00:52 +0200
6+
comments: false
7+
categories:
8+
---
9+
10+
`UIMotionEffects` was first introduced in iOS 7. The [WWDC session](https://developer.apple.com/videos/enterprise/#30) which presented this, amongst other cool things, is named Implementing Engaging UI on iOS. Nevertheless, `UIMotionEffects` is still overlooked. But not today, let's make something cool with it.
11+
12+
Motion effects is an easy way to react to external variations on the device's orientation. To say, `UIKit` performs UI changes whenever the user tilts the device, vertically or horizontally.
13+
14+
Let's use `UIInterpolatingMotionEffect` a subclass of `UIMotionEffects`, with `MapKit`. Notice how appealing it is.
15+
16+
{% img center /images/dont-forget-about-motion-effects/01.gif %}
17+
18+
Sweet, right?
19+
20+
Achieving it, is easier than you think. Just a few lines of code and you're good to go:
21+
22+
```swift
23+
var horizontalMotionEffect = UIInterpolatingMotionEffect(keyPath: "center.x", type: .TiltAlongHorizontalAxis)
24+
horizontalMotionEffect.minimumRelativeValue = -100
25+
horizontalMotionEffect.maximumRelativeValue = 100
26+
27+
mapView.addMotionEffect(horizontalMotionEffect)
28+
```
29+
30+
Think of `minimumRelativeValue` and `maximumRelativeValue` as leading and tralling constraints, respectivily, to its `superview`.
31+
32+
That's why you have to create the `UIView`, `MKMapView` in this case, outside its `superview`s bounds. Like so:
33+
34+
{% img center /images/dont-forget-about-motion-effects/02.png %}
35+
36+
37+
As the user tilts the device, `UIInterpolatingMotionEffect` translates the fixed offset values returned by the system to the range of specified values, then `UIKit` applies the translated values to any target views.
38+
39+
40+
Don’t forget about this! Details matters and it's what users love in mobile apps!
Loading
Loading

0 commit comments

Comments
 (0)