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
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.
1 Answer
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);
- mac 如何实现一个view自转的动画
- Android实现图片的自转
- Android自定义View: 如何实现类钟摆的动画效果?
- HTML5实现的多边形球体自转效果源码
- 我的自转
- 如何写一个点击view带动画的下滑展开显示隐藏内容的控件
- 一个滑出view 的动画
- 在Android中动画移动一个View的位置,采用Scroller类实现
- 实现一个view从顶部移到底部的动画 and 将RGB值转化为颜色
- RelativeLayout实现叠加View的动画效果
- Android自定义View的动画实现方法
- 实现一个View的横线
- 一个圣诞动画的实现
- 一个圣诞动画的实现
- 一个圣诞动画的实现
- 一个圣诞动画的实现
- 一个圣诞动画的实现
- 一个圣诞动画的实现
- 我的vim配置文件
- 算法设计与分析 - Week 3 分治策略
- hdu 5344 MZL's xor (15多校第五场1002)
- OC中属性和成员变量的区别和使用
- 【JavaScript】Javascript全局观
- mac 如何实现一个view自转的动画
- TypeError:can't use a string pattern on bytes-like object
- 使用U盘安装ubuntu 14.04
- C/C++语法知识精华整理(2)-栈与堆、内存四大区域、 全局变量、线程技术、位字段、函数返回值副本机制等
- iOS之UIView的圆角
- Android中visibility属性VISIBLE、INVISIBLE、GONE的区别
- PHP检测链接是否是SSL连接 ,也就是判断HTTPS
- 在VS2010下编写C应注意的问题(和编写C++的区别)
- 软件自动化实现的原理