gpt4 book ai didi

swift - 无法使用 animator() [ OSX ] 为 Swift 自定义属性设置动画

转载 作者:搜寻专家 更新时间:2023-11-01 05:38:33 26 4
gpt4 key购买 nike

我正尝试在 Objective-C 的 swift 中使用 animator() 为自定义属性设置动画。但是,当我尝试通过 animator 设置属性时,出现了 EXC_BAD_ACCESS 异常。在以前的一个用 Objective-C 编写的应用程序中,我使用了相同的想法并且成功了。

这是 Swift 中的类:

import Cocoa
import QuartzCore


@IBDesignable
class StopPlayButton: NSView {

//This is the property that I am trying to animate
@IBInspectable
var playButtonShapeInterval:Float = 1.0 {
didSet{
self.needsDisplay = true
}
}

@IBInspectable
var isPressed:Bool = false{
didSet{
self.needsDisplay = true
}
}
override func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect)
StyleKit.drawPlayStopButton(playButtonShapeInterval: CGFloat(self.playButtonShapeInterval), buttonPressed: self.isPressed)
}

func toggle(){
NSAnimationContext.beginGrouping()
NSAnimationContext.currentContext().duration = 1.0;
NSAnimationContext.currentContext().timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut);
if self.playButtonShapeInterval == 0 {
self.animator().playButtonShapeInterval = 1.0
}
else{
self.animator().playButtonShapeInterval = 0.0//exception happending here...
}
NSAnimationContext.endGrouping()
}
override static func defaultAnimationForKey(key: String) -> AnyObject?{
if key == "playButtonShapeInterval" {
return CABasicAnimation()
}
return super.defaultAnimationForKey(key)
}

}

请注意,如果我将属性名称从 playButtonShapeInterval 更改为 alphaValue,则 alpha 动画会起作用。

下面是上面的代码移植到Objective-C,做我想做的。我不确定有什么不同:

//StopPlayButton.m
#import "StopPlayButton.h"
#import "StyleKit.h"
#import <QuartzCore/QuartzCore.h>

@implementation StopPlayButton

- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
NSLog(@"This is the interval: %f", self.playButtonShapeInterval);
[StyleKit drawPlayStopButtonWithPlayButtonShapeInterval:self.playButtonShapeInterval buttonPressed:self.playButtonPressed];
}

-(void)setPlayButtonShapeInterval:(float)playButtonShapeInterval{
if(playButtonShapeInterval != _playButtonShapeInterval){
_playButtonShapeInterval = playButtonShapeInterval;
[self setNeedsDisplay:YES];
}
}

-(void)toggle{
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:0.5];
[[NSAnimationContext currentContext] setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
if(self.playButtonShapeInterval == 0){
self.animator.playButtonShapeInterval = 1;
}
else{
self.animator.playButtonShapeInterval = 0;
}
[NSAnimationContext endGrouping];
}

+(id)defaultAnimationForKey:(NSString *)key{
if ([key isEqualToString:@"playButtonShapeInterval"]) {
return [CABasicAnimation animation];
}
return [super defaultAnimationForKey:key];
}

@end

//StopPlayButton.h
#import <Cocoa/Cocoa.h>

@interface StopPlayButton : NSView

@property (nonatomic) BOOL playButtonPressed;
@property (nonatomic) float playButtonShapeInterval;

-(void)toggle;

@end

最佳答案

我遇到了同样的问题并通过将自定义属性声明为“动态”来修复它。我相信这是 KVO 的要求。

dynamic var playButtonShapeInterval:Float = 1.0 {
didSet{
self.needsDisplay = true
}
}

关于swift - 无法使用 animator() [ OSX ] 为 Swift 自定义属性设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33853708/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com