Created
April 25, 2011 10:30
-
-
Save ahmetardal/940348 to your computer and use it in GitHub Desktop.
EyeballAnimation Demo App
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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