Переместите UIButton 10px вниз и откройте новый ViewController - PullRequest
4 голосов
/ 21 декабря 2011

Я новичок в разработке для iOS, и это мой первый вопрос по SO.

Я хочу создать «анимацию».

Когда я нажимаю UIButton, я хочу медленно (около 0,5 секунды) переместить его вниз на 10 пикселей, а когда я поднимаю палец, я хочу открыть новый viewController.

Я сделал что-то , но я не знаю, как сделать "анимацию".

    UIImage *normalImage = [UIImage imageNamed:@"FirstImage"];        
    UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    firstButton.frame = CGRectMake(31, 194, normalImage.size.width, normalImage.size.height);
    [firstButton setImage:normalImage forState:UIControlStateNormal];
    [firstButton addTarget:self action:@selector(showSecondViewController) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:firstButton];


- (void)showSecondViewController; {
    SecondViewController *secondViewController = [[SecondViewController alloc] init];
    secondViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController:secondViewController animated:YES];
    [progressViewController release]; 
}

Ответы [ 3 ]

3 голосов
/ 21 декабря 2011
[UIView animateWithDuration:(NSTimeInterval)duration animations:^(void) {
    //put your animation result here
    //then it will do animation for you
} completion:^(BOOL finished){
    //do what you need to do after animation complete
}];

например:

UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

//add firstButton to some view~
//....

firstButton.transform = CGAffineTransformIdentity;
[UIView animateWithDuration:0.5 animations:^(void) {
    firstButton.transform = CGAffineTransformMakeTranslation(0,10);
} completion:^(BOOL finished){
    show your view controller~
}];
1 голос
/ 21 декабря 2011
[UIView animateWithDuration:0.5 animations: ^ {
  button.frame = CGRectMake:(button.frame.origin.x, button.frame.origin.y + 10.0f, button.frame.size.width, button.frame.size.height);
}];
0 голосов
/ 21 декабря 2011

Вы можете поместить преобразования (репозиционирование), масштабирование и вращение между блоками beginAnimations и commitAnimations, где вы можете указать AnimationRepeatCount, AnimationCurve и т. Д.

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelay:0.5];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];

[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationRepeatCount:2];
..........................//etc.

firstButton.frame = CGRectMake(31, 194, normalImage.size.width, normalImage.size.height);
[UIView commitAnimations];

Но в iOS4 и более поздних анимациях рекомендуется выполнять

animateWithDuration: задержка: параметры: анимация: конкуренция и аналогичные методы, которые задействуют очень мощные блоки.Для получения дополнительной информации о анимации блоков, пожалуйста, обратитесь к документации Apple

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...