I have created a category on UITextField which allows to animate between red and white color as shown below:
#implementation UITextField (Additions)
-(void) flash
{
[UIView animateWithDuration:2.0 animations:^{
[self setBackgroundColor:[UIColor redColor]];
} completion:^(BOOL finished) {
[self setBackgroundColor:[UIColor whiteColor]];
}];
}
#end
I call it like this:
[self.paymentTextField flash];
First time it works and it shows the red background and then switches back to white color. But on second call it does not do anything.
I'm not sure why that doesn't work. I tried it and got the same thing. However, since the color doesn't really animate, you can do it like this instead:
self.backgroundColor = [UIColor redColor];
[self performSelector:#selector(setBackgroundColor:) withObject:[UIColor whiteColor] afterDelay:2];
This should give you the same look.
Related
I currently have a UIView that has UILabels sitting on top of it.
These UILabels have rounded corners.
I have a button which I press, which then programmatically removes all these labels, using the UIView transitionWithView method.
However, during the transition, the rounded corners are lost.
Is it possible to maintain these rounded corners during the transition?
i.e. The corners should remain rounded BEFORE, DURING and AFTER the transition.
Here's some example code:
#interface ExampleViewController
#property (strong, nonatomic) UIView *view;
#property (strong, nonatomic) UILabel *myLabel;
#end
#implementation ExampleViewController
- (void) viewDidLOad
{
self.myLabel = [[UILabel alloc] initWithFrame:self.view.frame];
[self.myLabel setText:#"Example Label"];
[self.myLabel setCenter:CGPointMake(100,100)]; // position the label somewhere on the screen
[self.myLabel.layer setCornerRadius:5]; // set the corner radius
[self.myLabel.layer setMasksToBounds:YES]; // found this particular line on another stackoverflow thread (http://stackoverflow.com/questions/11604215/uiview-transitionwithview-discarding-layer-settings)
[self.myLabel setHidden:NO];
[self.myLabel setBackgroundColor:[UIColor orangeColor]];
[self.myLabel setNumberOfLines:0];
[self.myLabel sizeToFit];
[self.view addSubview:self.myLabel];
}
// user interaction
- (IBAction)labelOff:(id)sender
{
BOOL hidden = [self.myLabel isHidden];
[UIView transitionWithView:self.myLabel
duration:1
options:UIViewAnimationOptionTransitionCrossDissolve
animations:NULL
completion:NULL];
[self.myLabel setHidden:!hidden];
}
#end
I am using XCode 5 and iOS 7. Any help is really appreciated.
I'm not sure about how the corner radius is getting removed during transition. But here is an alternative method you can use which has the same end result.
- (IBAction)labelOff:(id)sender
{
[UIView animateWithDuration:1.0f
animations:^{
self.myLabel.alpha = !self.myLabel.alpha;
}];
}
I hope that, you need to empty the UILabel.text but the properties should be avail. right?, Then
BOOL hidden; // Declare under #interface
- (IBAction)labelOff:(id)sender
{
if(hidden){
hidden =false;
[self.myLabel setText:#""];
}
else{
hidden =true;
[self.myLabel setText:#"Example Label"];
}
[UIView transitionWithView:self.myLabel
duration:1
options:UIViewAnimationOptionTransitionCrossDissolve
animations:NULL
completion:NULL];
}
my goal is to create a simple popover view over a bar button in my tool bar. So apparently Apple doesn't want me to create this on my iPhone. But i already saw something familiar on other apps.
So instead of using the UIPopOver thing from Apple, i like to create an UIView which appears when the bar button is clicked. And disappears when it is clicked again. It would be over the top if i also could create the little arrow which points to the button (you know which arrow i mean :D). But this is not relevant at this time. I just want to create this "popover appears and disappears with the same button". My "solution" right know is not a solution:
- (IBAction)buttonPressed:(id)sender
{
UIView *myPopOverView = [[UIView alloc] initWithFrame:CGRectMake(25, 25, 500, 250)];
myPopOverView.alpha = 0.0;
myPopOverView.layer.cornerRadius = 5;
myPopOverView.layer.borderWidth = 1.5f;
myPopOverView.layer.masksToBounds = YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(testNSLog)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show myPopOverView" forState:UIControlStateNormal];
button.frame = CGRectMake(10, 10, 100, 30);
[myPopOverView addSubview:button];
[self.view addSubview:myPopOverView];
[UIView animateWithDuration:0.4 animations:^{
[myPopOverView setAlpha:1.0];
} completion:^(BOOL finished) {}];
}
So i don't have a clue on how to do that with this code fragment. It appears right away as i want. But i don't know how to create this toggle. I tried with a simple Bool-If-Toggle, but it didn't work. And i don't really know how to remove it from my view either.
Can you guys help me with this?
Thanks in advance!
Ok i solved it.
i defined a UIView in the .h-File and instantiate it in ViewDidLoad, then i did:
- (IBAction)buttonPressed:(id)sender
{
if (showedOptions == NO) {
[self.view addSubview:self.popoverView];
[UIView animateWithDuration:0.4 animations:^{
[self.popoverView setAlpha:1.0];
} completion:^(BOOL finished) {}];
showedOptions = YES;
return;
} else {
showedOptions = NO;
[self.popoverView removeFromSuperview];
}
}
I have a view I change the background of as the state of the application changes. Basically I want the view background changes to animate in some sort of a slide. I've been looking for a solution, but haven't really found anything.
So I want to have a transition from whatever the current background is to the next one.
//current to:
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
I have no trouble getting the background to change but I would like to animate it. Any help in the right direction would be greatly appreciated thanks.
You can use this:
[UIView animateWithDuration:1
delay:0
options:UIViewAnimationOptionAutoreverse
animations:^
{
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
}
completion:nil
];
a very easy way to handle it:
such as yourView.backgroundColor = [UIColor redColor];
Then use this to animate change the color
#import <QuartzCore/QuartzCore.h>
yourView.backgroundColor = [UIColor blueColor];
CATransition *transiton = [[CATransition alloc] init];
transiton.duration = .5;
[yourView.layer addAnimation:transiton forKey:#"Animation"];
Just a simple animation block:
[UIView animateWithDuration:time animations^{
self.view.backgroundColor = [UIColorWithPatternImage:image];
}];
this just animates the transfer, taking time seconds.
I wrote a custom UITableViewCell - image view, button and three labels now I am trying to add some animations to it. So once I tap the button, it fades away and the spinner replaces the button. After two seconds the cell is overlaid with a red color, as a subview of cell fades in, then the indicator is removed and and red overlay starts fading back out. As well the button I previously removed fades back in.
(I could not phrase it a better way :P )
The method is :
-(void)rentButtonPressed:(id)sender
{
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[indicator startAnimating];
indicator.center = self.rentButton.center;
[UIView animateWithDuration:0.2
animations:^{self.rentButton.alpha = 0.0;}
completion:^(BOOL finished){
[self.rentButton removeFromSuperview];
[self addSubview:indicator];
}
];
UIView *overlay = [[UIView alloc] initWithFrame:self.backgroundImage.frame];
overlay.alpha = 0.0;
overlay.backgroundColor = [UIColor redColor];
[self.contentView addSubview:overlay];
[UIView animateWithDuration:0.4
delay:2.0
options:UIViewAnimationCurveEaseInOut
animations:^{
[indicator removeFromSuperview];
overlay.alpha = 0.4;
}
completion:^(BOOL finished){
[UIView animateWithDuration:0.4
animations:^{ overlay.alpha = 0.0; }
completion:^(BOOL finished)
{
[overlay removeFromSuperview];
}
];
[self.contentView addSubview:self.rentButton];
[UIView animateWithDuration:0.4 animations:^{ self.rentButton.alpha = 1.0;}];
[self.delegate didTryToRentMovieAtCell:self];
}
];
}
So the code does fade out the button, replace it with spinner and fades in the red overlay. The problem is, the red overlay does not fade away, but disappears same with the button, instead of fading in, it just appears.
During your animation, you are changing the view hierarchy by adding and removing subviews. The UIView class method animateWithDuration:animations:completion is intended only animating property changes in a view, and not for changing the view hierarchy.
Try using the UIView class method transitionWithView:duration:options:animations:completion: instead, and use the cell's content view as the "container."
This documentation is helpful in distinguishing between animating view property changes and animating view transitions, specifically the section "Changing the Subviews of a View":
http://developer.apple.com/library/ios/#documentation/windowsviews/conceptual/viewpg_iphoneos/animatingviews/animatingviews.html
I have a label that gets updated with values calculated from information received by the server. Depending on how much traffic there is the updates can come in rapidly or at random intervals. What I would like to do is compare the new calculated value with the old value and if it has increased then change the text to be green and the background to be a darker green and if the value has decreased then change the text to a shade of red and dark red.
That part is easy to do, what I am having some trouble is after a half a second or so I would like to change the background and text to be their default colors. I'm doing this as part of user feedback for when the values change.
Any information would be greatly appreciated.
A label is a subclass of UIView. Assuming you have some method that notifies you of when the change occurs...
- (void)someMethodThatNotifiesOfChange {
// Calculate new values and assume the result means color button green
[UIView animateWithDuration:0.5 animations:^{
label.backgroundColor = [UIColor greenColor];
} completion:^(BOOL finished) {
if (finished) {
[UIView animateWithDuration:0.5 delay:5.0 options:nil animations:^{
label.backgroundColor = [UIColor clearColor];
} completion:nil];
}];
}
This just changes the background to be green, but illustrates the point none-the-less. This take 0.5 seconds to animate to green. Then, when it is complete, it waits 5 seconds and then takes 0.5 to animate back to clear.
You can use CoreAnimation library if you want advanced animation but if you just animate UILabel properties, you can use UIView static method to do this. You can do it like this:
[UIView animateWithDuration:0.3f animations:^{
label.textColor = [UIColor greenColor];
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.3f animations:^{
label.textColor = [UIColor blackColor];
} completion:nil];
}];
In first animation block, you set label's text color (and background color if you want). In its completion block you set the label's color back using another animation.
Hope it helps.
Take a look at NSTimer. Timers are useful to lots of situations!
Good luck!
Alternatively by IOS 4 the followwing block would do the same effect
[UIView transitionWithView: infoLabelInternet duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
infoLabelPremium.textColor = kPopoverActiveTextColor;
} completion:nil];
You can also do it this way:
label.backgroundColor = [UIColor greenColor];
[label performSelector:#selector(setBackgroundColor:) withObject:[UIColor clearColor] afterDelay:0.5];