How can I animate this sprite?
if ((self = [super initWithSpriteFrameName:#"Boss_ship_5.png" world:world shapeName:#"Boss_ship" maxHp:50 healthBarType:HealthBarTypeRed])) {
I'm trying with this, but with _layer doesn't work...
CCSpriteFrameCache * cache =
[CCSpriteFrameCache sharedSpriteFrameCache];
CCAnimation *animation = [CCAnimation animation];
[animation addSpriteFrame:
[cache spriteFrameByName:#"Boss_ship_5.png"]];
[animation addSpriteFrame:
[cache spriteFrameByName:#"Boss_ship_4.png"]];
animation.delayPerUnit = 0.05;
[_layer runAction:
[CCRepeatForever actionWithAction:
[CCAnimate actionWithAnimation:animation]]];
Before adding sprites to animation object, you'll need to load the textures into the CCSpriteFrameCache. Please, take a look at
http://www.cocos2d-iphone.org/archives/633
Also, CCAnimation actions are supposed to be used with CCSprites. Which class is _layer object?
Related
How can I hide my UIImageView after the animation is completed? My animation is working fine when I segue into the view. I want to be a able to hide the image after the animation is complete. How might I do that?
-(void) animate
{
NSLog(#"Animate");
CGPoint startPoint = [pickerCircle center];
CGPoint endPoint = [pickerButton1 center];
CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath, NULL, startPoint.x, startPoint.y);
CGPathAddLineToPoint(thePath, NULL, endPoint.x, endPoint.y);
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:#"position"];
animation.duration = 3.f;
animation.path = thePath;
animation.repeatCount = 2;
animation.removedOnCompletion = YES;
[pickerCircle.layer addAnimation:animation forKey:#"position"];
pickerCircle.layer.position = endPoint;
}
Set the animation's delegate property to self like so:
animation.delegate = self;
And then you can use:
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
// hide your UIImage here
}
It will be called when the animation is done.
Did you try completion? Here example 4.2there is an example about showing and hiding an view with using completion.
I don't know animations well but I think completion should work.
I have three views on same viewController and I need to switch beetween them with transitions. Which is the best method to do it? Thank you
Well I am not sure if you have made the question clear .I Guess you want to show the views one at a time.In that case :-
1>You can add the views in a horizontal UIScrollView with its frame size equal to the screen or whatever area you want(width should be equal to width of screen..shown 1 at a time) along with a UIPageControl.
2>The other way is to use the Methods:-
[self.view bringSubViewToFront:yourSubView];
[self.view sendSubViewToBack:yourSubView ];
according to your requirements.
3>You can use Animation to switch between views.
[UIView animateWithDuration:0.7
delay:1.2
options: UIViewAnimationCurveEaseOut
animations:^{
CGRect frame=assignAframeToSwitch;
yourView.frame = frame;
}
completion:^(BOOL finished){
NSLog(#"Done!");
}];
OR
_Weak CATransition *transition;
transition = [CATransition animation];
transition.delegate=self;
transition.duration = 0.75;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[[viewcontroller layer] addAnimation:transition forKey:#"transition"];
CATransition is the best way to animate transition
CATransition* trans = [CATransition animation];
[trans setType:kCATransitionPush];
[trans setDuration:0.5];
[trans setSubtype:kCATransitionFromBottom];
// code to change the view//
CALayer *layer = rootViewController.view.layer;
[layer addAnimation:trans forKey:#"Transition"];
Hope it helps.
if you use this bellow code for animation for transitions and then just need to hide and show the view on button click event...
CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setType:kCATransitionFromBottom];
[animation setDuration:1.0];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:
kCAMediaTimingFunctionEaseInEaseOut]];
[[yourViewControllers layer] addAnimation:animation forKey:#"transitionViewAnimation"];
hope,this help you..
:)
The fastest easiest way to do this is with the built in block based UIView transition animations. Just look at the documentation for UIView. Its way simpler than the CATransition methods.
In the Google Maps app on iPhone, the toolbar in the buttom is still visible after the modal with a partial curl transition is preformed. When I setup a modal seque in my storyboard with a partial curl, the new view controller is hiding the toolbar from the parant view.
How can I setup a partial curl like the Google Maps app?
Try this code.This will be helpful to you.PLEASE DON'T HESITATE TO TRY THIS CODE.
You will need to import a QuartzCore.framework.
locationMapView is your MKMapView and curlView is your second UIView
- (IBAction)curlButtonPressed:(id)sender {
if (isCurlStarted == NO) {
[UIView animateWithDuration:1.0
animations:^{
CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setDuration:0.7];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:#"default"]];
animation.type = #"pageCurl";
animation.fillMode = kCAFillModeForwards;
animation.endProgress = 0.65;
[animation setRemovedOnCompletion:NO];
[locationMapView.layer addAnimation:animation forKey:#"pageCurlAnimation"];
[locationMapView addSubview:curlView];
;}
];
isCurlStarted = YES;
}else{
[self curlDownPressed:curlDownButton];
}
}
- (IBAction)curlDownPressed:(id)sender {
[UIView animateWithDuration:1.0
animations:^{
CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setDuration:0.7];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:#"default"]];
animation.type = #"pageUnCurl";
animation.fillMode = kCAFillModeForwards;
animation.startProgress = 0.35;
[animation setRemovedOnCompletion:NO];
[locationMapView.layer addAnimation:animation forKey:#"pageUnCurlAnimation"];
[curlView removeFromSuperview];
;}
];
isCurlStarted = NO;
}
You'll probably need to go with OpenGL ES for this one. Core Animation exposes the ability to work with layers, even in 3-D, but all layers are just rectangles and they are manipulated as such. You can animate the flipping of a layer about an axis, even with a perspective distortion, but the kind of curving you want to do is more complex than you can manage using the Core Animation APIs.
You might be able to split your image up into a mesh of tiny layers and manipulate each using a CATransform3D to create this curving effect, but at that point you might as well be using OpenGL ES to create the same effect.
I am using Cocos2d engine and I've faced a strange problem.
I have a sprite. Also, I have 2 animations for that sprite. And I want to play one animation when app loads and second, after ccTouchevent is called.
walkAnim = [CCAnimation animation];
dropAnim = [CCAnimation animation];
for( int q=1;q<12;q++){
[walkAnim addFrameWithFilename: [NSString stringWithFormat:#"walkforward_%.2d.png", q]];
[dropAnim addFrameWithFilename: [NSString stringWithFormat:#"drop_%.2d.png", q]];
}
action = [CCAnimate actionWithAnimation:walkAnim];
action.duration = 2;
id act = [CCRepeatForever actionWithAction:action];
[sprite runAction:act];
So, here we see an animating sprite.
[sprite stopAllActions]; //and here my torture begins
I have tried many ways of creating an action:
I've tried to add another AnimateAction, tried to replace the current animation, but everything results in a crash.
[action setAnimation:dropAnim];
and
CCAnimate* animat = [[CCAnimate alloc]initWithDuration:30 animation:dropAnim restoreOriginalFrame:YES];
and
id action = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:dropAnim]];
[player1 runAction:action];
The crash is in [CCAnimate actionWithAnimation:]
+(id) actionWithAnimation: (CCAnimation*)anim
{
return [[[self alloc] initWithAnimation:anim restoreOriginalFrame:YES] autorelease];
}
Thanks!
To launch an action from another method, you have to retain the action Eg: [action retain];
To launch an action from another method, you have to retain the action
Eg: [action retain]; and then do not forget to release it
-(void)create{
for( int q=1;q<12;q++){
[playerWalkAnim addFrameWithFilename: [NSString stringWithFormat:#"walkforward_%.2d.png", q]];
}
playerAction = [CCAnimate actionWithAnimation:playerWalkAnim];
playerAction.duration = 2;
[playerAction retain];
}
-(void)launch{
[player1 runAction:playerAction];
}
I've successfully put together a mapview with a pin annotation representing the users current position that updates at a set interval. When the location manager updates the user's position, the pin annotation disappears and reappears at the new location. Has anyone played with getting the current user's GPS location to update through the use of an animation, like what is done in Apple's offical mapping application? If so, I'd love some pointers to get this to work. Thanks!
Solution 1:
First, you need to make your view controller implement MKMapViewDelegate if it doesn't already.
Then, implement this method:
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
MKAnnotationView *aV;
for (aV in views) {
CGRect endFrame = aV.frame;
aV.frame = CGRectMake(aV.frame.origin.x, aV.frame.origin.y - 230.0, aV.frame.size.width, aV.frame.size.height);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.45];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[aV setFrame:endFrame];
[UIView commitAnimations];
}
}
Add your annotations to the MapView and when they are added, this delegate method will be called and will animate the pins from top to bottom as they are added.
The values for timing and positioning can be changed a little bit but I've tweaked it to make it look best/closest to the traditional drop (as far as I've tested).
Solution 2:
Alternatively, if you're making a MKAnnotationView subclass, you can use didMoveToSuperview to trigger the animation. The following does a drop that ends in a slight 'squish' effect
#define kDropCompressAmount 0.1
#implementation MyAnnotationViewSubclass
...
- (void)didMoveToSuperview {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:#"transform"];
animation.duration = 0.4;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(0, -400, 0)];
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
CABasicAnimation *animation2 = [CABasicAnimation animationWithKeyPath:#"transform"];
animation2.duration = 0.10;
animation2.beginTime = animation.duration;
animation2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
animation2.toValue = [NSValue valueWithCATransform3D:CATransform3DScale(CATransform3DMakeTranslation(0, self.layer.frame.size.height*kDropCompressAmount, 0), 1.0, 1.0-kDropCompressAmount, 1.0)];
animation2.fillMode = kCAFillModeForwards;
CABasicAnimation *animation3 = [CABasicAnimation animationWithKeyPath:#"transform"];
animation3.duration = 0.15;
animation3.beginTime = animation.duration+animation2.duration;
animation3.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
animation3.toValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
animation3.fillMode = kCAFillModeForwards;
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = [NSArray arrayWithObjects:animation, animation2, animation3, nil];
group.duration = animation.duration+animation2.duration+animation3.duration;
group.fillMode = kCAFillModeForwards;
[self.layer addAnimation:group forKey:nil];
}
Hope this helps!
PK