I have a view where the user can draw. What I want is that, the strokes that were drawn by the user must slowly fade-out in the order that they were drawn. Any advice ?
JYou can try pluging in a AnimationFadeOut and also set a duration to it. Here is a code snippet for that. Let me know if this helps you out.
-(void)fadeOut:(UIView*)viewToDissolve withDuration:(NSTimeInterval)duration andWait:(NSTimeInterval)wait
{
[UIView beginAnimations: #"Fade Out" context:nil];
// wait for time before begin
[UIView setAnimationDelay:wait];
// druation of animation
[UIView setAnimationDuration:duration];
viewToDissolve.alpha = 0.0;
[UIView commitAnimations];
}
Rather than uiview you can use your action to fade out.
For what you're asking, you could simply add a stroke as a subview, then use a cross fade transition.
[drawingView addSubView:strokeView];
[UIView transitionWithView:strokeView duration:0.2f options:UIViewAnimationOptionTransitionCrossDissolve animations:NULL completion:NULL];
For something like this (showing a drawing being completed), I would personally capture the touch points and time offsets to animate the drawing of each point at the speed of the drawer. More code, but a stylistic choice.
Related
I am animating UIView when user touches on custom dropdown to show picker View from bottom side.UIView contains Pickerview...so when I change it's frame to move upwards.I get what I want, but pickerView doesn't recognize touch !(see screenshot below)
code is like this
CGRect pickerFrame=self.pickerSheet.frame;
CGRect viewFrame=self.view.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:SHEET_ANIMATION_DURATION];
//change Frame of View containing UIPickerView
pickerFrame.origin.y=202+animatedDistance;
viewFrame.origin.y-=animatedDistance;//animated distance is value by which view needs to move upward.
[self.pickerSheet setFrame:pickerFrame];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
screenshot
first of all, ++please**, stop using this animation declaration, and bedin use new, block animation:
[UIView animateWithDuration:SHEET_ANIMATION_DURATION
delay:0
options:nil
animations:^{
pickerFrame.origin.y=202+animatedDistance;
viewFrame.origin.y-=animatedDistance;
}
completion:^(BOO f){
//any actions after animation ended
}];
Second, I think your problem is somewhere else. May by, you implementing your touches or picker wrong.
Please start using the new , better animation block , but I think there is something missing in your code , you need to put the code below :
UIViewAnimationOptionAllowUserInteraction
Put this code in options in the new animation block , or find a way to put it in the old animation block
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.30f];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:viewSettings cache:YES];
viewSettings.alpha = 0;
[viewSettings removeFromSuperview];
[UIView commitAnimations];
I ve written the code above that works well when I add the view via animation, but it doesn't work when i remove the view from superview. Animation works if I remove [viewSettings removeFromSuperview] line. I don't know where I'm doing wrong.
You need to remove it from the superview after the animation has completed. This is very easy to accomplish if you use the blocks based API, which Apple is encouraging you to do:
[UIView transitionWithView:viewSettings
duration:0.30f
options:UIViewAnimationOptionTransitionNone
animations:^{
[viewSettings setAlpha:0];
} completion:^(BOOL finished) {
[viewSettings removeFromSuperview];
}];
You can read about all the options in Apple's documentation.
removeFromSuperview is not an animatable action, so it is getting performed immediately. Once you commitAnimations, your view is no longer part of it's superview, so you can't see the animation, if it is still even happening.
If you want your animation to happen, then the view to get removed, call removeFromSuperview when the animation ends, such as in a selector specified with setAnimationDidStopSelector:.
Try removing view after the animation is completed. Initially alpha value of the view is 1 then, you apply the animation and make it 0. Now the view is still there but it is not visible. Once the animation is over then remove the view. I think it should work.
I think viewSettings is removed before you commit the animation.
Try inverting the two last lines.
Is it possible (at all) to load a XIB with custom AnimationTransition?
What I have done, is creating an animation that "Covers" the screen, and what I want is that after that animation has done playing, I would like it to display the new XIB.
I cant seem to find any proper solution to this...Any ideas?
To be more clear: Press a button --> Play Animation (cover screen) --> Load XIB.
Hello again! Yes, the last way you described is the way I am doing it. I have two UIViews (Might be wrong already there), that are placed off-bounds on each side, (like x.-160.y.0 and x.320y.0)
-(IBAction) leftDoor{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDelegate:self];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
closeDoor1.center = CGPointMake( closeDoor1.center.x +160, closeDoor1.center.y);
[UIView commitAnimations];
}
-(IBAction) rightDoor{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDelegate:self];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
closeDoor2.center = CGPointMake( closeDoor2.center.x -160, closeDoor2.center.y);
[UIView commitAnimations];
}
So, what I am going to do is not to "split" the current view and then open a new XIB, the effect I am searching for is a "closing door" effect, thats why I used UIView ( thought I place graphics on top of those, like two ImageViews). Then, for loading the new XIB...This is where im really puzzled. My first way of trying this out was to make three IBActions, including the two I mentioned above, and then apply all three (multiple actions) to a button. So for switching views I did something like this: `-(IBAction) newViewDisplay:(id)sender{
theView *newViewController = [[theView alloc]
initWithNibName:#"theView" bundle:nil];
[self.view addSubview:newViewController.view];
}
`
As you said, this might be over my head, but if I just got some directions, I´ll walk miles to make this work. It would really give my app a facelift. A huge thanks for taking time to answer my question, All the best/Andy
What are you covering the screen with?
Think of it this way, (it sounds like) you have 2 views, the old one and the new one that is stored in this xib. The animation is secondary.
You need to load the new view, then display it (can be off-screen), then animate it (move it) into the place you want it to go. If you want to split it into two parts, one at the bottom and one at the top of the screen, then meet in the middle, I think that's both complicated and above you skill level. (Not trying to be mean here).
If you are trying to do a split animation as described, it can be done, but you need to 'fake it'. It would involve taking a 'screenshot' (of sorts), splitting it, moving these two images so they meet with an animation, loading the view underneath, and then removing the images. Tricky stuff.
Do you have to have this sort of animation?
If you could post the code you have, I can rearrange it, and add to it, for you.
You'll need to clarify what it is exactly you want to do, though.
UPDATE 3: I added another option for autoreverse.
//You can add both doors into one animation block.
//Create an animation block. Ease Out is better for this animation but you can change it.
[UIView animateWithDuration:1.0 delay:0.0 options:(UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAutoreverse) animations:^{
closeDoor1.center = CGPointMake( closeDoor1.center.x +160, closeDoor1.center.y);
closeDoor2.center = CGPointMake( closeDoor2.center.x -160, closeDoor2.center.y);
}completion:^(BOOL finished){
if (finished) {
//When finished, create the VC if it doesn't exist, and insert it below the 'closed door'.
if (newViewController == nil) {
UIViewController *newViewController = [[UIViewController alloc] initWithNibName:#"theView" bundle:nil];
}
[self.view insertSubview:newViewController.view belowSubview: closeDoor2];;
[self.closeDoor1 removeFromSuperview];
[self.closeDoor2 removeFromSuperview];
}
}];
I have a subView that I want to toggle between hidden and not hidden by a button. How do I fade in the subview and fade it out? For now it just appears immediately and disappear immediately when I toggle the button.
I am wondering what is the easiest way to do this animation.
Thanks
On iOS 4.0+ Apple recommends you use their new, block-based animation methods. Using these, the code would look something like this:
[UIView animateWithDuration:2.0
animations:^{myView.alpha = 0.0;}];
The properties you are animating go inside the block (the ^{...} part). Blocks are sort of like functions, so you can put multiple lines of code inside of them, if you want to animate multiple properties. For example:
[UIView animateWithDuration:0.2
animations:^{
view.alpha = 0.0;
view.backgroundColor = [UIColor redColor];
}];
If you need to perform an action after the animation is complete, use the +animateWithDuration:animations:completion: method (which also uses blocks), for example:
[UIView animateWithDuration:0.2
animations:^{view.alpha = 0.0;}
completion:^(BOOL finished){ [view removeFromSuperview]; }];
For more info, check out the UIView Class Reference 'Animations' section and 'Animating Views with Blocks' section.
This is the old pre-4.0 way:
http://objcolumnist.com/2009/07/18/simple-uiview-based-animations-on-the-iphone/
... which has the advantage of being conceptually simple and easy to implement.
float alpha = 1.0; // or 0.0 if it's already visible and you want to fade out
[UIView beginAnimations:#"" context:NULL];
[UIView setAnimationDuration:2.0]; // seconds, not ms. guess how i know?
[mySubView setAlpha:alpha];
[UIView commitAnimations];
Is there a way to animate enabling or disabling a button? I've tried the following with no success. I'm guessing at this point that the enabled property cannot be animated like opacity can – but I hope I'm wrong.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0f];
theButton.enabled = YES;
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
I can't believe there isn't a setEnabled:(BOOL)enabled animated:(BOOL)animated method.
This is actually really easy. UIView.transitionWithView can take a snapshot of any view and cross-dissolve from the snapshot to the new state of the view. You can use that capability to animate a lot of changes, including enabled.
Just use the View Controller's view as the target view, specify TransitionCrossDissolve, then animate as you would normally.
UIView.transitionWithView(self.view,
duration: 0.5,
options: UIViewAnimationOptions.TransitionCrossDissolve,
animations: { () -> Void in
self.someButton.enabled = !self.someButton.enabled
}, completion:nil)
Animatable properties (through UIView beginAnimations:context:) are: frame, bounds, center, transform, alpha (from Apple docs).
So you've to determine what exactly your animation is (in terms of color/alpha/transform animations) and create manual animation (maybe you'll have to use CAAnimation instead of UIView beginAnimations:context:)
You can break down the enable/disable of a button into a change in opacity between two overlapping images each in one state or the other (i.e. make it look like a fade-in/fade-out). In the animation completion handler you can to do the actual enable/disable toggle.
Here's an idea :)
Within your animation block, Remove the button from view and add it again with disabled state. You can specify what kinda animation you want here..
Set: self.buttonSetEvent.customView.alpha = 0.2;
and try this
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.15];
[UIView setAnimationRepeatCount:2];
[UIView setAnimationRepeatAutoreverses:YES];
self.buttonSetEvent.customView.alpha = 1;
[UIView setAnimationDelegate:self];
[UIView commitAnimations];