IOS7 UIKit Dynamics Tutorial
2013-11-07 11:32
603 查看
This post is also available in: Chinese
(Simplified), Italian, Russian
Note from Ray: This is an abbreviated version of a chapter fromiOS
7 by Tutorials that we are releasing as part of the iOS
7 Feast. We hope you enjoy!
You have probably come to realize that iOS 7 is something of a paradox; while you’re being encouraged to do away with real-world metaphors and skeuomorphism, Apple encourages you at the same time to create user interfaces that feel real.
What does this mean in practice? The design goals of iOS 7 encourage you to create digital interfaces that react to touch, gestures, and changes in orientation as if they were physical objects far beyond a simple collection of pixels. The end result gives the
user a deeper connection with the interface than is possible through skin-deep skeuomorphism.
This sounds like a daunting task, as it is much easier to make a digital interface look real, than it is to make it feel real. However, you have some nifty new tools on your side: UIKit Dynamics and Motion Effects.
UIKit Dynamics is a full physics engine integrated into UIKit. It allows you to create
interfaces that feel real by adding behaviors such as gravity, attachments (springs) and forces. You define the physical traits that you would like your interface elements to adopt, and the dynamics engine takes care of the rest.
Motion Effects allows you to create cool parallax effects like you see when you tilt
the iOS 7 home screen. Basically you can harness the data supplied by the phone’s accelerometer in order to create interfaces that react to changes in phone orientation.
When used together, motion and dynamics form a powerhouse of user experience tools that make your digital interfaces come to life. Your users will connect with your app at a deeper level by seeing it respond to their actions in a natural, dynamic way.
UIKit dynamics can be a lot of fun; the best way to start learning about them is to jump in feet-first with some small examples.
Open Xcode, select File / New / Project … then select iOS\Application\Single
View Application and name your project DynamicsPlayground. Once the
project has been created, open ViewController.mand add the following code to the end
of
The above code simply adds a square
the interface.
Build and run your app, and you’ll see a lonely square sitting on your screen, as shown below:
If you’re running your app on a physical device, try tilting your phone, turning it upside-down, or even shaking it. What happens? Nothing? That’s right — everything is working as designed. When you add a view to your interface you expect it to remain firmly
stuck in place as defined by its frame — until you add some dynamic realism to your interface!
Still working in ViewController.m, add the following instance variables:
Add the following to the end of
I’ll explain this in a moment. For now, build and run your application. You should see your square slowly start to accelerate in a downward motion until it drops off the bottom of the screen, as so:
In the code you just added, there are a couple of dynamics classes at play here:
This class keeps track of the various behaviors that you add to the engine, such as gravity, and provides the overall context. When you create an instance of an animator, you pass in a reference view that the animator uses to define its coordinate system.
and exerts forces on one or more items, allowing you to model physical interactions. When you create an instance of a behavior, you associate it with a set of items — typically views. This way you can select which items are influenced by the behavior, in this
case which items the gravitational forces affect.
Most behaviors have a number of configuration properties; for example, the gravity behavior allows you to change its angle and magnitude. Try modifying these properties to make your objects fall up, sideways, or diagonally with varying rates of acceleration.
NOTE: A quick word on units: in the physical world, gravity (g) is expressed in meters per second squared and is approximately
equal to 9.8 m/s2. Using Newton’s second law, you can compute how far an object will fall under gravity’s influence with the following formula:
distance = 0.5 × g × time2
In UIKit Dynamics, the formula is the same but the units are different. Rather than meters, you work with units of thousands of pixels per second squared. Using Newton’s second law you can still work out exactly where your view will be at any time based on
the gravity components you supply.
Do you really need to know all this? Not really; all you really need to know is that a bigger value for gmeans
things will fall faster, but it never hurts to understand the math underneath.
Although you can’t see it, the square continues to fall even after it disappears off the bottom of your screen. In order to keep it within the bounds of the screen you need to define a boundary.
Add another instance variable
(Simplified), Italian, Russian
Note from Ray: This is an abbreviated version of a chapter fromiOS
7 by Tutorials that we are releasing as part of the iOS
7 Feast. We hope you enjoy!
You have probably come to realize that iOS 7 is something of a paradox; while you’re being encouraged to do away with real-world metaphors and skeuomorphism, Apple encourages you at the same time to create user interfaces that feel real.
What does this mean in practice? The design goals of iOS 7 encourage you to create digital interfaces that react to touch, gestures, and changes in orientation as if they were physical objects far beyond a simple collection of pixels. The end result gives the
user a deeper connection with the interface than is possible through skin-deep skeuomorphism.
This sounds like a daunting task, as it is much easier to make a digital interface look real, than it is to make it feel real. However, you have some nifty new tools on your side: UIKit Dynamics and Motion Effects.
UIKit Dynamics is a full physics engine integrated into UIKit. It allows you to create
interfaces that feel real by adding behaviors such as gravity, attachments (springs) and forces. You define the physical traits that you would like your interface elements to adopt, and the dynamics engine takes care of the rest.
Motion Effects allows you to create cool parallax effects like you see when you tilt
the iOS 7 home screen. Basically you can harness the data supplied by the phone’s accelerometer in order to create interfaces that react to changes in phone orientation.
When used together, motion and dynamics form a powerhouse of user experience tools that make your digital interfaces come to life. Your users will connect with your app at a deeper level by seeing it respond to their actions in a natural, dynamic way.
Getting started
UIKit dynamics can be a lot of fun; the best way to start learning about them is to jump in feet-first with some small examples.Open Xcode, select File / New / Project … then select iOS\Application\Single
View Application and name your project DynamicsPlayground. Once the
project has been created, open ViewController.mand add the following code to the end
of
viewDidLoad:
UIView* square = [[UIView alloc] initWithFrame: CGRectMake(100, 100, 100, 100)]; square.backgroundColor = [UIColor grayColor]; [self.view addSubview:square]; |
UIViewto
the interface.
Build and run your app, and you’ll see a lonely square sitting on your screen, as shown below:
If you’re running your app on a physical device, try tilting your phone, turning it upside-down, or even shaking it. What happens? Nothing? That’s right — everything is working as designed. When you add a view to your interface you expect it to remain firmly
stuck in place as defined by its frame — until you add some dynamic realism to your interface!
Adding gravity
Still working in ViewController.m, add the following instance variables:UIDynamicAnimator* _animator; UIGravityBehavior* _gravity; |
viewDidLoad:
_animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view]; _gravity = [[UIGravityBehavioralloc] initWithItems:@[square]]; [_animator addBehavior:_gravity]; |
In the code you just added, there are a couple of dynamics classes at play here:
UIDynamicAnimatoris the UIKit physics engine.
This class keeps track of the various behaviors that you add to the engine, such as gravity, and provides the overall context. When you create an instance of an animator, you pass in a reference view that the animator uses to define its coordinate system.
UIGravityBehaviormodels the behavior of gravity
and exerts forces on one or more items, allowing you to model physical interactions. When you create an instance of a behavior, you associate it with a set of items — typically views. This way you can select which items are influenced by the behavior, in this
case which items the gravitational forces affect.
Most behaviors have a number of configuration properties; for example, the gravity behavior allows you to change its angle and magnitude. Try modifying these properties to make your objects fall up, sideways, or diagonally with varying rates of acceleration.
NOTE: A quick word on units: in the physical world, gravity (g) is expressed in meters per second squared and is approximately
equal to 9.8 m/s2. Using Newton’s second law, you can compute how far an object will fall under gravity’s influence with the following formula:
distance = 0.5 × g × time2
In UIKit Dynamics, the formula is the same but the units are different. Rather than meters, you work with units of thousands of pixels per second squared. Using Newton’s second law you can still work out exactly where your view will be at any time based on
the gravity components you supply.
Do you really need to know all this? Not really; all you really need to know is that a bigger value for gmeans
things will fall faster, but it never hurts to understand the math underneath.
Setting boundaries
Although you can’t see it, the square continues to fall even after it disappears off the bottom of your screen. In order to keep it within the bounds of the screen you need to define a boundary.Add another instance variable
相关文章推荐
- iOS7新特征汇总[05]:初窥UIKit Dynamics
- UIKit Dynamics Tutorial
- IOS7开发~UIKit Dynamics
- IOS7开发~UIKit Dynamics
- UIKit Dynamics Tutorial
- iOS7 UIKit Dynamics 的初步研究
- iOS7—Day by day—Day0:UIKit Dynamics
- iOS UIKit Dynamics入门 UIKit动力学,实现重力、连接、碰撞、悬挂等动画效果
- iOS 力学动画生成器UIKit Dynamics 之碰撞效果讲解
- iOS7新特征汇总[05]初窥UIKit动力(UIKit Dynamics)
- [iOS开发] UIKit Dynamics
- UIKit Dynamics入门
- Chapter 5:UIKit Dynamics - iOS 8 Swift Programming cookBook 读书笔记
- iOS 7 by tutorials 学习笔记 01 - UIKit Dynamics and Motion Effects 上
- ios7新特性1-UI变化、UIKit动态行为支持与Text Kit新接口
- 【iOS开发】UIKit Dynamics
- UIKit Dynamics动力系统入门
- Beginning Auto Layout Tutorial in iOS 7: Part 1 -- ios7 中的 Auto Layout 使用教程 第一节
- iOS7中UIKit支持的动力行为之UIAttachmentBehavior
- iOS 9 学习系列:UIKit Dynamics