1

Based similar questions asked about UIView and UITextField shake animations, Ive implemented this code which should apply to NSTextFields. However the animation never runs. What is the problem here?

NSRect textFieldFrame = [textfield frame];

CGFloat centerX = textFieldFrame.origin.x;
CGFloat centerY = textFieldFrame.origin.y;

CABasicAnimation *animation = [[CABasicAnimation alloc] init];
animation.keyPath = @"position";
animation.duration = 0.07;
animation.repeatCount = 4;
animation.autoreverses = true;

NSPoint one = NSMakePoint(centerX-5, centerY);
NSPoint two = NSMakePoint(centerX+5, centerY);

animation.fromValue = [NSValue valueWithPoint:one];
animation.toValue = [NSValue valueWithPoint:two];

[textfield.layer addAnimation:animation forKey:@"position"];

Note: [[textfield animator] setFrameOrigin:one]; successfully moves the textfield

4

1 回答 1

3

我最终使用了循环NSAnimationContext动画组,每个动画组在完成时调用另一个动画组。如果有更好的方法来做到这一点,我仍在寻找。但是如果其他人需要这个是我的解决方案

NSRect textFieldFrame = [textfield frame];

CGFloat centerX = textFieldFrame.origin.x;
CGFloat centerY = textFieldFrame.origin.y;

NSPoint origin = NSMakePoint(centerX, centerY);
NSPoint one = NSMakePoint(centerX-5, centerY);
NSPoint two = NSMakePoint(centerX+5, centerY);


[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setCompletionHandler:^{

    [NSAnimationContext beginGrouping];
    [[NSAnimationContext currentContext] setCompletionHandler:^{


        [NSAnimationContext beginGrouping];
        [[NSAnimationContext currentContext] setCompletionHandler:^{

            [NSAnimationContext beginGrouping];
            [[NSAnimationContext currentContext] setCompletionHandler:^{

                [[NSAnimationContext currentContext] setDuration:0.0175];
                [[NSAnimationContext currentContext] setTimingFunction: [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseOut]];
                [[textfield animator] setFrameOrigin:origin];

            }];

            [[NSAnimationContext currentContext] setDuration:0.0175];
            [[NSAnimationContext currentContext] setTimingFunction: [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseOut]];
            [[textfield animator] setFrameOrigin:two];
            [NSAnimationContext endGrouping];

        }];

        [[NSAnimationContext currentContext] setDuration:0.0175];
        [[NSAnimationContext currentContext] setTimingFunction: [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseOut]];
        [[textfield animator] setFrameOrigin:one];
        [NSAnimationContext endGrouping];
    }];

    [[NSAnimationContext currentContext] setDuration:0.0175];
    [[NSAnimationContext currentContext] setTimingFunction: [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseOut]];
    [[textfield animator] setFrameOrigin:two];
    [NSAnimationContext endGrouping];

}];

[[NSAnimationContext currentContext] setDuration:0.0175];
[[NSAnimationContext currentContext] setTimingFunction: [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseOut]];
[[textfield animator] setFrameOrigin:one];
[NSAnimationContext endGrouping];
于 2015-03-30T22:46:48.437 回答