Is there any way to only use certain aspects of the transform identity when I'm resetting something to its CGAffineTransformIdentity?
I have this method that resets my image view:
- (void)resetImage
{
[UIView beginAnimations:nil context:nil];
[firstImageView setTransform:CGAffineTransformIdentity];
[UIView commitAnimations];
}
But I now want to make a new method that centers the image but leaves everything else about the transform i.e.(scale, rotation).
Is this something I can do simply so I can use an animation block like in my resetImage method? Or do I need to go a far more complicated route?
Thanks in advance!
just as a warning, I have very little experience with CG so take it easy on me :)
The identity transformation has no aspects. You're not saying “subtract all the things I did to it before” (such that you could exclude some of those things from the subtraction); you are resetting it to its initial, non-transformative value.
But I now want to make a new method that centers the image but leaves everything else about the transform i.e.(scale, rotation).
I assume you mean “leaves everything else … out”.
Then you need to make a transformation that contains the translation(s), but not the scale or rotation.
Related
i am new to IOS programming. what i want is i want to show multiple images and then they go upward continuously and then repeat again and again in sort of a loop. how can i do this ?
i have total 8 images which i want to show like this
i don't know how can i add 8 images and then they go upward direction verically continuously repeating. please help me in this. if there is any tutorial related to that and then please share
You should not do it this way. If you make your step value small enough for smooth movement it will be too slow.
You want to use UIView animation. Take a look at the the method animateWithDuration:animations: (and it's variations)
Your code might look like this:
#define K_AMOUNT_TO_MOVE
-(void)moveImages
{
[UIView animateWithDuration: 2.0
animations: ^
{
for(int i=0;i<images.count;i++)
{
UIImageView *MyImage = images[i];
MyImage.center = CGPointMake (MyImage.center.x, MyImage.center.y- K_AMOUNT_TO_MOVE);
}
}
];
}
There are variations on that basic method that take options that will auto-reverse the animation, make it repeat, change the timing to linear instead of the default ease-in, ease-out, etc. Take a look at the method animateWithDuration:delay:options:animations:completion: in the Xcode documentation.
I have a UIView to which various scale and rotation CGAffineTransforms have been applied, in response to touch screen gestures.
When a scale operation completes, I want to adjust the bounds of the view to the new size, then have view to re-draw itself at a scale of 1.0, while keeping all other components of the transform the same.
To adjust the bounds of the view, I'm using:
self.myView.bounds = CGRectApplyAffineTransform(self.myView.bounds, self.myView.transform);
To "undo" the scale transform, I'm trying this:
self.myView.transform = CGAffineTransformConcat(self.myView.transform, CGAffineTransformMakeScale(1, 1));
then calling [myView setNeedsDisplay] to get the view to redraw itself.
However this does not produce the desired results and when a rotate transform is applied, the above code seems to cause what looks like a sideways translation transform to be applied too.
What's the cleanest way to "undo" just a scale transform and have the view redraw at 1:1 with all other transforms remaining intact?
I am handling something similar, and what I do is store the separate components in another struct (scale, rotation, translation). That way you can easily recreate the transform again (be careful about the order of your operations though).
One thing though: I suggest that you don't change the bounds and instead just apple a scale operation to the transform. This will avoid some potential unneeded layouts. All of this stuff can be handled purely with the transform property. The way you are doing it now is not going to change anything since applying a scale of 1 is a no-op. If you scaled up the view by 2, you would need to scale by 0.5 to get back to the original. Or, as I said above, store all the components and recreate it from the identity matrix (matrix math is fast, don't worry about that).
Save your scale values in a variable and then, try changing CGAffineTransformMakeScale(1, 1) into
CGAffineTransformMakeScale(1/totalScaleWidth, 1/totalScaleHeight)
so what I want is to do the same thing Clear (http://www.realmacsoftware.com/clear/) does.
If I pan up or down it makes a new rectangle appear with a 3D visual effect.
I did manage to do this by using a translation and rotation transform along with changing the m34 property.
The problem is that since I move 2 views (like Clear moving the rectangle that is showing on screen, and the rectangle appearing with the 3D visual effect), when the user stop touching the screen I use a
[UIView animateWithDuration:0.5
delay:0
options: UIViewAnimationCurveEaseOut
animations:^{
...
}
completion:nil];
to restore the views to a consistent state, either showing the new rectangle, or not (sliding it down), I can see the black background between the 2 views, meaning that the 2 rectangles are not being redrawn at same time.
How do I solve that?
It's difficult to answer your specific question without more details, but you can look at https://github.com/mpospese/MPFoldTransition for inspiration.
Mark Pospesel developed this wrapper to perform these kinds of folding transforms, and describes the process behind them in detail in his "Anatomy of a folding animation" blog post.
This is the result of what I want to achieve:
(source: mouseaddict.com)
I have static images for my app, but i want to add motion to them.
Imagine a meter. It has a needle that is locked at the bottom (at 6:00 on a round-clock-style-face) and whose needle-top swivels from left to right in an arc. the arc (zero) starts with the needle pointed toward (roughly) the 9:30 position on a round-clock-style-face and the top-most part of that arc should end at roughly 2:30 on a round-clock-style-face.
I have several graphical elements 1) the round meter face (png) and 2) the verticaly oriented png file of a meter needle. I need to cause the needle to move within the round face in an arc'ing pattern.
So, assuming the two requirements above, what is the best way to swivel the needle within the meter using animation?
I have seen this: Speedometer -- but is this the best way? My main issue is that I want the needle locked at the bottom and move left-to-right only a small amount..as WELL as the fact that there is little (actually no) documentation on it. Things like "calculateDeviationAngle" are unexplained.
Also the Speedometer seems to move a large amount and the pivot point on that example would not work...(i dont think)
I would like to call this with a command like:
[self moveNeedleToPosition:degreeOrClockFaceNumber] and have it move from it's current position to a bit past the degreeOrClockFaceNumber and bounce back to rest at degreeOrClockFaceNumber.
TIA
I did the following: I broke the image up into several pieces. The outline (black) portion, the backgrounds and the needle. Each piece having the same exact "footprint" (shapes).
Assuming theImageView is the imageView for the needle (layered on top of the other two imageViews), I created the following function:
-(void) rotateNeedle:(UIImageView *)theImageView toAngle:(float) theAngle
{
[UIView animateWithDuration:0.5
delay: 0.0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
theImageView.transform =CGAffineTransformMakeRotation((M_PI / 180) * (theAngle + 10));
}
completion:^(BOOL finished){
//
[UIView animateWithDuration:0.5
animations:^{
theImageView.transform = CGAffineTransformMakeRotation((M_PI / 180) * theAngle);
}];
}];
}
The first animation in the block performs the "shoot-past" portion of the animation...ie: moving 10 degrees past the actual value. The second animation in the completion block then "bounces" the needle back to the correct value. The speed of these animations create the physics effect. It works quite well for me.
What I am doing is very simple. I have a simple 2d Maze game - think puzzle game that I am working on. I am not needing to use OpenGL for this. I am wanting to use UIImage's. Imagine I have a wall tile (64x64 pixels) and I want to write code to duplicated this wall tile along the top and edges of the screen area- to in effect build a wall around the edges of the screen. play will take place inside this area. How would I go about doing this in code. From what I gather I would need to create a new UIImage each time I wanted to place a tile and move coordinates. Later on though, say I wanted to take the second tile along and replace it with say a bomb. Or maybe the 4th one along? My question really is.. in code how would I address this 2nd or 4th tile (when the number 2 or 4 could be random) and change its graphic? I hope I have explained this enough.. Also If I have updated some tiles, is there a call I should be making to hurry up the screen refresh so to speak?
As I understand, you can just create some UIImageView objects. Each one for one of your tiles. And then you'll be able to change frames with animation. For example. You have image you want to replace. I have no mac right now nearby, so, my code may not wark after just copy and paste)
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5f];
[imageView setFrame:newFrame];
[UIView commitAnimations];
This code will move your image with animation.