Animate button from right to left - ios

I have a swipe gesture on UITableViewCell, when it happens a button appears
I'd like to add animation to that button, animation should reveal the button from left to right.
What do I need to add to the following code, so the button will appear/fade in from left to right?
// set the original frame
button.frame = CGRectMake(30, 50, 100, 100);
// animate
[UIView animateWithDuration:0.75 animations:^{
button.frame = CGRectMake(10, 70, 100, 100);
}];

This is how you can do it:
UIButton* animatingButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 200, 100, 100)];
[animatingButton setTitle:#"text" forState:UIControlStateNormal];
[self addSubview:animatingButton];
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
animatingButton.frame = CGRectMake(220, 200, 100, 100);
} completion:^(BOOL finished) {
// your animation finished
}];

If you want to programmatically add a UIButton you have to first create the object and then add it in a UIView. After that you can do animations with it.

Related

Disable font animation in UIButton [duplicate]

This question already has answers here:
How to stop unwanted UIButton animation on title change?
(24 answers)
Closed 4 months ago.
I am animating a view that is sliding down. In the view there is a simple UIButton. The view and the button have fixed widths and heights (I checked with adding a color as background). Although when use:
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
//Slide Down the notification
CGRect notificationsRect = self.notificationContainerView.bounds;
notificationsRect.origin.y = 0;
notificationViewController.view.frame = notificationsRect;
} completion:^(BOOL finished) {
[notificationViewController didMoveToParentViewController:self];
self.currentNotification = notificationViewController;
}];
then the view slides down perfectly, expect for that the text in the UIButton kind of fades in. It starts really small and animates to the correct font size.
How can I disable the animation of the text in the UIButton?
Is the effect you have something similar to what is below? I tried to imitate your situation by having a view with a UIButton inside with some some text set for the title of the button. I then animated the button similarly to the way you did. As you can see there isn't really a fade happening on the text of my button inside my view that is sliding so I wonder if there is something else at play for you. I have the code I used below also to mock the situation.
- (void)viewDidLoad {
[super viewDidLoad];
self.myView = [[UIView alloc] initWithFrame:CGRectMake(200, 0, 100, 100)];
self.myView.backgroundColor = [UIColor greenColor];
UIButton *myButton= [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 80, 80)];
myButton.backgroundColor = [UIColor blueColor];
[myButton setTitle:#"fun times" forState:UIControlStateNormal];
[self.myView addSubview:myButton];
[self.view addSubview:self.myView];
UIButton *resetButton = [[UIButton alloc] initWithFrame:CGRectMake(300, 300, 50, 50)];
resetButton.backgroundColor = [UIColor blackColor];
[resetButton addTarget:self action:#selector(reset) forControlEvents:UIControlEventTouchUpInside];
UIButton *goButton = [[UIButton alloc] initWithFrame:CGRectMake(300, 100, 50, 50)];
goButton.backgroundColor = [UIColor redColor];
[goButton addTarget:self action:#selector(go) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:goButton];
[self.view addSubview:resetButton];
}
- (void)reset {
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
//Slide Up the notification
CGRect notificationsRect = self.myView.bounds;
notificationsRect.origin.y = 0;
notificationsRect.origin.x = 200;
self.myView.frame = notificationsRect;
} completion:nil];
}
- (void)go {
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
//Slide Down the notification
CGRect notificationsRect = self.myView.bounds;
notificationsRect.origin.y = 200;
notificationsRect.origin.x = 200;
self.myView.frame = notificationsRect;
} completion:nil];
}
Disable animations for UIButton using setAnimationsEnabled:(BOOL)enabled
Reference:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/index.html#//apple_ref/occ/clm/UIView/setAnimationsEnabled:

UIButton show and disappear with animation

I would like to create a button that will show for only 5 second and then disappear. User have to click the button before it disappear. So I use animationWithDuration to set alpha to 1 and then set it back to 0. Here is my code...
__block UIButton *showButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 150, 40)];
[showButton setTitle:#"go to next page!" forState:UIControlStateNormal];
showButton.alpha = 0;
[showButton addTarget:self action:#selector(showButtonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:showButton];
[UIView animateWithDuration:1
delay:0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
showButton.alpha = 1.0;
}
completion:nil];
[UIView animateWithDuration:0.5
delay:3.0
options: UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction
animations:^{
showButton.alpha = 0.0;
}
completion:^(BOOL finished){
[showButton removeFromSuperview];
showButton = nil;
}];
-(void)showButtonClick{
NSLog(#"click")
}
But the showButtonClick can't get called. What am I do wrong?
Try this code:
UIButton *showButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 150, 40)];
[showButton setTitle:#"go to next page!" forState:UIControlStateNormal];
showButton.alpha = 0;
[showButton addTarget:self action:#selector(showButtonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:showButton];
[UIView animateWithDuration:1
delay:0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
[showButton setHidden:NO];
showButton.alpha = 1.0;
}
completion: {
[UIView animateWithDuration:0.5
delay:0
options: UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction
animations:^{
showButton.alpha = 0.0;
}
completion:^(BOOL finished){
[showButton setHidden:YES];
}];
}];
-(void)showButtonClick{
NSLog(#"click")
}
You should create a NSTimer or something else to call the animation. Because both of your animation are being called at the same time. That is why you were not able to click the button, thus showButtonClick wasn't being call.
[UIView animateWithDuration:5.0
animations:^{
initWithFrame:CGRectMake(100, 0, 150, 40)
//Animation code goes here
} completion:^(BOOL finished) {
//Code to run once the animation is completed goes here
[your button remove from removefromsuperview];
}];
I can't understand,why you are using animation two times.....as your button is frame is allocated as initWithFrame:CGRectMake(0, 0, 150, 40).....check the animation code above....it will animate the button from 0 to 100....after animation completed the button will be removed...

Show banner from top of the screen

I want to create a turn-based match. When player received a notification player:receivedTurnEventForMatch:didBecomeActive:
I want to show a banner that slides from the top of the screen. "Your turn. Play" similar to the banner that Game Center shows when player is authenticated.
How can I do it? Should it be a UIViewController or UIAlert, or what?
here is a good example for notification for what you want, i hope that help you:
https://github.com/ekurutepe/MPNotificationView
or
https://github.com/edgurgel/CMNavBarNotificationView
or
https://github.com/terryworona/TWMessageBarManager
There are MANY ways to do what you mentioned. This is just one of them. Feel free to change the code around and read up on UIView animations to you can try out different things yourself.
-(void)myMethod {
// create UILabel and set its properties
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 320, 50)];
myLabel.backgroundColor = [UIColor grayColor];
NSString *myLabelText = #"Welcome back, Mike Lyman"; // showing you to use NSString for label text
myLabel.textAlignment = NSTextAlignmentCenter;
myLabel.text = myLabelText;
[self.view addSubview:myLabel]; // add UILabel to view
myLabel.frame = CGRectMake(0, -50, 320, 50); // set text label frame offscreen
// start the animation to slide UILabel into visible view
[UIView animateWithDuration:0.5 delay:0
options:UIViewAnimationOptionCurveLinear
animations:^ {
myLabel.frame = CGRectMake(0, 20, 320, 50);
}
completion:^(BOOL finished) {
// after 1 second delay, start sliding UILabel out of visible view
[UIView animateWithDuration:0.5 delay:1
options:UIViewAnimationOptionCurveLinear
animations:^ {
myLabel.frame = CGRectMake(0, -50, 320, 50);
}
completion:^(BOOL finished) {
NSLog(#"Done");
[self.view removeFromSuperview]; // remove UILabel from view completely
}];
}];
}

Creating a custom view with an animation

Im trying to make a custom view where one of it's elements(a UILabel) has animation that when a button of the custom view is tapped the label should expand and then contract. The first part of the animation happens correctly but not the second. Im using this method.
[UIView animateWithDuration:1.3 animations:^{
CGRect frame = CGRectMake(50, 15, 140, 20);
[labelFav setFrame:frame];
} completion:^(BOOL finished) {
//[labelFav setHidden:YES];
[UIView animateWithDuration:1.3 animations:^{
CGRect frame = CGRectMake(50, 15, 0, 20);
[labelFav setFrame:frame];
}];
}];
I believe it's a problem with how the methods Refresh and LayoutSubviews are implemented.
I did it following this tutorial, ray wenderlinch
The problem comes when after the first animation is called, LayoutSubviews is called, so all the items are set to the initial position, and when the first animation is finished the second one starts but the items are set to the original state, so I believe the animation doesnt happen because there's been a change between animations.
I would like to know how to implement this correctly, can you lend me a hand?
Thanks.
PS: This is a second question about the same problem but with a different angle.my previous question
[UPDATE]
This is my code for LayoutSubviews:
- (void)layoutSubviews {
[super layoutSubviews];
if(_idEvent == 0) return;
_buttonFav = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
[_buttonFav setImage:[UIImage imageNamed:#"fav.png"] forState:UIControlStateNormal];
[_buttonFav addTarget:self action:#selector(pressedFavButton:) forControlEvents:UIControlEventTouchDown];
_labelButton = [[UILabel alloc]initWithFrame:CGRectMake(0, 45, 0, 20)];
_labelButton.text = #"LABEL";
[self addSubview:_labelButton];
[self addSubview:_buttonFav];
}
Which is called right before the second animation. My refresh method is:
- (void)refresh {
if(selected){
[_buttonFav setImage:[UIImage imageNamed:#"fav_sel.png"] forState:UIControlStateNormal];
}
else{
[_buttonFav setImage:[UIImage imageNamed:#"fav.png"] forState:UIControlStateNormal];
}
}
Where I just set the image for the button depending on an attribute that lets me know if the button was tapped.
I do, how to make an animation with Auto-layout then? With the new
XCode you dont have an a option to disable it.
Add a width constraint to a label. Then create IBOutlet to this constraint in your implementation file (see the section 3. here, if you don't know how to do that), let's say something like:
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *buttonWidthConstraint;
Then you can animate it this way:
[UIView animateWithDuration:1.3 animations:^{
self.buttonWidthConstraint.constant = 140;
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
[UIView animateWithDuration:1.3 animations:^{
self.buttonWidthConstraint.constant = 0;
[self.view layoutIfNeeded];
}];
}];
use SetTransform and CGAffineTransform
try this out
[UIView animateWithDuration:1.3 animations:^{
[labelFav setTransform:CGAffineTransformMakeScale(0.5,0.5)];
} completion:^(BOOL finished) {
[UIView animateWithDuration:1.3 animations:^{
[labelFav setTransform:CGAffineTransformMakeScale(1.0,1.0)];
}];
}];
Try to encapsulate the UILabel into a UIView and animate that view.
UIView *auxView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 100.0)];
[auxView addSubview:labelFav];
[UIView animateWithDuration:1.3 animations:^{
CGRect frame = CGRectMake(50, 15, 140, 20);
[auxView setFrame:frame];
} completion:^(BOOL finished) {
//[labelFav setHidden:YES];
[UIView animateWithDuration:1.3 animations:^{
CGRect frame = CGRectMake(50, 15, 10, 20);
[auxView setFrame:frame];
}];
}];

How to remove SubView with slide in iOS?

I tried to add SubView with animation following codes.It's okay.
[self.view addSubview:pickerView];
pickerView.frame = CGRectMake(0, 0, 320, 50);
[UIView animateWithDuration:1.0
animations:^{
pickerView.frame = CGRectMake(0, 152, 320, 260);
}];
And i also want to remove the subView with slide animation like above animation.
How can i do that?
Thanks in advance.
you could use this animateWithDuration completion block to remove the view
[UIView animateWithDuration:1.0
animations:^{
pickerView.frame = //move it out of screen
} completion:^(BOOL finished) {
[pickerView removeFromSuperView];
}];
[UIView animateWithDuration:.2 animations:^{
pickerView.frame = CGRectMake(0, 0, 320, 50);
} completion:^(BOOL finished){
[pickerView removeFromSuperView];
}];

Resources