Skip to content

Instantly share code, notes, and snippets.

@ahmetardal
Created April 25, 2011 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmetardal/940348 to your computer and use it in GitHub Desktop.
Save ahmetardal/940348 to your computer and use it in GitHub Desktop.
EyeballAnimation Demo App
- (void) initializeTimerWithNSTimer
{
CGFloat interval = 1.0f / 50.0f;
[NSTimer scheduledTimerWithTimeInterval:interval
target:self
selector:@selector(animateBallNSTimer:)
userInfo:nil
repeats:YES];
}
- (void) initializeTimerWithCADisplayLink
{
_timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(animateBallCADisplayLink)];
_timer.frameInterval = 2;
[_timer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
- (void) performAnimationStep
{
//
// reposition the ball
//
CGPoint ballCenter = self.ballImageView.center;
CGFloat newX = ballCenter.x + _ballMovement.x;
CGFloat newY = ballCenter.y + _ballMovement.y;
self.ballImageView.center = CGPointMake(newX, newY);
//
// rotate the ball
//
self.ballImageView.transform =
CGAffineTransformRotate(self.ballImageView.transform, DegreesToRadians(_ballRotation));
//
// change the opacity of the ball
//
CGFloat newAlpha = self.ballImageView.alpha + _ballAlphaChange;
self.ballImageView.alpha = newAlpha;
//
// change move direction and rotation direction when
// the ball collides one of the borders of the screen
//
ballCenter = self.ballImageView.center;
if ((ballCenter.x > 300) || (ballCenter.x < 20)) {
_ballMovement.x *= -1;
_ballRotation *= -1;
}
if ((ballCenter.y > 440) || (ballCenter.y < 20)) {
_ballMovement.y *= -1;
_ballRotation *= -1;
}
//
// check bounds and reverse the opacity change direction
//
if ((newAlpha >= 1.0f) || newAlpha <= 0.15f) {
_ballAlphaChange *= -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment