I tried attempting to achieve the below animation using UIViewAnimationOptionTransitionFlipFromTop, it doesn't work and changes the image in no time. Following is the code I used:
[self.thanksButton setImage:[UIImage imageNamed:#"frame1_image"] forState:UIControlStateNormal];
[UIView animateWithDuration:3.0 delay:0 options:UIViewAnimationOptionTransitionFlipFromTop animations:^{
} completion:^(BOOL finished) {
[self.thanksButton setImage:[UIImage imageNamed:#"frame4_image"] forState:UIControlStateNormal];
}];
Also tried splitting the images into four different frames and applying those images one by one using nested animation like this,
[UIView animateWithDuration:3.0 animations:^{
[self.thanksButton setImage:[UIImage imageNamed:#"frame2_image"] forState:UIControlStateNormal];
} completion:^(BOOL finished) {
[UIView animateWithDuration:3.0 animations:^{
[self.thanksButton setImage:[UIImage imageNamed:#"frame3_image"] forState:UIControlStateNormal];
} completion:^(BOOL finished) {
[UIView animateWithDuration:3.0 animations:^{
[self.thanksButton setImage:[UIImage imageNamed:#"frame4_image"] forState:UIControlStateNormal];
} completion:^(BOOL finished) {
}];
}];
}];
Please let me know what needs to be fixed here. Following is the animation:
The setImage cannot be animated, just by putting in it the UIView animationblock, therefore the completionBlock doesn't not wait 3 seconds, but calls immediatly. To see an actual effect set some alpha on the self.thanksbutton like 0.3, 0.5, 1 ( i don't think it will look good, but you should see the effect)
Also, I would make 2 buttons so I could use the :
[UIView transitionFromView:firstButton
toView:theOtherButton duration:0.3f
options:UIViewAnimationOptionTransitionFlipFromBottom
completion:^(BOOL finished) {
}];
Related
I have a UIView which is initially hidden. I need to setHidden:NO (visible) with dropdown effect...
There's my simple code without effect
-(IBAction)btnAbrirDestaquesClick:(id)sender {
[self.viewDestaques setHidden:NO];
}
A simpler alternative to UIDynamicAnimator in iOS 7 is Spring Animation (a new and powerful UIView block animation), which can give you nice bouncing effect with damping and velocity:
[UIView animateWithDuration:duration
delay:delay
usingSpringWithDamping:damping
initialSpringVelocity:velocity
options:options animations:^{
//Animations
[self.viewDestaques setHidden:NO];
}
completion:^(BOOL finished) {
//Completion Block
}];
If you simply want to animate it try something like this:
[UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.viewDestaques.frame = CGRectMake(0, 0, 320,30);
} completion:^(BOOL finished) {
[UIView animateWithDuration:.5 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.viewDestaques.frame = CGRectMake(0, -30, 320,30);
} completion:^(BOOL finished) {
}];
}];
It worked for me:
-(IBAction)btnAbrirDestaquesClick:(id)sender {
[self.viewDestaques setTranslatesAutoresizingMaskIntoConstraints:YES]; //respeita o frame que eu setar, independentemente das constraints
[self.viewDestaques setFrame:CGRectMake(self.viewDestaques.frame.origin.x, self.viewDestaques.frame.origin.y, self.viewDestaques.frame.size.width, 0)];
[self.viewDestaques setHidden:NO];
while (self.viewDestaques.frame.size.height < self.frameViewDestaquesOriginal.size.height) {
[UIView animateWithDuration:2.0 animations:^{
[self.viewDestaques setFrame:CGRectMake(self.viewDestaques.frame.origin.x, self.viewDestaques.frame.origin.y, self.viewDestaques.frame.size.width, self.view.frame.size.height + 10)];
}completion: ^(BOOL completed){
}];
}
}
I have six button on view with two pair of two grid.The problem is how to show button with animation, here I attach example url how to create this type of animation.
In this example when you click on menu button then show button in side menu view with animation
https://github.com/djardon/SideMenuFrostedAnimated
I tried this but I can't get any success.
I have two button one is btnHome and second is btnAbout:
-(void)animationStart
{
[btnHome setAlpha:0.f];
[UIView animateWithDuration:1.f delay:0.f options:UIViewAnimationOptionCurveEaseIn animations:^{
[btnHome setAlpha:0.f];
} completion:^(BOOL finished) {
[UIView animateWithDuration:2.f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^{
[btnHome setAlpha:1.f];
[self animationStart2];
} completion:nil];
}];
}
-(void)animationStart2
{
[btnAbout setAlpha:0.0f];
//fade in
[UIView animateWithDuration:2.0f animations:^{
[btnAbout setAlpha:1.0f];
} completion:^(BOOL finished) {
//fade out
[UIView animateWithDuration:2.0f animations:^{
[btnAbout setAlpha:0.0f];
} completion:nil];
}];
}
Try this.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
btnHome.alpha=0.0;
btnAbout.alpha=0.0;
}
-(void)animationStart
{
[UIView animateWithDuration:0.4f delay:0.0f options: UIViewAnimationOptionCurveEaseInOut animations:^{
btnHome.alpha=1.0;
} completion:^(BOOL finished) {
[self animationStart2];
}];
}
-(void)animationStart2
{
[UIView animateWithDuration:0.4f delay:0.0f options: UIViewAnimationOptionCurveEaseInOut animations:^{
btnAbout.alpha=1.0;
} completion:^(BOOL finished) {
}];
}
- (IBAction)btnClick:(id)sender
{
[self animationStart];
}
Here initially i am setting the buttons alpha value to 0.0f. On click of third button i am showing these two buttons.
I am trying to animate one of my image form bottom to Top of my screen , for that i am implementing an animation that the current width and height of image will become half during the transition stage and will take its original size once animation completes.
The issue is once the 1st animation is done it hold's for a while and then starts another animation , i want a smooth animation .
Here is the code :
[UIView animateWithDuration:1.0
delay:0.5
options:UIViewAnimationOptionTransitionCurlUp
animations:^{
[_myImageView setFrame:CGRectMake(20.0, 220.0, currentframe.size.width/2, currentframe.size.height/2)];
} completion:^(BOOL finished)
{
if (finished)
{
[UIView animateWithDuration:1.0
delay:0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
[_myImageView setFrame:CGRectMake(20.0, 20.0, currentframe.size.width, currentframe.size.height)];
} completion:^(BOOL finished) {
if (finished) {
NSLog(#"2 nd log ");
}
}];
NSLog(#"animation fisnished !!");
}
}];
What should be done ?
try this:
[UIView animateWithDuration:1.0
delay:0
options:UIViewAnimationOptionCurveLinear// use CurveLinear option
animations:^{
} completion:^(BOOL finished) {
}];
I have three UIViews that i want to animate in a certain order (It is supposed to look like a news ticker with three different labels moving from above then display and then move all the way to the outside of the view). Now this whole process is supposed to repeat infinitely.
This is the code i used:
[UIView animateWithDuration:5.0 delay:0 options:UIViewAnimationOptionRepeat| UIViewAnimationOptionCurveLinear animations:^
{
[morningPrayerView setFrame:CGRectMake(0, 0, 320, 30)];
[morningPrayerView setAlpha:1.f];
}
completion:^(BOOL finished)
{
[UIView animateWithDuration:5.0 delay:0 options:UIViewAnimationOptionRepeat| UIViewAnimationOptionCurveLinear animations:^{
[morningPrayerView setFrame:CGRectMake(0, 30, 320, 30)];
[morningPrayerView setAlpha:1.f];
} completion:^(BOOL finished) {
{
[UIView animateWithDuration:5.0 delay:0 options:UIViewAnimationOptionRepeat| UIViewAnimationOptionCurveLinear animations:^{
[middayPrayerView setFrame:CGRectMake(0, 0, 320, 30)];
[middayPrayerView setAlpha:1.f];
} completion:^(BOOL finished) {
{
[UIView animateWithDuration:5.0 delay:0 options:UIViewAnimationOptionRepeat| UIViewAnimationOptionCurveLinear animations:^{
[middayPrayerView setFrame:CGRectMake(0, 30, 320, 30)];
[middayPrayerView setAlpha:1.f];
} completion:^(BOOL finished) {
{
[UIView animateWithDuration:5.0 delay:0 options:UIViewAnimationOptionRepeat| UIViewAnimationOptionCurveLinear animations:^{
[eveningPrayerView setFrame:CGRectMake(0, 0, 320, 30)];
[eveningPrayerView setAlpha:1.f];
} completion:^(BOOL finished) {
{
[UIView animateWithDuration:5.0 delay:0 options:UIViewAnimationOptionRepeat| UIViewAnimationOptionCurveLinear animations:^{
[eveningPrayerView setFrame:CGRectMake(0, 30, 320, 30)];
[eveningPrayerView setAlpha:1.f];
} completion:^(BOOL finished) {
//this block is called when the animation is over
}];
} }];
} }];
} }];
} }];
}];
The problem is that only the first animation gets the go and none of the other animations start.
IMHO the UIViewAnimationOptionRepeat switch means to repeat the animation indefinitely, therefore your first animation loops forever and never calls the completion handler. Simply wrap the whole animation setup in method, remove the repeat switches and when the last completion handler is executed, call the method again to repeat the whole show? Something like this:
- (void) animateView {
[UIView animateWithDuration:5.0 animations:^{
// …
} completion:^(BOOL finished) {
[self animateView];
}];
}
That’s probably a retain cycle, but as long as you cancel the animation eventually, it should be OK.
I've only worked a bit with UIView animations and blocks, basically one step animations. I'd like to stack a few steps into a sequence. The code below seems to be working but I'm wondering if this is the right approach and/or if there are any issues/limitations with placing blocks within blocks.
Blocks seem great though the code formatting gets sort of unwieldy/unreadable.
CGRect newFrame = CGRectMake(0, 0, 500, 500);
UIView *flashView = [[UIView alloc] initWithFrame:newFrame];
flashView.tag = 999;
[flashView setBackgroundColor:[UIColor grayColor]];
[flashView setAlpha:0.f];
[self.view addSubview:flashView];
[UIView animateWithDuration:.2f
animations:^{
// STEP 1: FADE IN
[flashView setAlpha:1.f];
}
completion:^(BOOL finished){
[UIView animateWithDuration:.9f
animations:^{
// STEP 2: FADE OUT
[flashView setAlpha:0.f];
}
completion:^(BOOL finished){
// STEP 3: CLEAN UP
[flashView removeFromSuperview];
}
];
}];
What you're doing is correct. Sometimes the nesting can get ugly. One alternative for readability is to put each animation in its own method:
-(IBAction)someButtonPressed:(id)sender {
[self saveSomeData];
[self fadeInAndOut];
}
-(void)fadeInAndOut {
[UIView animateWithDuration:.2f
animations:^{
// STEP 1: FADE IN
[self.flashView setAlpha:1.f];
}
completion:[self fadeOut]
];
}
-(void (^)(BOOL))fadeOut {
return ^(BOOL finished) {
[UIView animateWithDuration:.9f
animations:^{
// STEP 2: FADE OUT
[self.flashView setAlpha:0.f];
}
completion:[self cleanUpFlashView]
];
};
}
-(void (^)(BOOL))cleanUpFlashView {
return ^(BOOL finished){
// STEP 3: CLEAN UP
[self.flashView removeFromSuperview];
};
}
That way isn't horrible if you're just nesting once with simple code. If you're going to do something more complex you might try animateWithDuration:delay:options:animations:completion:
using delay: as a way to link the animations. For example:
[UIView animateWithDuration:.2f delay:0
options:UIViewAnimationOptionCurveEaseIn|UIViewAnimationOptionAllowUserInteraction
animations:^{
// STEP 1: FADE IN
[flashView setAlpha:1.f];
}
completion:nil
];
[UIView animateWithDuration:.9f delay:.2f
options:UIViewAnimationOptionCurveEaseIn|UIViewAnimationOptionAllowUserInteraction
animations:^{
// STEP 2: FADE OUT
[flashView setAlpha:0.f];
}
completion:^(BOOL finished){
// STEP 3: CLEAN UP
[flashView removeFromSuperview];
}
];