I'm developing a game in which I want my image to reduce in size gradually. I'm reducing the frame size gradually in my code when it works fine. [I've already used CGAffineTransform and it doesn't suit my requirement.]
-(void)function
{
ravanImage1.frame=CGRectMake(150,((ravanImage1.frame.origin.y)-5),q,z);
if(ravanImage1.center.y>=300&&ravanImage1.center.y<=370)
{
q=60;
z=60;
ravanImage1.frame=CGRectMake(150,((ravanImage1.frame.origin.y)-5),q,z);
}
if(ravanImage1.center.y>=230&&ravanImage1.center.y<=299)
{
q=40;
z=40;
ravanImage1.frame=CGRectMake(150,((ravanImage1.frame.origin.y)-5),q,z);
}
if(ravanImage1.center.y>=150&&ravanImage1.center.y<=229)
{
q=20;
z=20;
ravanImage1.frame=CGRectMake(150,((ravanImage1.frame.origin.y)-5),q,z);
}
}
But when I apply a while loop for the same code specifying wheather at what point to stop reducing the frame("while that point isn't reached"), it doesn't show the image frame reduction little by little as it shows it otherwise, but directly places the image at the end point with proper frame.
I want it to get displyed the way it gets without the while loop i.e. reduction little by little. Yes, while debugging it steps through all the steps properly.
Can anybody please help me?
As others have pointed out, manually adjusting the frame of your view will give you terrible performance. If you really don't want to use a standard UIView animation block for changing your view, you can specify bounds size values to animate through using a CAKeyframeAnimation applied to your view's layer:
CAKeyframeAnimation * customSizeAnimation = [CAKeyframeAnimation animationWithKeyPath:#"bounds.size"];
NSArray *sizeValues = [NSArray arrayWithObjects:[NSValue valueWithCGSize:size1], [NSValue valueWithCGSize:size2], [NSValue valueWithCGSize:size3], nil];
[customSizeAnimation setValues:frameValues];
NSArray *times = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f], [NSNumber numberWithFloat:0.5f], [NSNumber numberWithFloat:1.0f], nil];
[customSizeAnimation setKeyTimes:times];
customSizeAnimation.fillMode = kCAFillModeForwards;
customSizeAnimation.removedOnCompletion = NO;
[view.layer addAnimation: customSizeAnimation forKey:#"customSizeAnimation"];
This animation will start at size1, pass through size2 at the midway point in the animation, and end at size3. You can have an arbitrary number of key frames and times for your animation, so you should be able to achieve the effect you desire
EDIT (1/5/2010): Removed kCAAnimationPaced as a calculationMode, which would cause the key times to be ignored. Also, I forgot that frame was a derived property, so you need to animate something like the bounds size instead.
The reason it does that is because it executes the while loop very quickly. I think the best thing to do is put some sort of a delay timer after each step of the while loop, then you'll see each step and it won't just 'jump' to it's final state.
[self setTimer: [NSTimer scheduledTimerWithTimeInterval: 3.5
target: self
selector: #selector (function_name)
userInfo: nil
repeats: YES]];
try using this.
This' my move function in which I'm trying to change the size of my imageView. If you can point out any error, I'll be really grateful..
-(void)move
{
UIImageView *imageViewForAnimation = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"ravan.jpg"]];
imageViewForAnimation.alpha = 1.0f;
CGSize size1=CGSizeMake(60,60);
CGSize size2=CGSizeMake(40,40);
CGSize size3=CGSizeMake(20,20);
CAKeyframeAnimation *customFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:#"frame"];
NSArray *sizeValues = [NSArray arrayWithObjects:[NSValue valueWithCGSize:size1], [NSValue valueWithCGSize:size2], [NSValue valueWithCGSize:size3], nil];
[customFrameAnimation setValues:sizeValues];
customFrameAnimation.duration=10.0;
NSArray *times = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f], [NSNumber numberWithFloat:0.5f], [NSNumber numberWithFloat:1.0f], nil];
[customFrameAnimation setKeyTimes:times];
customFrameAnimation.fillMode = kCAFillModeForwards;
customFrameAnimation.removedOnCompletion = NO;
[imageViewForAnimation.layer addAnimation:customFrameAnimation forKey:#"customFrameAnimation"];
[self.view addSubview:imageViewForAnimation];
}
Related
Ok I am trying to deal a deck of cards at certain positions, but I am stuck at a spot. I am only dealing one less than the max. I am trying to deal to every spot in the index of the nsarray.
- (CGPoint)animateDealingToPlayer:(Player *)player withDelay:(NSTimeInterval)delay
{
self.frame = CGRectMake(-100.0f, -100.0f, CardWidth, CardHeight);
self.transform = CGAffineTransformMakeRotation(M_PI);
NSArray *position = [NSArray arrayWithObjects:
[NSValue valueWithCGPoint:CGPointMake(0, 0)],
[NSValue valueWithCGPoint:CGPointMake(0, 0)],
[NSValue valueWithCGPoint:CGPointMake(0, 0)],
[NSValue valueWithCGPoint:CGPointMake(0, 0)],
[NSValue valueWithCGPoint:CGPointMake(10, 50)],
nil];
for(int i=0; i<5; i++) {
NSValue *value = [position objectAtIndex:i];
CGPoint point = [value CGPointValue];
NSLog(#"%#",NSStringFromCGPoint(point));
self.center = point;
}
}
I am dealing the card at 10,50 and not dealing the card at previous positions such as 0,0. This is probably easy to solve, but I can't seem to remember how to fix this. Please help me. Also, note that I am still somewhat new to objective c so you may have to dumb this down if you solve this lol.
The problem is that you are setting the cards position 5 times without doing any animation or giving the card object a chance to be redrawn in between changing it's position. iOS doesn't actually position the card until the run loop gets a chance to fire. There are a number of possible solutions, but you should probably look into adding an animation block inside your loop so that the movement is queued for the the display.
I have a situation over here.
I am using AVFoundation to capture the camera frame.
Now what i want to do is that for certain frames, i need to display a picture which revolves in a step by step fashion.
What I am trying to do is that I am taking 4 CALayers comprising of front back left and right images of an object and using CALayer time property and group animation property, i want to display all the images one by one after certain milli seconds interval of time so that the continuous images seems to be like an animation.
How to go about it ? Please help me with some coding here.
-(void)startMainAnimation
{
//Animationframes is array of images that should be CGImage Type not UIImage..
//animation Layer that is added above view……
CAKeyframeAnimation *frameAnimation = [[CAKeyframeAnimation alloc] init];
[frameAnimation setKeyPath:#"contents"];
frameAnimation.calculationMode = kCAAnimationDiscrete;
[animationLayer setContents:[animationFrames lastObject]];
frameAnimation.autoreverses = NO;
frameAnimation.duration = ((float)[animationFrames count])/4.5;;
frameAnimation.repeatCount = 1;
[frameAnimation setValues:animationFrames];
[frameAnimation setRemovedOnCompletion:YES];
[frameAnimation setDelegate:self];
[animationLayer addAnimation:frameAnimation forKey:#"contents"];
[frameAnimation release];
}
Answer on the basis of Mohit Gupta's pastie link:
Set CALayer on which you want image sequence animation
CALayer *animationLayer = [CALayer layer];
[animationLayer setFrame:CGRectMake(125, 0, 240, 300)];
[self.baseLayer addSublayer:animationLayer];
Define Array of Images needed to be shown in sequence animation
NSArray *animationFrames = [NSArray arrayWithObjects:(id)[UIImageimageNamed:#"1.png"].CGImage, (id)[UIImage imageNamed:#"2.png"].CGImage, (id)[UIImage imageNamed:#"3.png"].CGImage, nil];
Using CAKeyframeAnimation to display array of images in sequential manner
CAKeyframeAnimation *frameAnimation = [[CAKeyframeAnimation alloc] init];
[frameAnimation setKeyPath:#"contents"];
frameAnimation.calculationMode = kCAAnimationDiscrete; //mode of transformation of images
[animationLayer setContents:[animationFrames lastObject]]; //set the array objects as encounterd
frameAnimation.autoreverses = NO; //If set Yes, transition would be in fade in fade out manner
frameAnimation.duration = ((float)[animationFrames count])/4.5; //set image duration , it can be predefined float value
frameAnimation.repeatCount = HUGE_VAL; //this is for inifinite, can be set to any integer value as well
[frameAnimation setValues:animationFrames];
[frameAnimation setRemovedOnCompletion:YES];
[frameAnimation setDelegate:self];
[animationLayer addAnimation:frameAnimation forKey:#"contents"]; //add animation to your CALayer
[frameAnimation release];
Hope this helps
Can one customize the underside color of curling view in a UIViewAnimationTransitionCurlUp animation. The default seems to be gray/white but i needed a different one.
Officially, no.
Unofficially, you can change the color using undocumented methods – but it will cause your app to be rejected if you target for AppStore,
CAFilter* trans_filter = [CAFilter filterWithName:#"pageCurl"];
[trans_filter setValue:[NSArray arrayWithObjects:
[NSNumber numberWithFloat:1.0f], // Red
[NSNumber numberWithFloat:0.0f], // Green
[NSNumber numberWithFloat:0.0f], // Blue
[NSNumber numberWithFloat:1.0f], // Alpha
nil] forKey:#"inputColor"];
CATransition* trans = [CATransition animation];
trans.filter = trans_filter;
trans.duration = 2; // etc.
[the_superview_containing_the_transitions.layer addAnimation:trans
forKey:#"transition"];
[the_old_view removeFromSuperview];
[the_superview_containing_the_transitions addSubview:the_new_view];
I am trying to create a falling coin. The coin image is a CALayer with 2 CABasicAnimations on it - a falling down and a rotation one. When the falling down animation gets to its end, it stays there. The rotation animation though, which is supposed to be random and end up in a different angle every time, just pops back to the original CALAyer image.
I want it to stay in the angle it finished the animation on. Is it possible? How do I do it?
Code:
//Moving down animation:
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:#"transform.translation.y"];
anim.duration = 1;
anim.autoreverses = NO;
anim.removedOnCompletion = YES;
anim.fromValue = [NSNumber numberWithInt: -80 - row_height * (8 - _col)];
anim.toValue = [NSNumber numberWithInt: 0];
//Rotation Animation:
CABasicAnimation *rota = [CABasicAnimation animationWithKeyPath:#"transform.rotation"];
rota.duration = 4;
rota.autoreverses = NO;
rota.removedOnCompletion = NO;
rota.fromValue = [NSNumber numberWithFloat: 0];
rota.toValue = [NSNumber numberWithFloat: 2.5 * 3.15 ];
[cl addAnimation: rota forKey: #"rotation"];
[cl addAnimation: anim forKey: #"animateFalling"];
Have you set the removedOnCompletion property of the rotation animation to NO, e.g.,
rota.removedOnCompletion = NO;
That should leave the presentation layer where it was when the animation finished. The default is YES, which will snap back to the model value, i.e., the behavior you describe.
The fillMode should also be set, i.e.,
rota.fillMode = kCAFillModeForwards;
I was trying to rotate an arrow back and forth, like the Twitter/Facebook "Pull to Refresh" effect.
The problem is, I was doing the rotation back and forth on the same UIView, so after adding
rotation.removedOnCompletion = NO;
rotation.fillMode = kCAFillModeForwards;
The forward animation war working OK but the backwards animation was not working at all.
So I added the last line suggested by yeahdixon, and in addition set the view's transform to the animation's completed state: (rotation by 180 degrees)
[myview.layer removeAllAnimations];
myView.transform = CGAffineTransformMakeRotation(M_PI);
For the 'restore' animation (backwards) I do this on completion:
myView.transform = CGAffineTransformMakeRotation(0);
...and it works fine. Somehow it doesn't need the removeAllAnimations call.
I found that by setting : removedOnCompletion = NO;
did not produce a visible leak in instruments, but did not get deallocated and was accumulating a small amount of memory. IDK if its my implementation or what, but by adding removeAllAnimations at the end of the animation seemed to clear out this tiny bit of residual memory.
[myview.layer removeAllAnimations];
Got a question. I have a subclassed UIView that is acting as my background where I am scrolling the ground. The code is working really nice and according to the Instrumentation, I am not leaking nor is my created and still living Object allocation growing.
I have discovered else where in my application that adding an animation to a UIImageView that is owned by my subclassed UIView seems to bump up my retain count and removing all animations when I am done drops it back down.
My question is this, when you add an animation to a layer with a key, I am assuming that if there is already a used animation in that entry position in the backing dictionary that it is released and goes into the autorelease pool?
For example:
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
NSString *keyValue = [theAnimation valueForKey:#"name"];
if ( [keyValue isEqual:#"step1"] && flag ) {
groundImageView2.layer.position = endPos;
CABasicAnimation *position = [CABasicAnimation animationWithKeyPath:#"position"];
position.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
position.toValue = [NSValue valueWithCGPoint:midEndPos];
position.duration = (kGroundSpeed/3.8);
position.fillMode = kCAFillModeForwards;
[position setDelegate:self];
[position setRemovedOnCompletion:NO];
[position setValue:#"step2-1" forKey:#"name"];
[groundImageView2.layer addAnimation:position forKey:#"positionAnimation"];
groundImageView1.layer.position = startPos;
position = [CABasicAnimation animationWithKeyPath:#"position"];
position.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
position.toValue = [NSValue valueWithCGPoint:midStartPos];
position.duration = (kGroundSpeed/3.8);
position.fillMode = kCAFillModeForwards;
[position setDelegate:self];
[position setRemovedOnCompletion:NO];
[position setValue:#"step2-2" forKey:#"name"];
[groundImageView1.layer addAnimation:position forKey:#"positionAnimation"];
}
else if ( [keyValue isEqual:#"step2-2"] && flag ) {
groundImageView1.layer.position = midStartPos;
CABasicAnimation *position = [CABasicAnimation animationWithKeyPath:#"position"];
position.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
position.toValue = [NSValue valueWithCGPoint:endPos];
position.duration = 12;
position.fillMode = kCAFillModeForwards;
[position setDelegate:self];
[position setRemovedOnCompletion:NO];
[position setValue:#"step1" forKey:#"name"];
[groundImageView1.layer addAnimation:position forKey:#"positionAnimation"];
}
}
This chains animations infinitely, and as I said one it is running the created and living object allocation doesn't change. I am assuming everytime I add an animation the one that exists in that key position is released.
Just wondering I am correct. Also, I am relatively new to Core Animation. I tried to play around with re-using the animations but got a little impatient. Is it possible to reuse animations?
Thanks!
Bryan
Your assumption is difficult to verify without Apple's source code. When you call addAnimation:forKey:, the CALayer makes a copy of the animation object. There is no guarantee from the API (in the docs or elsewhere) that it will release a running animation when another is added for the same key. It would be much safer for you to explicitly call removeAnimation:forKey: rather than relying on the API to do what you want.
Also, why have you set removedOnCompletion to NO in the code above? The default behavior of a CAAnimation is to remove itself from the layer once it has completed, and then call animationDidStop:finished: on the delegate. You can then add any other animation to the layer with the same key. Isn't this exactly what you're trying to do? It seems like you might be over thinking it a little.
As for your question about reusing animations: since the animation object is copied when added to a CALayer, you can hold a reference to an animation object that you create and keep adding it to as many layers as you want as many times as you like.