Skip to Content
iOS 7 Programming Cookbook
book

iOS 7 Programming Cookbook

by Vandad Nahavandipoor
October 2013
Intermediate to advanced
1053 pages
28h 7m
English
O'Reilly Media, Inc.
Content preview from iOS 7 Programming Cookbook

2.5. Adding a Dynamic Snap Effect to Your UI Components

Problem

Using an animation, you want to snap a view in your UI to a specific point on the screen, with the elasticity of a real-world snap effect. This means that when your UI component snaps to the given point, you will feel that it has elasticity built into it.

Solution

Instantiate an object of type UISnapBehavior and add it to an animator of type UIDynamicAnimator.

Discussion

To really understand how the snap dynamic behavior works, think about a small amount of jelly covered in oil with a string attached to it, sitting on a very smooth table. I know that sentence sounds really odd. But bear with me. Now imagine from another point on the table, pulling on that string to get the jelly to move from its initial point to the point you ordered it to move to. With the oil all around it, the jelly will move smoothly from that point to where you want it to go, and because it is jelly, it will wiggle when it snaps to position. This behavior is exactly what you can achieve with the UISnapBehavior class.

One of the use cases for this is when you have an app and some views on the screen, such as images, and you want the user to be able to dictate where those views have to be moved to create a customized UI for the user. One way of handling this is using the technique that we learned in Recipe 2.3, but that solution is quite rigid and has its own use cases. Here in this recipe, we have a view on our screen, and we want to allow the user to ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

iOS 4 Programming Cookbook

iOS 4 Programming Cookbook

Vandad Nahavandipoor
iOS 6 Programming Cookbook

iOS 6 Programming Cookbook

Vandad Nahavandipoor

Publisher Resources

ISBN: 9781449372446Errata Page