介绍
NSViewAnimation是Cocoa框架中专门用于动画效果实现的类。它通常被用来对NSView及其子视图进行动画处理,可以通过指定动画的起始状态和结束状态来实现各种动画效果。
使用NSViewAnimation需要创建一个NSViewAnimation对象,并设置一些属性,比如动画的持续时间、缓动函数等。接着,可以调用addAnimations方法来添加需要执行动画的NSView及其属性,然后调用startAnimation方法开始动画。
属性
duration
动画的持续时间,以秒为单位。默认值为0.5秒。
animationCurve
动画的缓动函数,用于控制动画过程中属性值的变化速度。可选值为:
- NSAnimationEaseInOut:先加速后减速。
- NSAnimationEaseIn:加速。
- NSAnimationEaseOut:减速。
- NSAnimationLinear:匀速。
默认值为NSAnimationEaseInOut。
frameRate
动画的帧率,表示每秒钟需要更新多少次视图的显示。默认值为30帧/秒。
animationBlockingMode
动画阻塞模式,用于指定在动画执行期间是否允许用户交互。可选值为:
- NSAnimationNonblockingThreaded:在另一个线程中执行动画,不会阻塞主线程,用户可以继续与应用程序交互。
- NSAnimationBlocking:在主线程中执行动画,会阻塞用户界面,用户无法进行交互。
默认值为NSAnimationBlocking。
方法
initWithDuration:animationCurve:
初始化方法,用于创建NSViewAnimation对象。该方法需要两个参数:
- duration:动画的持续时间,以秒为单位。
- animationCurve:动画的缓动函数。
addAnimations:
添加需要执行动画的NSView及其属性。该方法接受一个NSDictionary类型的参数,该参数包含了需要执行动画的NSView及其属性信息。例如,要对NSView的frame属性执行动画,可以创建一个NSDictionary对象,将NSViewFrameAttribute键映射到一个NSAnimationProgression对象。NSAnimationProgression对象包含了动画开始和结束时frame属性的值。
startAnimation
开始执行动画。
stopAnimation
停止动画。
clearStartAnimation
清除动画的起始状态。
clearEndAnimation
清除动画的结束状态。
setCurrentProgress:
设置动画的当前进度。参数为0.0到1.0之间的一个浮点数,表示动画已完成的百分比。
currentProgress
获取动画的当前进度。
setDelegate:
设置NSViewAnimation代理对象。
delegate
获取NSViewAnimation代理对象。
以上是NSViewAnimation类的一些常用属性和方法,它们提供了丰富的功能,可以帮助我们轻松实现各种动画效果。
示例
简单实现视图动画的效果,通过上下左右按键移动视图
OC的实现
- (IBAction)btn_Remove:(id)sender {
CGFloat upX = 0.0;
CGFloat upY = 0.0;
if ([[sender title] isEqualToString:@"上"]) {
upX = NSMinX(_customView.frame);
upY = _customView.frame.origin.y + 10;
}else if ([[sender title] isEqualToString:@"下"]) {
upX = NSMinX(_customView.frame);
upY = _customView.frame.origin.y - 10;
}else if ([[sender title] isEqualToString:@"左"]) {
upX = _customView.frame.origin.x - 10;
upY = _customView.frame.origin.y;
}else if ([[sender title] isEqualToString:@"右"]) {
upX = _customView.frame.origin.x + 10;
upY = _customView.frame.origin.y;
}
NSPoint endPoint = NSMakePoint(upX, upY);
[self startAnimation:_customView endPoint:endPoint];
}
-(void)startAnimation:(NSView *)animationTarget endPoint:(NSPoint)endPoint {
NSRect startFrame = [animationTarget frame];
NSRect endFrame = NSMakeRect(endPoint.x, endPoint.y, startFrame.size.width, startFrame.size.height);
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
animationTarget,NSViewAnimationTargetKey,
[NSValue valueWithRect:startFrame],NSViewAnimationStartFrameKey,
[NSValue valueWithRect:endFrame],NSViewAnimationEndFrameKey, nil];
NSViewAnimation *animation = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dictionary]];
// animation.delegate = self;
animation.duration = 1;
//NSAnimationBlocking阻塞
//NSAnimationNonblocking异步不阻塞
//NSAnimationNonblockingThreaded线程不阻塞
[animation setAnimationBlockingMode:NSAnimationBlocking];
[animation startAnimation];
}
Swift的实现
@IBAction func btn_Remove(_ sender: NSButton) {
var upX = 0.0
var upY = 0.0
switch sender .title {
case "上":
upX = customView.frame.origin.x
upY = customView.frame.origin.y + 10
case "下":
upX = customView.frame.origin.x
upY = customView.frame.origin.y - 10
case "左":
upX = customView.frame.origin.x - 10
upY = customView.frame.origin.y
case "右":
upX = customView.frame.origin.x + 10
upY = customView.frame.origin.y
default:
break
}
let endPoint = NSMakePoint(upX, upY)
self.startAnimation(animationTarget: customView, endPoint: endPoint)
}
func startAnimation(animationTarget:NSView, endPoint:NSPoint) {
let dict = NSMutableDictionary()
let startFrame = animationTarget.frame
let endFrame = NSMakeRect(endPoint.x, endPoint.y, startFrame.size.width, startFrame.size.height)
dict.setObject(animationTarget as Any, forKey: NSViewAnimation.Key.target as NSCopying)
dict.setObject(NSValue.init(rect: startFrame), forKey: NSViewAnimation.Key.startFrame as NSCopying)
dict.setObject(NSValue.init(rect: endFrame), forKey: NSViewAnimation.Key.endFrame as NSCopying)
let animation = NSViewAnimation.init(viewAnimations: NSArray.init(object: dict) as! [[NSViewAnimation.Key : Any]])
animation.start()
}
界面显示