I am trying to create an animation where the view looks like it pops up to you. 'Animation Option 1' is exactly how I want the effect to look but performing a transformation before the frame change, messes up the frame (after view.transform=CGAffineTransformIdentity;
the views frame does not match the original size). 'Animation Option 2' is kind of the idea but looks bad. Any idea how I can make a transformation and change the views frame?
//ANIMATION OPTION 1
CGRect frame=view.frame;
view.transform=CGAffineTransformMakeScale(0.94f, 0.94f);
frame.origin.y=10.0f; //The view is originally off screen, this is to show
[UIView animateWithDuration:0.2f animations:^{
[view setFrame:frame];
}completion:^(BOOL finished){
[UIView animateWithDuration:0.2f animations:^{
view.transform=CGAffineTransformIdentity;//This pops the view up
}];
}];
//ANIMATION OPTION 2
CGRect frame=view.frame;
frame.size.width=296;
frame.size.height=296;
frame.origin.x=8;
frame.origin.y=8;
[UIView animateWithDuration:0.2f animations:^{
[view setFrame:frame];
}completion:^(BOOL finished){
CGRect frame=view.frame;
frame.size.width=300;
frame.size.height=300;
frame.origin.x=10;
frame.origin.y=10;
[UIView animateWithDuration:0.2f animations:^{
[view setFrame:frame];
}];
}];
You are changing the frame after the transform so identity is not working on the transform. Instead of changing the x and y use a translation on the UIView. My objective c is rusty but see if this is what you are thinking. I delay it so you can see the starting position and ending position.Also the pros of using translation instead of manipulating the frame is if you use AutoLayout you are not messing with the frame.
#import "ViewController.h"
@interface ViewController ()
@property UIView *testView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_testView = [[UIView alloc]init];
[_testView setFrame:CGRectMake(20, 100, self.view.bounds.size.width - 40, 100)];
[_testView setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:_testView];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
//just delaying it so you can see the start position
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
//code to be executed on the main queue after delay
self.testView.transform = CGAffineTransformConcat(CGAffineTransformMakeScale(0.94, 0.94), CGAffineTransformMakeTranslation(0, 10));
self.testView.alpha = 0;
[UIView animateWithDuration:1 delay:2 options:(UIViewAnimationOptionCurveEaseInOut) animations:^{
self.testView.transform=CGAffineTransformIdentity;//This pops the view up
self.testView.alpha = 1;
} completion:^(BOOL finished) {
}];
});
}
@end