Animating View - compatible for iPad 3.2 and 4.2 version - objective-c

Two views need to be resized when a button is clicked in iPad .
I Tried it with following code
[UIView animateWithDuration:1.0 animations:^{
//Target Rect of the View will be given here
}];
It works fine with iPad4.2 ,but Crashes with 3.2 version(Incompatible).
On Googling found that 3.2 version supports only Animating Views (W/O Blocks)
Source:
http://developer.apple.com/library/iOS/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/clm/UIView/beginAnimations:context:
They have mentioned its not advisable to use it for 4.0 and later
How can I achieve animating the views for both 3.2 and 4.2.

I you really need to support versions below 4.0, you can use this:
[UIView beginAnimations:#"animation" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:1.0]; //In seconds
//Here do your stuff
int newPosY = myImage.frame.origin.y - 10;
[myImage setFrame:CGRectMake(self.frame.origin.x, newPosY, myImage.frame.size.width, myImage.frame.size.height)];
[UIView commitAnimations];
That moves an UIImageView 10 pixels up.
Also check UIView reference to see all the possibilities.

Related

Strange results using CGAffineTransformIdentity

I have several UI elements in a nib that are positioned using autolayout and constraints. When the view loads i move the elements off screen using
[self.button setTransform:CGAffineTransformMakeTranslation(-self.button.frame.origin.x * 2 + self.button.frame.size.width, 0)];
then when the view appears i use the following to ease the button into the correct position on the page. However it seems to move too far across.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:2];
[self.button setTransform:CGAffineTransformIdentity];
[UIView commitAnimations];
Am i doing something wrong? Is there a better way to move the button off the screen with CGAffineTransform?
Any help would be great
The issue was caused by autolayout and the best way to animate UI elements when using autolayout is to change the constraints

Animating an image view to slide upwards

I am attempting to make an image view (logo below) slide upwards by 100 pixels. I am using this code, but nothing happens at all:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:3];
logo.center = CGPointMake(logo.center.x, logo.center.y - 100);
[UIView commitAnimations];
This code is in the viewDidLoad method. Specifically, the logo.center = ... is not functioning. Other things (like changing the alpha) do. Maybe I'm not using the right code to slide it upwards?
For non-autolayout storyboards/NIBs, your code is fine. By the way, it's now generally advised that you animate using blocks:
[UIView animateWithDuration:3.0
animations:^{
self.logo.center = CGPointMake(self.logo.center.x, self.logo.center.y - 100.0);
}];
Or, if you want a little more control over options and the like, you can use:
[UIView animateWithDuration:3.0
delay:0.0
options:UIViewAnimationCurveEaseInOut
animations:^{
self.logo.center = CGPointMake(self.logo.center.x, self.logo.center.y - 100);
}
completion:nil];
But your code should work if you're not using autolayout. It's just that the above syntax is preferred for iOS 4 and later.
If you're using autolayout, you (a) create an IBOutlet for your vertical space constraint (see below), and then (b) you can do something like:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
static BOOL logoAlreadyMoved = NO; // or have an instance variable
if (!logoAlreadyMoved)
{
logoAlreadyMoved = YES; // set this first, in case this method is called again
self.imageVerticalSpaceConstraint.constant -= 100.0;
[UIView animateWithDuration:3.0 animations:^{
[self.view layoutIfNeeded];
}];
}
}
To add an IBOutlet for a constraint, just control-drag from the constraint to your .h in the assistant editor:
By the way, if you're animating a constraint, be sensitive to any other constraints that you might have linked to that imageview. Often if you put something right below the image, it will have its constraint linked to the image, so you may have to make sure you don't have any other controls with constraints to your image (unless you want them to move, too).
You can tell if you're using autolayout by opening your storyboard or NIB and then selecting the "file inspector" (the first tab on the right most panel, or you can pull it up by pressing option+command+1 (the number "1")):
Remember, if you planning on supporting pre-iOS 6, make sure to turn off "autolayout". Autolayout is an iOS 6 feature and won't work on earlier versions of iOS.
have u try
logo.frame = CGRectMake(logo.frame.origin.x, logo.frame.origin.y - 100,logo.frame.size.width,logo.frame.size.height)

Change backside color of uiview when using Curlup animation

I have an uiview which i add a transition that removes it from an uiwindow using curlup animation. When the animation occurs the backside of the view is white...i would like to change the color of it or even put some of my own texture. Any help appreciated.
[UIView beginAnimations: #"Curl up" context:nil];
// wait for time before begin
[UIView setAnimationDelay:wait];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:viewToCurlUp cache:YES];
// druation of animation
[UIView setAnimationDuration:duration];
[UIView commitAnimations];
As far as I know, it is not possible to create a texture (ie: your own image) on a UIView. The color of 'back of the curl' should correspond to the background color you have set for your UIView. You can do this in the inspector in Interface Builder, or programmatically like so:
[view setBackgroundColor:[UIColor greenColor]];
where view is the UIView of interest. Use the appropriate color, of course.
Hope that helps.
Apple engineers responded that this is not possible in my case. I have asked them a few days ago.

How to implement the effect of popping up tableview from bottom of screen?

I'm a newbie of Xcode and Objective-c. Recently I wanna do a task about popping up tableviews. When a button is pressed, the tableview should pop up from the bottom of the screen. The tableview will contain just a few items so I don't wanna it occupy full screen. I read the manual and found UIModalPresentationStyle, but it seems not fulfill my requirements. So what's the more accurate methods I can use?
While #Bharat J's answer is correct, those methods are deprecated in iOS 4.0.
The latest and greatest implementation is + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations
Implemented as:
[UIView animateWithDuration:0.5 animations: ^{
tableView.frame = newFrame;
}
];
You can use below code to create an animation block . Set the frame for the table view as (0,480,320,0) When you hit a button change the frame of the table view in the animation block and make it to CGRectMake(0,200,320,280) or something .
[UIView beginAnimations:#"AnimateTableView" context:view];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
tableView.frame = CGRectMake(0, 200 , 320, 280);
[UIView commitAnimations];
The same animation block for hiding it again but with the frame again begin set to CGRectMake(0,480,320,0). This should work in your case .
Another option would be to use a UIActionSheet and add the uitableview as a subview.
Here is a very similar question about how to add a UIPickerview with similar effect.
Add UIPickerView & a Button in Action sheet - How?

Mac OS X quick user switch in iOS

I want to flip a view like the Mac OS X user switch,i have used this code
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:myview cache:YES];
[myview removeFromSuperview];
[UIView commitAnimations];
But this flipping is different from mac os user switch, help me if you can..This code provides different flip.
There is no built-in "cube rotate" in iOS. You'd need to create it yourself. There are several ways to do this. One way to approach it is to put both views into a single superview, and apply a CATransform3D transform to the "target" view to create the box. Then animate a CATransform3D for the entire superview to rotate it. When you're done, remove the old "source" view.
See the Core Animation Programming Guide, particularly Transforming a Layer's Geometry, for more information.