У меня есть простое приложение Mac, которое я разрабатываю. Я хочу иметь возможность применить преобразование в NSButton и сделать его больше. Дважды его размер. Однако мой код не работает, он просто перемещает кнопку в угол. Кто-нибудь знает, что не так?CGAffineTransformMakeScale не работает - OS X - Cocoa
[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];
Update 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...");
}];
}];
Update 2
Вот мой код Судо не делать, надеюсь, это поможет:
Мой заголовок (.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.
Спасибо, Дэн.
Привет, большое спасибо за ваш ответ. Поэтому в основном я делаю игру, и когда пользователь нажимает на NSButton, я хочу, чтобы он увеличился, а затем снова стал меньше. Это просто приятная забавная простая анимация, которую я хочу в приложении. – Supertecnoboff
Я установил allowImplicitAnimation на YES, и пока анимация теперь гладкая, она все еще не увеличивает кнопку. – Supertecnoboff
То, что вы делаете, не будет работать. См. Мое редактирование. –