How to permanently leave a CALayer rotated at its final location? - cocoa

I am trying to rotate a CALayer to a specific angle however, once the animation is done, the layer jumps back to its original position. How would I rotate the layer properly so that it stays at its final destination?
Here is the code that I am using
CABasicAnimation *rotationAnimation =[CABasicAnimation animationWithKeyPath:#"transform.rotation.z"]; //Rotate about z-axis
[rotationAnimation setFromValue:[NSNumber numberWithFloat:fromDegree]];
[rotationAnimation setToValue:[NSNumber numberWithFloat:toDegree]];
[rotationAnimation setDuration:0.2];
[rotationAnimation setRemovedOnCompletion:YES];
[rotationAnimation setFillMode:kCAFillModeForwards];
Any suggestions? Thank you.

Ok I fixed it by setting removeOnCompletion to NO.
[rotationAnimation setRemovedOnCompletion:NO];

You're adding an animation, but you aren't modifying the actual underlying property. After you create the animation, just set the layer's transform property to contain the same final result.

Related

Creating BezierPath from Texture in SpriteKit/Swift

So, basically, I have a sprite (.png) in SpriteKit, where most of the pixels are zero/no detail etc.
I wish to create a UIBezierPath of only the detail inside the Sprite, so later I can use containsPoint.
After a bunch of googling I found you can create a PhysicsBody using the path of the Texture in the sprite image, like this:
let texture = SKTexture(imageNamed: "myImage.png")
img.physicsBody = SKPhysicsBody(texture: texture, size: img.size)
This creates a physics body around only the detail inside the image, excluding any alpha value pixels, if you know what I mean.
Is there a way to create a BezierPath doing the same? or maybe creating a CGPath, then using that to create a BezierPath?
The reason is, I'm hoping to test if a CGPoint is inside this sprite, without using the physicsDelegate or physicsBodys.
Thanks in advance.

Scale NSView centered

I need to scale an NSView around it's center.
I tried using CATransform3DMakeScale and it worked to scale the view towards it's corner but I couldn't achieve to center it. I tried to set the layers anchor point to .5, .5 but that didn't work.
I tried using scaleUnitSquareToSize but I ran into the problem that this seems unresetable without a lot of work.
In the end I need to be able to set the scale of an NSView to something like 0.8 and have it zoom out around it's center and be able to set the scale to 1 again to reset it.
Added a frame fix for #fyasar's answer, works for me on MacOS:
CGRect frame = layer.frame;
layer.anchorPoint = CGPointMake(.5, .5);
layer.frame = frame; // Fix the location shift caused from anchor change
I also faced with smilar problem.
Please be sure exeute that line before construct CABasicAnimation.
layer.anchorPoint = CGPointMake(.5, .5);
So, that worked for me.
Good luck
If you can require 10.8, then you could call NSScrollView's function:
- (void)setMagnification:(CGFloat)magnification centeredAtPoint:(NSPoint)point

How to animate horizontal bar with right cap, by UIView animation in iPhone

I got wiered but unsurprising animation from call UIView animation, I don't know if it works the same by using CALayer animation. I guess it is.
please see this picture: what I want is to animate a horizontal bar growing horizontally, what I do is:
Subclass a UIView, called UIViewBar, and in its drawRect:rect method, I draw the shape of rectangle and a right cap, it's done by
CGContextMoveToPoint A
CGcontextAddLineToPoint B
CGContextAddArcToPoint D
CGContextAddLineToPoint C
//then close the path and fill the context
Then I call this UIViewBar (initWithFrame:rect) in my UIViewController, and the shape is what I want, looks fine. BUT when I perform [UIView animation] say from original rect to rectGrew it does not perform nicely. Instead, it perform as the picture says.
Well, after thinking, it's not totally unreasonable because when the UIView horizontally stretches itself, it does not know the cap will not be changed. So is there another way to do this?
Actually, I'm very new to drawing, graphic, and animation. What I know is that (if wrong please kindly correct me):
If I want to "draw something", I should override the UIView's drawRect method, and this is done by initing the UIView, should not or better not to be called outside. Say I drew a rectangle in a UIView by CGContextFillRect.....
But what if I want to animate this rectangle? I cannot make animation based on Quartz2D, but from UIView animation or CALayer(deeper tech), so What I have to do is to make the rectangle itself as a seperate UIViewRect seperately, so I can call the [UIViewRect animation] method, AM I RIGHT ON THIS
If so, I have to make every rectangle that I want to animate to be UIView respectively. Doesn't it affect performance? Or it is just the way apple prefers to do ?
Any suggestion will be much helpful, GREAT thanks.
Wow, it's the first time that no answer posted at all! TO answer my question: it requires to consider animate custom property of CALayer. The custom property is the AB width (the width without cap width, that should work). If someone wants detailed code. Please google custom property animate in CALayer.

TextGeometry to always face user?

I've added some text to my scene with THREE.TextGeometry, and the text seems to be stuck in whichever xy plane I place it. Any way to have it adjust to always be in plane with the screen- readable for the user?
Try
mesh.lookAt( camera.position );
The local z-axis of the mesh should then point toward the camera.
To make the text always face the screen (rather than the camera):
mesh.quaternion.copy(camera.quaternion);
Note that this differs from object.lookAt(camera); since this will result in different orientation depending on where on the screen the object is located.
Object3D.onBeforeRender can be used to update the orientation on each frame. It might be useful to disable frustum culling using Object3D.frustumCulled = false; to ensure that the callback always is triggered.

Replacing image in sprite - cocos2d game development of iPhone

I want to change the sprite image.
Say for example:
mainSprite=[Sprite spriteWithFile:#"redFile.png"];
[self addChild:mainSprite];
Here, Sprite is already added to a layer. I have mainSprite (pointer) which can access it.
If I change
[mainSprite setOpacity:150];
it works perfectly. But here I want to change the sprite image instead of opacity.
But I don't know how.
#sagar:
In cocos2d 0.99.x I use
[sprite setTexture:[[CCTextureCache sharedTextureCache] addImage:#"sprite.png"]];
It works. The next question is, how can I get back to my previous sprite? Thanks
Ok. Damn Simple.
I find it by R & D.
Texture2D *x=[[Texture2D alloc]initWithImage:[UIImage imageNamed:#"box-purple-dark.png"]];
[mainSprite setTexture:x];
Replace image in sprite :
[sprite setTexture:[[CCTextureCache sharedTextureCache] addImage:#"image.png"]];
Replace image in sprite :
CCTexture2D *tex1 = [[CCTextureCache sharedTextureCache] addImage:#"new-image.png"];
[mainSprite setTexture:tex1];

Resources