Move the UIButton 10 pixels down and open a new ViewController

I am new to iOS development and this is my first question about SO.

I want to create an "animation".

When I press UIButton, I want to slowly (about 0.5 seconds) move it by 10 pixels, and when I raise my finger, I want to open a new viewController.

I did something, but I donโ€™t know how to make an โ€œanimationโ€.

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]; } 
+4
source share
3 answers
 [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 }]; 

eg:

 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~ }]; 
+3
source
 [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); }]; 
+1
source

You can put transformations (repositioning), scaling, and rotation between the beginAnimations and commitAnimations blocks, where you can specify AnimationRepeatCount, AnimationCurve, etc.

 [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]; 

But in iOS4 and later animations, it is recommended to perform

animateWithDuration: delay: options: animations: compeletion and similar methods that contain blocks that are very powerful. For more information on block animations, please refer to the Apple documentation.

0
source

Source: https://habr.com/ru/post/1387348/


All Articles