在目标c中的可可mac OSX应用程序中的NSButton生长缩小动画

时间:2015-07-03 13:16:51

标签: objective-c macos animation nsbutton

我在目标c中有一个mac app。

其中,我希望我的按钮能在几秒钟后生长成动画。

我已经搜索了很多但在cocoa应用程序中找不到任何东西。

[UIView beginAnimations:nil context:NULL];
CGRect pos=v2.bounds;
pos.size.height=500;
pos.size.width=500;
[UIView setAnimationDuration:1.0];

[UIView setAnimationRepeatCount:15];
[UIView setAnimationRepeatAutoreverses:YES];
v2.bounds=pos;
[UIView commitAnimations];

我在ios中使用上面的代码来增加一个按钮但是如何在cocoa mac应用程序中实现这一点?

所以基本上我想要一个NSButton的成长和缩小动画。

请帮帮我......

2 个答案:

答案 0 :(得分:3)

在可可你有几种动画方式,你可能需要选择适合自己的方式。例如,您可以使用NSAnimationContext来执行此任务,并使用以下代码:

- (IBAction)buttonPressed:(id)sender {
    [self runAnimations:10];
}

- (void)runAnimations:(NSUInteger)repeatCount {

    CGFloat oldWidth = self.buttonWidthConstraint.constant;
    CGFloat oldHeight = self.buttonHeightConstraint.constant;

    [NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
        context.duration = 1.;
        self.buttonWidthConstraint.animator.constant = 500;
        self.buttonHeightConstraint.animator.constant = 500;
    } completionHandler:^{
            //change back to original size
            [NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
                context.duration = 1.;
                self.buttonWidthConstraint.animator.constant = oldWidth;
                self.buttonHeightConstraint.animator.constant = oldHeight;
            } completionHandler:^{
                if (repeatCount - 1 > 0) {
                    [self runAnimations:repeatCount - 1];
                }
            }];
    }];
}

最好不要直接使用bounds操作约束。但如果您真的需要 - 您可以将此代码用于您想要的任何内容。

WWDC 2013, Best Practices for Cocoa Animation中可以看到关于您在OSX上播放动画的所有选项的精彩视频,我建议您完全观看。

答案 1 :(得分:0)

以下是Swift5中的一些示例。

@IBOutlet weak var burpButton: NSButton!
@IBOutlet weak var rotateButton: NSButton!
@IBOutlet weak var flashButton: NSButton!


override func viewDidLoad() {
    super.viewDidLoad()

    rotateButton.wantsLayer = true
    flashButton.wantsLayer = true
    burpButton.wantsLayer = true

    flashButton.layer?.cornerRadius = 30
    flashButton.layer?.masksToBounds  = true
    flashButton.layer?.borderWidth = 2
    flashButton.layer?.borderColor = NSColor.blue.cgColor


    rotateButton.layer?.backgroundColor = NSColor.systemRed.cgColor
    flashButton.layer?.backgroundColor = NSColor.systemYellow.cgColor
    burpButton.layer?.backgroundColor = NSColor.systemBlue.cgColor
}



@IBAction func rotateButtonPressed(sender: NSButton) {
    runRotateAnimations(repeatCount: 10)

}

func runRotateAnimations(repeatCount: Int) {
    let rotate = CABasicAnimation(keyPath: "transform.rotation")
    rotate.fillMode = CAMediaTimingFillMode.forwards
    rotate.fromValue = 0.0
    rotate.toValue = CGFloat(Double.pi * -2.0)
    rotate.duration = 1
    rotate.repeatCount = Float.init(exactly: 1)!
    rotateButton.layer?.position = CGPoint.init(x: rotateButton.frame.midX, y: rotateButton.frame.midY)

    //Make(sender.frame.midX, sender.frame.midY)
    rotateButton.layer?.anchorPoint = CGPoint.init(x: 0.5, y: 0.5)
    rotateButton.layer?.add(rotate, forKey: nil)
}


@IBAction func burpButtonPressed(sender: NSButton) {
    runBurpAnimations()
}


func runBurpAnimations() {

    burpButton.layer?.anchorPoint = CGPoint.init(x: 0.5, y: 0.5)
    let burp = CABasicAnimation(keyPath: "transform.scale")
    burp.fromValue = 0.0
    burp.toValue = 1.50 //CGPoint(x: bigWidth, y: bigHeight)
    burp.duration = 0.35
    //burp.repeatCount = Float.init(exactly: 1)!
    burp.repeatCount = 2
    burp.autoreverses = true
    //burp.speed = 0.75

    burpButton.layer?.position = CGPoint.init(x: burpButton.frame.midX, y: burpButton.frame.midY)
    burpButton.layer?.add(burp, forKey: nil)

}


@IBAction func flashButtonPressed(sender: NSButton) {
    runFlashAnimations()
}

func runFlashAnimations() {
    let flash = CABasicAnimation(keyPath: "opacity")
    flash.duration = 0.3
    flash.fromValue = 1
    flash.toValue = 0.1
    flash.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
    flash.autoreverses = true
    flash.repeatCount = 2
    flashButton.layer?.add(flash, forKey: nil)
}

我从@SegaZero获得了一些想法。来自this medium article的一些。虽然文章是关于iOS的,但不难发现macOS的可用元素。