Hey everbody. I have a little issue here. I just want to scale my view, but only to up. I mean, i dont want it move out from original coords, i just want to scale in one direction, without affecting the position.
[UIView beginAnimations: #"myViewAnimation" context: nil];
[UIView setAnimationDuration:1.0];
redView.transform = CGAffineTransformMakeScale(1.0, 3.0);
[UIView commitAnimations];
Thanks!
Origin of all UIView transforms is its layer's anchorPoint, which is in the center of the view by default. Set anchorPoint to CGPointZero so view will be scaled to the right and upwards and its current position will be fixed:
redView.layer.anchorPoint = CGPointZero;
...
UIView beginAnimations: #"myViewAnimation" context: nil];
[UIView setAnimationDuration:1.0];
redView.transform = CGAffineTransformMakeScale(1.0, 3.0);
[UIView commitAnimations];
Note also that view's frame is calculated using its layer's anchorPoint so you may need to also adjust your view's frame accordingly
To access view's layer you will need to add QuartzCore.framework to link with your project and import <QuartzCore/QuartzCore.h> header file.
Related
I am fading in a label and I am attempting to have it come in from the side of the view. I am using this code to fade it in:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[label setAlpha:1.0];
[label setFrame:CGRectMake(10,20,100,200)];
In the story board I have the X axis as -52 and I am trying to have it change to 10 in the 1.0 second animation. The fading part is working fine but I can't figure out how to change the x or y axis through the animation. I have tried a lot of different things but nothing is working. Any help is much appreciated!
You have to use frame property to set the x and y. Do not change the width/height.
[label setFrame:CGRectMake(10,20,label.frame.size.width,label.frame.size.height)];
The code you have shown does not actually show committing the animations. You should use the more modern block based animations that prevents this from happening
[UIView animateWithDuration:1.0f
animations:^{
label.alpha = 1.f;
CGRect frame = label.frame;
frame.origin = CGPointMake(10.f, 20.f);
label.frame = frame;
}];
I have a problem where I need to rotate an UIImageView and resize depending if it's Portraite mode or Landscape mode.
I've managed to get the image view to rotate correctly, by doing as the answer in this related problem says: Rotate UIImageView Depending on iPhone Orientation.
This is my code for the rotation (for now I've only focused on rotating it to left landscape):
- (void)rotateImage:(UIImageView *)image duration:(NSTimeInterval)duration
curve:(int)curve degrees:(CGFloat)degrees
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
[UIView setAnimationBeginsFromCurrentState:YES];
float angle = DEGREES_TO_RADIANS(degrees);
CGAffineTransform transform = CGAffineTransformRotate(
CGAffineTransformMakeTranslation(
160.0f-self.view.center.x,
240.0f-self.view.center.y
),angle);
image.transform = transform;
[UIView commitAnimations];
}
How I initiate the UIImageView:
UIImage *image = [UIImage imageNamed:#"picture.png"];
self.testImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
self.testImageView.image = image;
self.testImageView.contentMode = UIViewContentModeScaleAspectFit;
self.testImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
What's happening now is that the Image gets rotated but has the same size as in portrait mode in landscape mode, but is centered (that I understand why, as I did the rotation as the answer to the linked question).
So, what I want is that the image is viewed in fullscreen, as the original image has the scale to fit in fullscreen landscape. As I've set
UIViewContentModeScaleAspectFit
my original naive thought that this would magically rescale to fit the proper landscape scale. That's obviously not the case. Any hints?
Try using UIViewContentModeScaleAspectFit and set in your parent view property autoresizesSubviews to YES. Image should resize automagically.
I have a problem that hopefully has a simple solution. I am trying to create a bobbing effect using a button, a timer, frame, cgaffinetransformrotations, and delays. Everything would be great, except that when the button is rotated and I set the frame of the button to decrease or increase the y by 10 or so pixels, the rotation makes the frame of the button bigger than it normally is. The end result is a button that constantly grows until it swallows the screen.
I tried making the upward and downward movements cgaffinetranslations, but that uses the transform property (which is the same one that the rotations use). The result is a very jumpy and non-realistic bob.
What I am trying to accomplish is just to set the origin component of the frame without having to specify a width and height, because even specifying a hard-coded width and height still makes it shrink when it is rotating and grow when it is approaching equilibrium again.
Any ideas?
Thanks!
CODE:
- (void)movePlay{
[self performSelector:#selector(moveDown:) withObject:play];
[self performSelector:#selector(rotateRight:) withObject:play afterDelay:0];
[self performSelector:#selector(moveUp:) withObject:play afterDelay:0.5];
[self performSelector:#selector(rotateLeft:) withObject:play afterDelay:1];
}
- (void)moveUp:(UIButton*)log{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.5];
log.transform = CGAffineTransformMakeTranslation(0, -20);
[UIView commitAnimations];
}
- (void)rotateRight:(UIButton*)log{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
log.transform = CGAffineTransformMakeRotation(0.0174532925*10);
[UIView commitAnimations];
}
This is just an idea.
Try combining all the separate transformations together. So instead of committing independently
log.transform = CGAffineTransfor...,
combine them first with
CGAffineTransformConcat(CGAffineTransform t1, CGAffineTransform t2);
CGAffineTransformRotate(CGAffineTransform t1, CGFloat angle);
etc.
I'm building a basic calculator app. The calculator is already fully functional with basic features. The problem I'm having is I ran out of room on the screen to add other, more advanced features. Ideally what I would like to do is create some kind of subclass and view that slides up to to the bottom of the label (where the completed calculations are displayed), when a button on the bottom of the screen is pressed. In other words, I want a view with more operators and computation options to slide up to the bottom of the label and I dont want this view to cover up any digits that are being displayed in the label. Is there anyway to achieve this?
Yes, you can. When your button is pressed, create your new view (with the extra functions), add it as a subview to self.view, and animate a change to it's frame, so that it's frame changes from below the visible view to just below your digits.
Like this:
-(void) buttonPressed {
UIView *myNewView = << create your view >>
myNewView.frame = CGRectMake(0,480, 320,200); // or whatever your width and height are
// 480 means it will be below the visible frame.
[self.view addSubview: myNewView];
float bottom_Y_of_digit_display = 100;// or wherever it is...
[UIView beginAnimations:nil contenxt:nil];
[UIView setAnimationDelay: 0.0];
[UIView setAnimationDuration: 1.0]; // one second.
myNewView.frame = CGRectMake(0,bottom_y_of_digit_display, 320,200);
[UIView commitAnimations];
}
You can use UIAnimations to create an animation out of any changes to UIView properties. You can make your UIView hidden by settings its frame.origin.y to 480 and then change the rect and wrap it in a UIAnimation.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
CGRect newFrame = newSubView.frame;
newFrame.origin.y -= newSubView.frame.size.height;
newSubView.frame = newFrame;
[UIView commitAnimations];
UPDATE
If you are targeting iOS 4.0 and later then you should use the new UIAnimations interface.
UIViewAnimationOptions options = UIViewAnimationOptionCurveLinear;
[UIView animateWithDuration:0.75 delay:0 options:options
animations:^(){
CGRect newFrame = newSubView.frame;
newFrame.origin.y -= newSubView.frame.size.height;
newSubView.frame = newFrame;
}
completion:^(BOOL finished){
//Do something upon completion
}];
If I attempt to animate the frame height of a tableView (ex: height -= 200), the cells that appear in the last 200px disappear suddenly before the smooth animation of the frame completes.
To make sure that it's nothing else I'm doing, I created a new View-Based application. In the main viewController I create my own tableview with enough pseudo rows to fill the entire screen. And on selection of a row I do a simple height animation.
most relevant code:
- (void)viewDidLoad {
[super viewDidLoad];
self.myTable = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)] autorelease];
myTable.delegate = self;
myTable.dataSource = self;
[self.view addSubview:myTable];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
CGRect frame = self.myTable.frame;
frame.size.height = 200;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDelay:.5f];
[UIView setAnimationDuration:0.5f];
self.myTable.frame = frame;
[UIView commitAnimations];
}
Does anyone know why this is happening, or what a fix/workaround may be?
Any suggests are really appreciated.
TIA!
I'm not sure I had exactly the same problem, but using [UIView setAnimationBeginsFromCurrentState:YES]; solved (parts of) the glitches (my table view slid around crazily when animating a frame height change).
Same problem here as well. This is what I'm doing and the origin animates smoothly, but the size changes immediately... annoying.
[UIView beginAnimations:#"HideTabbar" context:nil];
[UIView setAnimationDuration:.3];
self.tableView.frame = CGRectMake(0.0, 44.0, 320, 366);
[UIView commitAnimations];
UPDATE: Try adding this before the animation:
self.tableView.autoresizingMask = UIViewAutoresizingNone;
For anyone hitting this question & answer in the future, here's how to use #ryyst's answer in UIView's block based animations introduced in iOS 4.
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
// change frame etc here
} completion:^(BOOL finished) {
// any clean up here
}];
This is an old question and there are already a couple suggestions for a workaround, but I thought I'd add mine.
I ended up animating the contentInset and scrollIndicatorInsets properties, which provides the illusion that the table itself is being resized.
I have exactly the same problem. I imagine that tableviews have a special behavior on "setFrame:", it seems that the tableview remove the cells that won't be visible with the new frame.
In case of an animation, the cells won't be visible only at the end of the animation, but it seems that tableviews don't care.
If someone have a better theory, I'd be glad to hear it !
Finally found the solution! there is indeed a bug!! don't use variables, when animating the height use [[UIScreen mainScreen] bounds]. Like this:
[UIView animateWithDuration:0.5 animations:^{
tableView.frame=CGRectMake(0, 38, fullScreenRect.size.width, [[UIScreen mainScreen] bounds].size.height-38);
}];
not :
[UIView animateWithDuration:0.5 animations:^{
tableView.frame=CGRectMake(0, 38, fullScreenRect.size.width, fullScreenRect.size.width-38);
}];
and it will work like magic!
I found your question while seeking the proper method to resize a tableView. I think your problem is in your animation you've specified UIView instead of UITableView. I was unable to duplicate your problem using either UIView or UITableView, but I'm using SDK 3.1 and it might be a bug that has been fixed since your post. I'm not sure, but I hope this helps!
It's an old question but this might help someone in the future;
I solved a similar problem by embedding the tableview in a UIView, and resizing the UIView instead of tableview. I set the tableview's height to a large value and also "clip subviews" property on the UIView. I resize the UIView proportional to tableview's contentSize. Its not a good solution but it worked for my purposes.