我有一个正在开发的简单 Mac 应用程序。我希望能够在 NSButton 上应用变换并使其更大。其大小的两倍。但是我的代码不起作用,它只是将按钮滑到一个角落。有谁知道出了什么问题?
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
[numberButton1.animator.layer setAffineTransform:CGAffineTransformMakeScale(4.0, 4.0)];
numberButton1.layer.anchorPoint = CGPointMake(0.5, 0.5);
[numberButton1.animator.layer setAffineTransform:CGAffineTransformMakeScale(1.0, 1.0)];
} completionHandler:nil];
更新 1
我尝试了以下,但它没有做任何事情:(
CGAffineTransform test = CGAffineTransformMakeScale(4.0, 4.0);
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *ctx) {
numberButton1.animator.layer.affineTransform = test;
} completionHandler:^{
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *ctx) {
numberButton1.animator.layer.affineTransform = CGAffineTransformIdentity;
} completionHandler:^{
NSLog(@"Done...");
}];
}];
更新 2
这是我的代码 sudo,希望这会有所帮助:
我的头文件(.h)文件:
#import <Cocoa/Cocoa.h>
#import <QuartzCore/QuartzCore.h>
@interface ViewController : NSViewController {
IBOutlet NSButton *numberButton1;
}
-(IBAction)demo:(id)sender;
@end
我的实现(.m)文件:
#import "ViewController.h"
@implementation ViewController
-(IBAction)demo:(id)sender {
CGRect frame = numberButton1.layer.frame;
CGPoint center = CGPointMake(CGRectGetMidX(frame), CGRectGetMidY(frame));
numberButton1.layer.position = center;
numberButton1.layer.anchorPoint = CGPointMake(0.5, 0.5);
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
context.allowsImplicitAnimation = YES;
[[NSAnimationContext currentContext] setDuration:0.2];
numberButton1.animator.layer.affineTransform = CGAffineTransformMakeScale(4.0, 4.0);
} completionHandler:^{
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
context.allowsImplicitAnimation = YES;
[[NSAnimationContext currentContext] setDuration:0.2];
numberButton1.animator.layer.affineTransform = CGAffineTransformIdentity;
} completionHandler:nil];
}];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
numberButton1.wantsLayer = YES;
numberButton1.layerContentsRedrawPolicy = NSViewLayerContentsRedrawOnSetNeedsDisplay;
numberButton1.layer.backgroundColor = [NSColor colorWithRed:(17/255.0) green:(145/255.0) blue:(44/255.0) alpha:1.0].CGColor;
}
- (void)setRepresentedObject:(id)representedObject {
[super setRepresentedObject:representedObject];
// Update the view, if already loaded.
}
@end
我也没有用于Auto Layout
我的 UI 文件。
谢谢,丹。