UIView animation is leaking - iphone

In my viewcontroller i am using animation for changing the frame of UIButton and UIView, going from portrait to landscape user can see views growing but problem is animation is leaking everywhere and showing everything coming from different sides.
Here is the code
[UIView beginAnimations:Nil context:Nil];
[UIView setanimationDuration:1];
[view1 setFrame:CGRectMake(100,100,200,300)];
[UIView commitAnimations];
Thanks

Please elaborate more on what "leaking everywhere" means, and does it make a difference if you use block animation:
[UIView animateWithDuration:1.0 delay:0.0 options:nil animations:^{
[view1 setFrame:CGRectMake(100,100,200,300)];
}completion:^(BOOL done){
if (done) {
NSLog(#"animation complete");
}
}];
According to Apple's documentation:
In iOS 4 and later, use the block-based animation methods.
(Recommended)

Related

How animate a view in with a flip animation

This is my current code, but it seems to just be flipping the view - I want it to flip-in i.e. flip for not seeing it to seeing it. I am doing this in the viewdidload method:
[UIView animateWithDuration:1.5 delay:0.0 options:UIViewAnimationOptionOverrideInheritedDuration animations:^{
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.alertView cache:YES];
}completion:^(BOOL finished)
{
}];
Use the transitionFromView:toView:duration:options:completion: method with a full transpatent view as 'FromView' and your alertView as toView

UIViews not animating when changing between each other

I have 2 views which are the same size, with colourPreview set as a subview of self.view. This is my code for showing the animation:
-(void)startEditingHexLabel {
[UIView animateWithDuration:0.3
animations:^{
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:hexTextView cache:YES];
[colourPreview removeFromSuperview];
[self.view addSubview:hexTextView];
[self.view sendSubviewToBack:colourPreview];
}
completion:^(BOOL finished){
[hexText becomeFirstResponder];
}];
}
But it just changes to the new view without a transition. Any ideas?
Have you tried using transitionWithView:duration:options:animations:completion: (docs here). Seems this is preferred in iOS 4.0+. There's an example of how to use it in those docs.
If you want to use your current method, I think forView in [UIView setAnimationTransition:forView:cache:] needs to be the superview of the views you want to animate. In your case, this looks to be self.view. Full docs here.
HTH

Problems moving animation to iOS4 blocks

I have a working view animation, that curls up a container view, while the containerview.subviews changes. (before the animation a UITableView will be shown, after it is a custom view, name keypadView)
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.75];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp
forView:containerView
cache:YES];
[secondView removeFromSuperview];
[containerView addSubview:keypadView];
[UIView commitAnimations];
Now I want to rewrite this code for the iOS4 block-based api, as I want to use the completion block. I wrote this:
[UIView transitionWithView:containerView
duration:.75
options:UIViewAnimationTransitionCurlUp
animations:^{
NSLog(#"Hey Ho");
[secondView removeFromSuperview];
[containerView addSubview:keypadView];
}
completion:NULL];
The views switch — but not animated.
what is wrong with my code?
Edit
completion: ^(BOOL completed){
NSLog(#"completed %d", completed);
}
doesn't help, as NULL is an accepted value, according to the docs
do:
options:UIViewAnimationOptionTransitionCurlUp
instead of:
options:UIViewAnimationTransitionCurlUp
That is why your code works now :).
The sample in the UIView class reference may be wrong - or maybe there's a bug with adding and removing views in the animations block object, but the only way I've been able to get it to work is as follows:
[secondView removeFromSuperview];
[containerView addSubview:keypadView];
[UIView transitionWithView:containerView
duration:.75
options:UIViewAnimationOptionTransitionCurlUp
animations:^{}
completion:^(BOOL finished) {
NSLog(#"finished %d", finished);
}];
Did you leave [UIView beginAnimations:nil context:nil]; above your new block?
Is the completion block always NULL? Try putting an NSLog statement in there or something. I don't know if NULL blocks would mess it up.

iPhone: transitioning views with Core Animation

On the Mac, the best way for a simple cross-fade transition of views (without any custom keyframe timing) is to do something such as the following excerpt:
[[self animator] replaceSubview:aView with:bView];
Unfortunately the animator property isn't available on the iPhone. What's the best bet of doing this on the iPhone? Is it setting alpha channels on each view? Sample code would be excellent.
Thanks.
The basic way to animate views on the iphone is to use the UIView beginAnimations and commitAnimations calls. They allow you to modify the animatable properties of a view and have those changes animated.
For instance I have a custom view that is hidden and shown using this approach:
- (void) showAView:(CustomAView *)aView
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
aView.frame = CGRectMake(0.0f, 110.0f , aView.frame.size.width, aView.frame.size.height);
[UIView commitAnimations];
}
- (void) hideAView:(CustomAView *)aView
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
aView.frame = CGRectMake(0.0f, self.view.frame.size.height, aView.frame.size.width, aView.frame.size.height);
[UIView commitAnimations];
}
By wrapping the frame property change in the UIView beginAnimations/commitAnimations the change has a standard animation applied to it.
You can add additional properties to the animation by using UIView animation class methods eg.
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

UIScrollView scrollRectToVisible at custom speed

I have a UIScrollView and I'm calling scrollRectToVisible:animated:YES on it.
I would like to set the speed at which it is animated. Can that be done?
I ended up finding a solution. In my case, the scrolling was animated programmatically after launch, to mimic a slot machine (with 3 horizontal UIScrollViews). Was doing this with the scrollRectToVisible:animated: method.
I got to set a custom speed using UIView's beginAnimation:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:(abs(rMid-pMid)*0.3)];
scrollMid.contentOffset = CGPointMake(rMid*320, 0);
[UIView commitAnimations];
AnimationDuration depends on the distance the scroller has to move between each "drawing".
A modern version with blocks:
[UIView animateWithDuration:1.0 animations:^{
[self.scrollView scrollRectToVisible:CGRectMake(...) animated:NO];
} completion:^(BOOL finished) {
...
}];