iOS App Development For Dummies by Jesse Feiler
Author:Jesse Feiler
Language: eng
Format: epub
ISBN: 9781118871072
Publisher: Wiley
Published: 2014-03-20T01:47:56+00:00
CGPoint center = CGPointMake(self.car.center.x,
self.view.frame.origin.y + self.car.frame.size.height/2);
[UIView animateWithDuration:3 animations:^ {
self.car.center = center;
}
completion:^(BOOL finished){
[self rotate];
}];
}
- (void)rotate {
}
- (void)returnCar {
}
- (void)continueRotation {
}
Now, run your program and click or touch the Test Drive button. You’ll see your car move up the screen. You’re on your way!
Looking more closely at Listing 10-1, you see that you start by creating the coordinate (CGPoint) of where you would like the car to end up.
A car is just another view. The following code shows how to move the car on-screen by simply moving the center of the view that holds the image of the car.
CGPoint center = CGPointMake(self.car.center.x,
self.view.frame.origin.y + self.car.frame.size.height/2);
You use the center and frame properties primarily for manipulating the view. If you're changing only the position of the view (and not its size), the center property is the preferred way to do so.
CGPointMake is a function that creates a point for you when you specify the y and x coordinates as parameters. (You’ll be setting the car’s new center point.)
You can leave the x coordinate as is. Doing so makes the car drive right up the center of the screen.
self.car.center.x
Here’s the y coordinate:
self.view.frame.origin.y + self.car.frame.size.height/2)
self.view.frame.origin.y is the top of the view, but if you have the center there, half the car is off the screen. To keep it all on the screen, you add back half the car’s height by including car.frame.size.height/2.
Notice I'm adding to the y coordinate because y increases as you move down the screen from the origin.
So, how do you get the sucker to actually move? Listing 10-1 uses the following code:
[UIView animateWithDuration:3 animations:^ {
self.car.center = center;
}
completion:^(BOOL finished){
[self rotate];
}];
animateWithDuration:animations:completion: is a UIView class method that allows you to set an animation duration and specify what you want animated as well as a completion handler that's called when the animation is complete.
First you specify that you want the animation to take three seconds:
animateWithDuration:3
and then you pass in an animation block with what you want animated:
animations:^ {
self.car.center = center;
}
This sets the new center you just computed, taking three seconds to move it from start to finish.
If the preceding syntax seems mysterious (what’s the ^ doing there and what’s up with the code as part of the message?), don’t worry: I explain blocks in the next section.
So although that’s all there is to get the car to move across the screen, you’re not done. You want it to rotate and then drive back across the screen and then rotate again. That’s where the completion handler comes in.
Although you can use a completion handler to simply let you know that an animation is finished, you can also use a completion handler to link multiple animations. (In fact, it’s the primary way to take care of that task.)
The completion handler that you specify
completion:^(BOOL finished){
[self rotate];
}
causes the rotate message to be sent when the animation is complete. You do the actual rotation in the rotate method.
Of course, right now, the rotate method does nothing. I have you add it so that the app will compile and run. I have you add returnCar and continueRotation to prevent the Incomplete Implementation TestDriveController.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(12569)
Hello! Python by Anthony Briggs(9914)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9795)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Dependency Injection in .NET by Mark Seemann(9337)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8296)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7065)
Microservices with Go by Alexander Shuiskov(6825)
Practical Design Patterns for Java Developers by Miroslav Wengner(6746)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6688)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Angular Projects - Third Edition by Aristeidis Bampakos(6093)
The Art of Crafting User Stories by The Art of Crafting User Stories(5618)
NetSuite for Consultants - Second Edition by Peter Ries(5557)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5360)
Kotlin in Action by Dmitry Jemerov(5062)
