2

I need guidance in how to animate small 2d things such as, for example, a character that simply remains blinking. Are SpriteKit and cocos2d "too much" to implement this sort of things? I'm not planning to make a game, I just want to mix standard iOS controls with artwork performing simple animations like the mentioned, but maybe I'll need to make more elaborate animations in a near future (a character running, for example). Should I use Core Animation framework for this? Or maybe another and better alternative?

I'll support iOS 7.0+

Thanks in advance

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
AppsDev
  • 12,319
  • 23
  • 93
  • 186
  • 1
    Use what's nearby. Blinking is hardly an animation, you just cycle the "hidden" or "visible" property between on and off. Even image-based animations can be implemented with ease with any of the technologies you mentioned. – CodeSmile Sep 05 '14 at 08:09
  • 3
    Tip for the future: Avoid "best way" in title as it invites close votes from people too lazy to read the question. – amphetamachine Sep 05 '14 at 14:42
  • Agree with amphetamachine (in his opinion and his pro-schedule-narcotics-themed name ;-)) - I found your post while reviewing "voted to close" questions. Yours invited several "opinion based" close votes. I voted to keep open. Will amend. – Joshua Nozzi Sep 05 '14 at 14:52
  • 1
    @LearnCocos2D I disagree. See my answer and my comment on iZabala's answer. The timer-based hidden/visible approach won't scale and is awfully demanding on a mobile device because of the use of timers. Using Core Animation lets the system decide whether it needs to sacrifice frame rate for UI responsiveness. This adds up if you have "a lot" (less than you might think) of blinking elements. Should you start having blinking elements with differing frequencies, you need at least one timer per frequency. At this point, you're reinventing a crude (and resource-rude) version of Core Animation. – Joshua Nozzi Sep 05 '14 at 15:01

2 Answers2

2

Core Animation is best here, since you're specifically wanting to cause a user interface element to blink (rather than building a game with a board and sprites). Use Core Animation to set up a repeating animation to change its alpha value.

Use of timings (like Ease-In-Ease-Out) can even add a "Mac sleep light" breathing effect. Using this versus iZabala's (admittedly functional) approach means you're letting the system decide if every single frame of this animation can be run in a given moment. If it's too busy to animate and keep the UI responsive, Core Animation will drop frames as needed.

You can also group animations (so if you have a "bank of buttons with blinking indicators", you can run them all in the same animation group with the same timing so they don't get out of sync with each other and look like a sparkly Christmas tree).

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
-1

You can use the class NSTimer to call a function each X time. For example, if you want something to blink you can do something like this:

NSTimer* myTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self
                                   selector: @selector(blink) userInfo: nil repeats: YES];

-(void) blink { //supose that myObject is an UIView in this example
   if(myObject.hidden){ 
     myObject.hidden=NO;

   }
   else { 
     myObject.hidden=YES;
   }
}

You can do the same using position to move, alpha or transparency to fade out, fade in... play with the properties the object has.

Draelach
  • 531
  • 3
  • 14
  • 4
    Running a bunch of timers to hide/reveal UI elements is no way to go through life, son. ;-) They insist upon being heard, whether the system is a "little bit too busy" to handle every. single. frame. or not, whereas using the system's animation libraries optimizes this (it'll drop frames as needed to keep the UI responsive if it's too busy, and more). – Joshua Nozzi Sep 05 '14 at 14:54