terça-feira, 27 de setembro de 2011

iOS Quick Tip - Turning Off Automatic Reference Counting aka ARC

When developing your new apps under the newest XCode beta with iOS 5 you`ll get the ARC as gift, ok not a full git YET, but still a powerful gift.
So the question(s) arises: "What to do with code that isn`t under ARC yet?", "Can I turn it off?", "Can I turn it off only in a few files?"

Easy now, that is not a big trouble, I do hope that Apple engineering team improve this but for now let`s stick with what we got.

Turning ARC off for a new project (XCode Preview 7 onward *we hope*)

This is the easiest way, when creating a new project just check/uncheck "Use Automatic Reference Counting"


Turning it off for a few files
I`m personally not a huge fan of compile parameters but at least we have then when nothing else help us.
So open your project and select your desired target. Then select Build Phases, expand Compile Sources. There you`ll find the files that XCode is compiling, select everyone that will not use ARC and hit "enter". In the white box type -fno-objc-arc and hit enter. Done!



That`s all we need to stop using ARC in some of our project files, and whenever you change your mind just remove the flag.





quarta-feira, 21 de setembro de 2011

Fabulosa ilustração do nosso amado Legend of Zelda


Vi no capinaremos

Apps for iOS - Jetpack Joyride


We are used to lots of apps every month but some times one or another really does all that "OMG THAT`S AWESOME!" and Jetpack Joyride from @halfbrick is exactly that kind of app. Congrats @halfbrick team, this is a great and inspiring job for us devs and gamers.

It`s as simple as a running game with a cool machine, rainbow, shark and other jetpacks, some machine to ride and lots of awesomeness. The game is very beautiful, his background goes fast backwards in parallax movement (different speed for each background layer) while you go forward flying or riding a hot machine. The characters have expressions in their movements, even being a little small, and the animations are smooth.

Its very easy to play, your only command is up and down holding or releasing the touch screen, if you catch a ride a nice machine will appear and touching transforms in the machine command. Besides the music and the sound effects are nice too, sometimes the music starts to repeat to much but you never get  tired of it since you probably will have breaks between one play round and another.

Oh and there is all those kick ass itens like dynamites, head starts, golden machines, new cloths and new jetpacks, and more. And if you get a special x coin you can spin the machine to get a prize after you die, sometimes you can revive others you`ll simply explode with bombs and get shot a little further in the level.

The screen video bellow show a friend o`mine playing on its ipod touch and the other video is the official trailer. The last message I have to give you all, if you play this you won`t stop playing....don`t say I didn`t warned you.



sábado, 17 de setembro de 2011

iOS Quick Tips - Flip Animation

Whenever you have views in your fancy applications you`re probably thinking about some animations to make navigation smoother between some screens or between views.
Let`s imagine that we are building a card based game and that at some point we want to touch the card and it`ll flip revealing it front side.

Start by creating a new project on xcode. File > New > New Project or simply command+shift+N.
Select single view application and click next. Fill the info in the next screen, don`t check anything and let iphone device selected.

Create two new files a view nib and a view controller named FlipAnimationCardView and FlipAnimationCardViewController.
After creating them open the nib and attach its file owner to our view controller and attach our root view to the view controller.
After it you can put two UIButton, make them fullscreen with different image in each one, them link them and its touch up actions to our view controller.
The video bellow show how to do these steps.



Now we can place our animation in action with onCardBackTouched and onCardFrontTouched.
UIView has a static methods for animation, we`ll use transitionWithView but you can play with animateWithDuration too.



In the code above we are saying to the UIView class that we want a transition using btnCardBack during 1.5 seconds and we are setting the animation to be a Flip From Right type and telling the UIView to animate it with a EaseInOut curve so it will be smooth. There are more options and you only need to separate the parameters with | so the function identify what it have to do.
During the animation we ask the view to change btnCardBack and btnCardFront alpha property to 0.0 and 1.0, so it will vanish from the screen showing the front part (btnCardFront).
Every property that can be animated and is inserted into animations block will be animated by the UIView method.

The code above only animate our back part of the card, how can we animate its front card at the same time so it will appear like we are really flipping it?

We need to add this second part, the same code but passing btnCardFront as the view to use in the transition.



From here you can insert this into onCardBackTouched and the result should be 



Before we go testing lets tie app delegate to our new view controller so it`ll appear when the app execute.

Your appdelegate.h should be like this



In the .m file your didFinishLaunching method should be similar to this


Now build your code and test it on the simulator, you will see the card flipping nice and smoothly, but when you touch it again nothing happens, why? Because we didn`t created any animation to make it flip back. To do this insert the following code into onCardFrontTouched



If you execute it at this stage you will see the card flip back after you see the front part, very nice and not so hard to do. This code we made works for both fullscreen and not-fullscreen cards, but there is a shortcut for fullscreen cards.

Change our touch actions code to be like this



Cleaner than before but doesn`t work for cards smaller than the screen the reason is that transitionFromView:toView: animate the view and its parent.

Now to the last golden egg, what if we want the card to flip automagically after some time? We have two options performSelector and NSTimer, the first one will call our method after a time delay the second is a timer that can be scheduled to happen and have a loop parameter.
They have its pros and cons and I`ll not discuss this right now and for this tip we will use performSelector.
I decided to use viewWillAppear, but you can call your performSelector or register your timer wherever you want.

Add viewWillAppear to you view controller or code the performSelector inside it passing onCardBackTouched as parameter



Build and play with it, you can go further and flip it back automagically, insert multiple cards, make a category helper class to call this code from a view instance or something similar, just have fun!