Edited to better explain my problem
I am trying to perform a zoom operation using my custom view (not UIView). The view has translation, scale, rotate values. I use these as follows, between calls to glPushMatrix() and glPopMatrix().
- (void)transform
{
glTranslatef(translation.x + anchor.x, translation.y + anchor.y, 0.0f);
//glRotatef(-rotation * 57.2957795f, 0.0f, 0.0f, 1.0f);
glScalef(scale.x, scale.y, 1.0f);
glTranslatef(-anchor.x, -anchor.y, 0.0f);
}
I am trying to figure out how I should modify the anchor and/or translation values so that the zoom operation is relative to what appears on screen. At 1:1 scale I can simply use the raw screen coordinates as the anchor and perform the above transform. But when the view is already at some arbitrary scale/position, the anchor and/or translation needs to account for that.
So far this is what I've figured out:
1) Get the displacement from the center of scale to the view origin, in screen coordinates.
2) Scale this value so it's in the view's local coordinate system.
3) Now I have the new anchor for scaling. I set the view's anchor to this value.
This alone is not enough it seems. I think I am missing a translation component, or another variable that goes into the new anchor point. What am I missing?
I found a solution. I used a separate transform specifically for the zoom operation, in addition to the transform that defines the original view. I simply concat the two to get the result I wanted -- which was to ensure that the zoom is relative to the original view.
Some of the CGAffine animations make assumptions about the components of the matrix that you wouldn't expect.
For example,i found that CGAffineTransformMakeTranslation also effected the rotation of the View. For this reason i would recommend not using the 'Make' transforms if your concating many instances of CGAffineTransform
Also, in your above example i can see that you assume that your matrix understands that your two translates should occur at different times. A transform matrix is is a set of physical attributes about the object at any 1 point in time.
You want an object to move to -anchor
and scale
You want an object to move to back to
anchor
These should be considered as two different animations
You should do this with 'Key Frame Animation', here is an example: CGPath Animation
Alternatively, you could chain two CGAffineTransform methods together.
-(void)Anim1
{
[UIView beginAnimations:#"Anim1_Done" context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
UIView setAnimationDidStopSelector:#selector(animationFinished:finished:context:)];
//DO STUFF HERE
[UIView commitAnimations];
}
- (void)animationFinished:(NSString *)animationID finished:(BOOL)finished context:(void *)context
{
if ([animationID isEqualToString:#"Anim1_Done"])
{
[self Anim2];
}
}
-(void)Anim2
{
[UIView beginAnimations:#"Anim2_Done" context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
UIView setAnimationDidStopSelector:#selector(animationFinished:finished:context:)];
//DO STUFF HERE
[UIView commitAnimations];
}
Your transform intent still isn't exactly clear.
The CGAffineTransformMakeTranslation transform will create the translation matrix needed to get to a desired point, but as i mentioned in my other answer some of the make transforms make assumptions about other things like rotation, if your object hasn't been rotated and therefore cant be reset then this isn't a problem, other wise you can use the translation components of the matrix:
Transform.tx;
Transform.ty;
Edit: 3 separate animations should be done with 3 separate matrices:
baseMatrix = CGAffineTransformIdentity;
affineMatrix1 = CGAffineTransformTranslate(baseMatrix, -anchor.x, -anchor.y);
affineMatrix2 = CGAffineTransformScale(baseMatrix, scale.x, scale.y);
affineMatrix3 = CGAffineTransformTranslate(baseMatrix, anchor.x, anchor.y);
Related
I have a button (actually four of them) in a specific position that I want to rotate and translate at the same time, therefore I opted to use CGAffineTransformMake and provide the transformation matrix.
However I noticed that if I want to translate x = -100, for instance, the button will first be instantly shifted x = +200 and then will animate it's transformation to x = -100.
Is there any way to make it translate without this shift to the opposite direction?
CGAffineTransform transform = CGAffineTransformMake(-1, 0, 0, -1, -100, 0); // Spins 180° and translates
[UIView beginAnimations:#"Show Menu" context:nil];
[UIView setAnimationDuration:2.4];
_menuButton1.transform = transform;
[_menuButton1 setAlpha:1.0];
[UIView commitAnimations];
You shouldn't be using that animation method. It is discouraged by Apple after iOS 4.0. https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/occ/clm/UIView/beginAnimations:context:
The preferred method is...
[UIView animateWithDelay:0.0
duration:2.4
options:0
animations:^() {
_menuButton1.transform = CGAffineTransformMake(-1, 0, 0, -1, -100, 0);
_menuButton1.alpha = 1.0;
}
completion:nil];
Will edit my answer when you reply to my comment.
OK, the cause for this shift is that you are trying to translate the view whilst using AutoLayout to constrain it.
I don't know why it causes an issue, but it does.
Your two options for fixing it are...
Turn of AutoLayout and then you code will work.
Keep AutoLayout enabled and learn how to animate constraints.
For the second option I can recommend the book "iOS6 by Tutorials" by Ray Wenderlich and his team.
http://www.raywenderlich.com/store/ios-6-by-tutorials
I used this to learn how to do AutoLayout properly.
You can start with one of the transforms, let's say translation:
CGAffineTransform transform = CGAffineTransformMakeTranslation(-100.0f, 0.0f);
And then add rotation to it:
transform = CGAffineTransformRotate(transform, (float)M_PI_2);
And finally set this transform to your view's transform property.
I have two IBOutlet variables connected to two instances of UIView in Interface Builder, called blankView and fadedBGView in my ViewController.
How can I set up this code so that the blankView instance will fade into fadedBGView, using the UIViewAnimationOptionTransitionCrossDissolve transition, and the two UIViews can simultaneously transform (move) from their current position (they have equal positions) to 0,0?
What's happening is the fade occurs, and then the fadedBGView view abruptly moves to 0,0.
In other words, I'd like to have the first UIView fade into the second, while simultaneously moving up the screen.
Hopefully this is clear enough to answer.
Best...SL
[UIView transitionFromView:blankView
toView:fadedBGView
duration:1.0
options:UIViewAnimationOptionTransitionCrossDissolve
completion:^(BOOL finished) {
[blankView removeFromSuperview];
}];
[UIView commitAnimations];
CGRect frame = fadedBGView.frame;
frame.origin.x = 0;
frame.origin.y = 0;
[UIView animateWithDuration:1.0 animations:^{
NSLog(#"UIView Transition/Move Called");
fadedBGView.frame = frame;
}];
iOS 4 and newer provides block-based animations, which your code is already using:
+[UIView animateWithDuration:animations:];
+[UIView animateWithDuration:animations:completion:];
Within the animation block, you can set multiple destination values. See Apple's UIView documentation for a reference to the animatable properties. So within one block, you can modify frames, and alpha (transparency) values of views. I'm not sure how the cross dissolve animation works, but if it's simply one view going from 0 to 1, and another view going from 1 to 0, that's easy to implement.
Instead of
UIViewAnimationOptionTransitionCrossDissolve
use
UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionAllowAnimatedContent
UIViewAnimationOptionAllowAnimatedContent option allows additional animations during transition. Make sure your fadedBGView has correct starting frame.
I have question about artifacts that appear when UITextField is moving in animation block...
Before i move my UITextField it looks like this:
and after moving like this:
My guess is that it has something to do with fonts after UITextField is shifted.
here's code that i use for moving UITextField:
if (answerText.editing)
{
[UIView beginAnimations:#"Moving UITextField" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:1.0];
movingAnswer = CGPointMake(160,75);
answerText.center = movingAnswer;
[UIView commitAnimations];
}
It could be that the resulting frame isn't aligned on an even integer. i.e. Moving that amount is causing the frame to be something like (100.5, 50.0, 50.0, 50.0). When you are drawing on a half-pixel boundary, some of the drawing routines are going to make things look blurry to try and make it appear in the correct place. I would print out the frame after the animation and check:
NSLog(#"%#", NSStringFromCGRect(movingAnswer.frame));
If you see any non-integer values, use one of the floor() functions to modify the resulting frame in order to snap it to a boundary.
I have a view which I want to be scaled and translated to a new location by animating it. I tried to achieve it with the following code:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kDurationForFullScreenAnimation];
[[self animatingView] setFrame:finalRect];
[UIView commitAnimations];
The effect of this code is, the view first changes its content's size to the finalRect and then translates it to the new location. i.e. The scaling part is never animated. The view is just transformed to the new size and then translated.
This issue is already discussed in several other threads but none of them draw a conclusion. A solution does exist though, to use a timer and set the frame each time in the timer callback, but it has a performance drawback.
What is the most appropriate solution to this problem, also, why in first case this problem occur?
Thanks
Setting the frame does neither a scale nor a translation. You are either using the wrong terminology or you are using the wrong tool for the job. Scale and translate are both done using Core Graphics Affine transforms when you're looking to affect the UIView (as opposed to the layer which use Core Animation transforms).
To scale a view use
// 2x
[rotationView setTransform:CGAffineTransformMakeScale(2.0, 2.0)];
To translate, use
// Move origin by 100 on both axis
[rotationView setTransform:CGAffineTransformMakeTranslation(100.0, 100.0)];
To animate these, wrap them in an animation block. If you want to transform the view with both of these, then you need to concatenate them.
If you are not wanting scale and translation (transforms) at all, then what you mean is you want to change the view's bounds and position. These are changed with calls to
[view setBounds:newRect];
[view setCenter:newCenter];
Where newRect and newCenter are a CGRect and CGPoint respectively that represent the new position on the screen. Again, these need wrapped in an animation block.
Here is an example with block animation:
CGPoint newCenter = CGPointMake(100.0,100.0);
[UIView animateWithDuration: 1
delay: 0
options: (UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction)
animations:^{object.center = newCenter ; object.transform = CGAffineTransformScale(CGAffineTransformIdentity, 2.0, 2.0);}
completion:^(BOOL finished) { }
];
Try this solution :
CGAffineTransform s = CGAffineTransformMakeScale(0.5f,0.5f);
CGAffineTransform t = CGAffineTransformMakeTranslation(100, 0);
v2.transform = CGAffineTransformConcat(t,s); // not s,t
this operation is not commutative. The order is the opposite of the order when using convenience functions for applying one transform to another.
And you can use this operation to : for example (remove the Scale) :
v2.transform =
CGAffineTransformConcat(CGAffineTransformInvert(s), v2.transform);
Im currently working on an iPhone project. I want to enlarge dynamically an UILabel in Objective-C like this:
alt text http://img268.imageshack.us/img268/9683/bildschirmfoto20100323u.png
How is this possible? I thought I have to do it with CoreAnimation, but I didn't worked. Here is the code I tried:
UILabel * fooL = //[…]
fooL.frame = CGRectMake(fooL.frame.origin.x, fooL.frame.origin.y, fooL.frame.size.width, fooL.frame.size.height);
fooL.font = [UIFont fontWithName:#"Helvetica" size:80];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
fooL.font = [UIFont fontWithName:#"Helvetica" size:144]; //bigger size
fooL.frame = CGRectMake(20 , 44, 728, 167); //bigger frame
[UIView commitAnimations];
The problem with this code is that it doesn't change the fontsize dynamically.
All you need to do is apply an affine transform to the UILabel object.
CGFloat scaleFactor = 2.0f;
label.transform = CGAffineTransformMakeScale(scaleFactor, scaleFactor); // Enlarge by a factor of 2.
Scaling your label as suggested by others using the transform property will work great. One thing to keep in mind is that as the label gets larger, the font is not increasing, but just the rendered text, which means it will appear "fuzzier" as it gets larger.
Just scale your Label instead of changing the fontSize.
Try this method:
+ (void)setAnimationTransition:(UIViewAnimationTransition)transition
forView:(UIView *)view
cache:(BOOL)cache
Parameters:
transition
A transition to apply to view. Possible values are described in UIViewAnimationTransition.
view
The view to apply the transition to.
cache
If YES, the before and after images of view are rendered once and used to create the frames in the animation. Caching can improve performance but if you set this parameter to YES, you must not update the view or its subviews during the transition. Updating the view and its subviews may interfere with the caching behaviors and cause the view contents to be rendered incorrectly (or in the wrong location) during the animation. You must wait until the transition ends to update the view.
If NO, the view and its contents must be updated for each frame of the transition animation, which may noticeably affect the frame rate.
Discussion
If you want to change the appearance of a view during a transition—for example, flip from one view to another—then use a container view, an instance of UIView, as follows:
Begin an animation block.
Set the transition on the container view.
Remove the subview from the container view.
Add the new subview to the container view.
Commit the animation block.