mac 如何实现一个view自转的动画

来源:互联网 发布:淘宝口碑好的大码女装 编辑:程序博客网 时间:2024/05/15 23:43

让NSView围绕中心点旋转,只要注意view中layer的anchorPoint即可,下面是代码片段,最后跟着的是stackoverflowd的帖子


       NSImage *rimage = rb.GetNativeImageNamed(IDR_X115_CHECK_VERSION_LOADING).ToNSImage();

       rotatoinWidth_ = [rimagesize].width;

       rotationImageView.reset([[[NSImageViewalloc] initWithFrame:NSMakeRect(196,156+rotatoinWidth_/2.0,rotatoinWidth_,rotatoinWidth_)]autorelease]);

        [selfaddSubview:rotationImageView];

        [rotationImageViewsetImage:rimage];

        [selfsetWantsLayer:YES];

        [rotationImageViewsetWantsLayer:YES];

       CABasicAnimation *animation = [CABasicAnimation

                                      animationWithKeyPath:@"transform" ];

        animation.fromValue = [NSValuevalueWithCATransform3D:CATransform3DIdentity];

       //围绕Z轴旋转,垂直与屏幕

        animation.toValue = [NSValue valueWithCATransform3D:

                            CATransform3DMakeRotation(M_PI,0.0,0.0,1.0) ];

        animation.duration =1;

        animation.cumulative =YES;

        animation.repeatCount =100000;

        [rotationImageViewlayer].anchorPoint =CGPointMake(0.5,0.5);

        [[rotationImageViewlayer]addAnimation:animationforKey:nil];




http://stackoverflow.com/questions/14839335/core-animation-set-anchorpoint-on-10-8-to-rotate-a-layer-about-its-center

Core Animation: set anchorPoint on 10.8 to rotate a layer about its center

up vote3down votefavorite
2

NB: This is for a Cocoa app on OS X, NOT iOS.

I have a layer-backed NSButton (subclass of NSView). What I want to do is rotate that button using Core Animation. I'm using the following code to do it:

CABasicAnimation *a = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];a.fromValue = [NSNumber numberWithFloat:0];a.toValue = [NSNumber numberWithFloat:-M_PI*2];[_refreshButton.layer setAnchorPoint:CGPointMake(0.5, 0.5)];a.duration = 1.8; // secondsa.repeatCount = HUGE_VAL;[_refreshButton.layer addAnimation:a forKey:nil];

This works, EXCEPT that when it runs, the layer jumps down and to the left so that its center point is at the origin point of the NSView, which is the lower-left corner at (0,0). The layer then rotates about its center, but obviously the jumping-to-the-lower-left-corner is not acceptable.

So, after much reading, I found this line in the 10.8 API release notes:

On 10.8, AppKit will control the following properties on a CALayer (both when "layer-hosted" or "layer-backed"): geometryFlipped, bounds, frame (implied), position, anchorPoint, transform, shadow*, hidden, filters, and compositingFilter. Use the appropriate NSView cover methods to change these properties.

This means that AppKit is "ignoring" my call to -setAnchorPoint in the code above and, instead, is setting that anchor point to the NSView's origin (0,0).

My question is: how do I solve this? What is the "appropriate NSView cover method" to set the anchorPoint for the layer (I can't find such a method on NSView). At the end of the day, I just want my button to rotate around its center point, indefinitely.

shareedit
 

1 Answer

activeoldestvotes
up vote10down voteaccepted

I don't see any methods on NSView that are a direct “cover” for anchorPoint.

What I do see in the 10.8 release notes, besides what you quoted, is this:

The anchorPoint is also always set to be (0,0), …

The anchorPoint controls which point of the layer is at position in the superlayer's coordinate system. NSView sets self.layer.anchorPoint to (0,0), which means the layer's lower-left corner is at self.layer.position.

When you set anchorPoint to (0.5,0.5), that means the center of the layer should be at the layer's position. Since you didn't modify position, this has the effect of moving the layer down and to the left, as you are seeing.

You need to compute the position you want the layer to have when its anchorPoint is (0.5,0.5), like this:

CGRect frame = _refreshButton.layer.frame;CGPoint center = CGPointMake(CGRectGetMidX(frame), CGRectGetMidY(frame));_refreshButton.layer.position = center;_refreshButton.layer.anchorPoint = CGPointMake(0.5, 0.5);
shareedit
 
 
Perfect! You know; I saw the position property but didn't give it a try because I read the note above that says AppKit is going to control it. Thanks for the solution! –  Bryan Feb 13 '13 at 1:15
 
Any idea why the rotation M_PI/2 would rotate counter clockwise the first time and clockwise after that? – greg Jul 8 '14 at 4:23
 
I would have to see your code. You should post your own question. –  rob mayoff Jul 8 '14 at 5:36
 
Here you go: stackoverflow.com/questions/24623725/… –  greg Jul 8 '14 at 15:16
0 0
原创粉丝点击