Last active
August 29, 2015 13:56
-
-
Save aug2uag/9264193 to your computer and use it in GitHub Desktop.
Core Animation Basic Example
This file contains 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)viewDidLoad | |
{ | |
UIView* sampleView = [[UIView alloc] init]; | |
[self.view addSubview:sampleView]; | |
sampleView.backgroundColor = [UIColor redColor]; | |
// define center of view | |
CGPoint currentCenterPoint = self.view.center; | |
CGRect invisibleRect = CGRectMake(currentCenterPoint.x, currentCenterPoint.y, 0, 0); | |
NSArray* randomValueArray = @[@276.23, @839.32, @894.3, @263.4]; | |
NSNumber* randomValue = [randomValueArray objectAtIndex:arc4random() % randomValueArray.count]; | |
NSNumber* randomValue2 = [randomValueArray objectAtIndex:arc4random() % randomValueArray.count]; | |
NSNumber* randomValue3 = [randomValueArray objectAtIndex:arc4random() % randomValueArray.count]; | |
NSNumber* randomValue4 = [randomValueArray objectAtIndex:arc4random() % randomValueArray.count]; | |
NSLog(@"randomValue = %@", randomValue); | |
// origin x | |
// centerX - finalWidth/2 | |
// origin y | |
// centerY - finalHeight/2 | |
CGRect visibleRect = CGRectMake(currentCenterPoint.x - ([randomValue integerValue]/10)/2, currentCenterPoint.y - ([randomValue2 integerValue]/10)/2, [randomValue integerValue]/10, [randomValue2 integerValue]/10); | |
CGRect visibleRect2 = CGRectMake(currentCenterPoint.x - ([randomValue3 integerValue]/10)/2, currentCenterPoint.y - ([randomValue4 integerValue]/10)/2, [randomValue3 integerValue]/10, [randomValue4 integerValue]/10); | |
// 99% of animations are frame manipulations | |
sampleView.frame = invisibleRect; | |
double delayInSeconds = 2.0; | |
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); | |
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ | |
[UIView animateWithDuration:0.3f animations:^{ | |
sampleView.bounds = visibleRect; | |
} completion:^(BOOL finished) { | |
[UIView animateWithDuration:0.3f animations:^{ | |
sampleView.bounds = invisibleRect; | |
} completion:^(BOOL finished) { | |
[UIView animateWithDuration:0.3f animations:^{ | |
sampleView.bounds = visibleRect2; | |
} completion:nil]; | |
}]; | |
}]; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment