#iphone #animation
#iPhone #Анимация
Вопрос:
Как мне создать анимацию «сверху вниз» или «снизу вверх» при нажатии кнопки «Добавить» (плюс) на iPhone?
Ответ №1:
Если вы ищете «Встроенный» API, Алекс прав. И это работает отлично.
Если вы хотите понять, как создавать свои собственные анимации, у вас есть несколько вариантов в Core Animation
но отличной отправной точкой является просто UIView animateWithDuration
вам также следует ознакомиться с CGAffineTransform
собрав все это вместе, вот пример перемещения вида на экран из нижнего правого угла с одновременным его исчезновением. (Я действительно не знаю ситуации, когда я хотел бы это сделать, но это пример).
Я поместил это в делегат приложения, чтобы его было легко просто «вставить» в новый проект. Обычно вы бы не помещали какие-либо виды такого рода непосредственно в окно.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UIWindow *w = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];;
self.window = w; //property defined in the .h file
[w release];
UIView *v = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
v.backgroundColor = [UIColor redColor];
v.center=self.window.center;
v.alpha=0;
[self.window addSubview:v];
//if you ran the code to here, you would see a square in the center of the window.
//now lets move the square off (off screen) with a transform and then animate it back (on screen)
CGAffineTransform t = CGAffineTransformMakeTranslation(160 50, 240 50); //place the view just off screen, bottom right
v.transform=t;
//if you ran the code to here, you wouldnt see anything as the squre is placed just off screen, bottomr right
[UIView animateWithDuration:.5 //make the animation last .5 seconds
delay:.3 //wait .3 seconds before performing the animation
options:UIViewAnimationOptionCurveEaseOut //slow down as it reaches its destination
animations:^
{
v.transform=CGAffineTransformIdentity; //reset any transformation
v.alpha=1.0; //fade it in
}
completion:^(BOOL finished){} //nothing to do when it completes
];
[self.window makeKeyAndVisible];
return YES;
}
Ответ №2:
Я думаю, вы думаете о presentModalViewController; вы можете использовать его для представления нового экрана, который скользит вверх от нижней части экрана.