UIViewAnimation block UIView interaction - iphone

I have an UIViewController named MainViewController.
It's frame:
I also have an UIView named SingleSearchView which is a subview of MainViewController.
And I add this singleSearchView to the MainViewController:
self.singleSearchView = [[SearchView alloc] initWithFrame:CGRectMake(0, -480, 320, 480)];
[self.view addSubview:singleSearchView];
When I'm trying the following animation to "switch" to the singleSearchView it blocks me from any touch interaction with the singleSearchView
[UIView beginAnimations:#"searchAnimation" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:1.0f];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.singleSearchView cache:YES];
[self.view setFrame:CGRectMake(0, 480, 320, 480)];
[UIView commitAnimations];
I also tried this way:
[UIView animateWithDuration:1.5 delay:0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
[self.view setFrame:CGRectMake(0, 480, 320, 480)];
}
completion:nil];
Thanks

From this answer to another question:
Turns out UIView's block animation by default blocks user interaction, and to get around it you need to pass UIViewAnimationOptionAllowUserInteraction as one of the options. Hopefully someone else will have some use of this information as well.

You shouldn't try to move self.view. The self.view property is special in the UIVIewControll hierarchy. You should create a subview and animate this one instead...

Figured it out!
CATransition* trans = [CATransition animation];
[trans setType:kCATransitionPush];
[trans setDuration:0.5];
[trans setSubtype:kCATransitionFromBottom];
[self.singleSearchView setFrame:CGRectMake(0, 0, 320, 480)];
CALayer *layer = self.view.layer;
[layer addAnimation:trans forKey:#"Transition"];

Related

Expanding UIView -Iphone

I'm working on an i-phone app that when you press a button a UITableView drops down. I want to push/animate the objects that are underneath the button so that they are underneath the tableview.
For each label/textfield that needs to be moved down would I need something like:
[UIView beginAnimations: #"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration:0.3f];
self.view.frame = CGRectOffset(self.view.frame, 0, 70);
Any examples would be helpful.
In iPhone OS 4.0 and later, block-based animation methods are recommended by Apple such as
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations
for eg.
[UIView animateWithDuration:0.3
delay:0
options:UIViewAnimationOptionBeginFromCurrentState
animations:(void (^)(void)) ^{
self.view.frame = CGRectOffset(self.view.frame, 0, 70); }
completion:^(BOOL finished){
}];
Yes, you missed commitAnimations for the view.
So, there will be a UITableView that come from above to self.view and you wanted to move down all the elements in self.view, except the UITableView.
How about putting all the elements in self.view to a container view what have the same size as self.view? And then you can animate all the UI elements with one move
try this,
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
[UIView setAnimationDelay:0.3];
self.view.frame = CGRectOffset(self.view.frame, 0, 70);
[UIView commitAnimations];

Push-Pop kind Animation for UIViewController without using NavigationController

I want to implement Push/Pop kind of animation in my application. Currently in my application, I'm simply adding & removing viewcontroller using addsubview & removesubview without any animation.
Adding navigation controller will be a major change in application as it will change whole structure of application. Is there any way to implement such kind of animation with using navigation controller.
Try this
first give your subview a frame
secondview.frame=CGRectMake(330, 0, 320, 460);
Then when you are adding it
[self.view addSubView:secondview];
[UIView beginAnimations:#"bringViewDown" context:nil];
[UIView setAnimationDuration:0.2];
firstview.frame=CGRectMake(-330, 0, 320, 460);
secondview.frame=CGRectMake(0, 0, 320, 460);
[UIView commitAnimations];
Hope this helps.....
- (void)viewDidLoad
{
[super viewDidLoad];
viewSecond.frame = CGRectMake(330, 0, 320, 548);
}
- (IBAction)onBtnClick:(id)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
viewFirst.frame = CGRectMake(-330, 0, 320, 548);
viewSecond.frame = CGRectMake(0, 0, 320, 548);
[UIView commitAnimations];
}
- (IBAction)onBtnClick2:(id)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
viewFirst.frame = CGRectMake(0, 0, 320, 548);
viewSecond.frame = CGRectMake(330, 0, 320, 548);
[UIView commitAnimations];
}
Implement custom container controller refer Session 102 - Implementing UIViewController Containment in WWDC 2011
Add subview with push animation
//push animation
CATransition *transition = [CATransition animation];
transition.duration = 0.5;
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
[transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[self.view.layer addAnimation:transition forKey:nil];
[self addSubview:subView];
and remove subview with pop animation
//pop animation
CATransition *animation = [CATransition animation];
[animation setDuration:0.5];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromLeft];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[self.superview layer] addAnimation:animation forKey:#"SlideView"];
[self removeFromSuperview];

iPhone:Animation move view controller upward

I am developing an iPhone application, where i need to move a first view controller slowly upward in animation and move to second view controller. I am thinking to use CoreAnimation for moving the first view controller slowly upward and push to next view controller. Could someone help on giving what are the classes/apis available to achieve this?
Thank you!
Try this,
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:yourfirstview cache:YES];
[UIView setAnimationDidStopSelector:#selector(remove_view)];
//change frame here
yourfirstview.frame =CGRectMake(0,-480,320,480);
[UIView commitAnimations];
-(void)remove_view
{
[yourfirstview removeFromSuperview];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:yoursecondview cache:YES];
//change frame here
yoursecondview.frame =CGRectMake(0,0,320,480);
[UIView commitAnimations];
}
You can easily achieve this on a single view controller using multiple UIViews
As per Apple's documentation:
In iOS 4 and later, use the block-based animation methods.
So, in order to produce the intended results, using the following code.
UIView *mySecondView = [[UIView alloc] initWithFrame:CGRectMake(0, 480, 320, 480)];
[mySecondView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:mySecondView];
[UIView animateWithDuration:2.0 delay:3.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
[mySecondView setTransform:CGAffineTransformMakeTranslation(0, -480)];
}completion:^(BOOL done){
}];

iPhone Animation : How to animate only subview ? Not the whole view?

I am new with animations and I am using following code for Flip animation.
//MyMapView is UIView
MyMapView *aMapView = [[MyMapView alloc] initWithFrame:CGRectMake(0, 118, 321, 300) AndResArray:self.nearMeArray];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[[self view] addSubview:nearMeMap];
[UIView commitAnimations];
Its working fine. But whole view (with super view) Flips.
I only want to Flip subview that is MyMapView. The super view should not flip.
How to do this ???
EDITED
screen :
Below is main screen. When click on Map button I want to Flip only Brown part of the screen. Whole screen should not Flip.
Thanks...
I have figure out what's the problem. Please see the below code.
MapView.m
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
[self performSelector:#selector(addMapView) withObject:nil afterDelay:2.0];
}
return self;
}
- (void) addMapView {
//MyMapView is UIView
MKMapView *aMapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 321, 300)];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self cache:YES];
[self addSubview:aMapView];
[UIView commitAnimations];
}
And after that simply add object of MapView class where you want. like below.
//MyMapView is UIView
mapView *aMapView = [[mapView alloc] initWithFrame:CGRectMake(0, 118, 321, 300)];
[[self view] addSubview:aMapView];
vote up if this comment help you.
Thanks,
MinuMaster.
Matteo's answer is partially correct, but you need to set the animation on the view before you add it, or it won't work correctly. Like this:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:aMapView
cache:YES];
[self.view addSubview:aMapView];
[UIView commitAnimations];
Alternatively, you can use the block-based API introduced in iOS 4. Here's an example.
[UIView transitionWithView:aMapView
duration:1.0
options:UIViewAnimationTransitionFlipFromRight
animations:^{ [self.view addSubview:aMapView]; }
completion:^(BOOL f){ }
];
You need to add your view to self.view and then flip only aMapView:
MyMapView *aMapView = [[MyMapView alloc] initWithFrame:CGRectMake(0, 118, 321, 300) AndResArray:self.nearMeArray];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[self.view addSubView:aMapView];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:aMapView cache:YES];
[UIView commitAnimations];
Animated transitions like this animate the contents of a view. The system takes a snapshot of the contents at the beginning of the animation, another snapshot when you commit, and then animates between them. As you've seen with your original code, the entire view gets animated.
The simplest solution is probably just to introduce an intermediate view that's the size and position of where the map will go. Then animate that view as you add the map to it.
More info in the View Programming Guide's section on Creating Animated Transitions Between Views. Though the methods you're using have, in iOS 4, been superceded by new block-based versions.
-(void)viewDidLoad
{
[super viewDidLoad];
OptionView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 70, 70)];
OptionView.backgroundColor=[UIColor blueColor];
OptionIsShow=false;
MoveView=[[UIView alloc] initWithFrame:CGRectMake(40, 40, 70, 70)];
[MoveView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:MoveView];
}
-(void)ViewOrHideOptionView
{
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:self.MoveView cache:NO];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:1.0];
if(!OptionIsShow)
{
[self.MoveView addSubview:OptionView];
OptionIsShow=true;
}
else
{
[self.OptionView removeFromSuperview];
OptionIsShow=false;
}
[UIView commitAnimations];
}

animate UIImageView with flip animation

I'm a noob in objective-c and cocoa, and I would like to implement a simple animation of UIImageView with flipping animation. But I'm having a hard time doing it. Below are my snippets:
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,33,33)];
UIImageView *img1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:IMG1]];
UIImageView *img2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:IMG2]];
[containerView addSubview:img1];
[containerView addSubview:img2];
[self.view addSubview:containerView];
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationDuration:0.75];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:containerView cache:YES];
[containerView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
[UIView commitAnimations];
Thanks.
You refer to an object called view which is never declared. I think you need to change your second-to-last line to
[containerView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
See if that helps.