在macOS Objective-c

问题描述

我使用的是XCode 12,macOS,objective-c,而不是iOS。

我有一个包含3个项目的菜单(水平堆栈视图):

enter image description here

蓝色下划线是通过程序创建的:

- (void)viewDidLoad {

    [super viewDidLoad];     
    [self createAnimatedLineAtButton:self.importButton];
}

// Create line
- (void)createAnimatedLineAtButton:(NSButton*)button {

    // This view is layer backed
    SHViewCustomBackground* underlineView = [[SHViewCustomBackground alloc] init];
    
    underlineView.backgroundColor = [NSColor colorNamed:@"color_spot"]; // blue color
    underlineView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:underlineView];
    self.underlineView = underlineView;    

    [self updateAnimatedLineUnderButton:self.importButton];
}

接下来的代码将更新约束(最初在上面的行创建中调用),然后在单击每个按钮时进行更新。

- (void)updateAnimatedLineUnderButton:(NSButton *)button {
    
    // Constraints are stored here
    if (self.underlineConstraints) {
        [self.view removeConstraints:self.underlineConstraints];
    }

    [NSAnimationContext runAnimationGroup:^(NSAnimationContext *context){
        context.duration = 1;
        context.allowsImplicitAnimation = YES;
    
        NSView* underlineView = self.underlineView;
        NSDictionary *views = NSDictionaryOfVariableBindings(underlineView,button);
        self.underlineConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[button]-[underlineView(1)]" options:NSLayoutFormatAlignAllLeading | NSLayoutFormatAlignAllTrailing metrics:nil views:views];
        [self.view addConstraints:self.underlineConstraints];
    
        [self.view layoutSubtreeIfNeeded];
    } completionHandler:nil];    
}

这是按钮操作

- (IBAction)buttonClicked:(id)sender {
    
    [self updateAnimatedLineUnderButton:(NSButton*)sender];
}

现在我的问题是:当我按此顺序单击按钮“导入”>“导出”>“导入”时,该行将按照预期的方式从导入到导出再返回动画。每当我单击“占位符”(中间按钮)时,该行就会跳跃并且没有动画。当我随后单击“导入”或“导出”时,它也会跳转

enter image description here

我想知道这里出了什么问题。所有按钮正确连接,所有动作均正确触发。

编辑:比较所有按钮后,我发现导入和导出按钮(在它起作用的地方)都具有相同的宽度。

解决方法

这是一个主意……可行。最初,我打算按照自己的方式进行操作,但是,您可能不喜欢这样,最终产生了两个约束。但是效果很好。

enter image description here

这个想法与您的想法非常相似,但是不同之处在于,我创建了一个前导约束和尾随约束,并对其进行动画处理。

@implementation ViewController

- ( void ) viewDidLoad
{
    super.viewDidLoad;
    [self animateLine:self.longButton];
}

// Animate the line
- ( void ) animateLine:( NSButton * ) button
{
    [NSAnimationContext runAnimationGroup: ^ ( NSAnimationContext * context ) {

        context.duration = 1;
        context.allowsImplicitAnimation = YES;
        self.leadingConstraint.animator.constant = button.frame.origin.x;
        self.trailingConstraint.animator.constant = self.stackView.bounds.size.width - button.frame.origin.x - button.frame.size.width;
        NSLog ( @"Setting leading to %f trailing to %f",self.leadingConstraint.constant,self.trailingConstraint.constant );

        self.lineField.layoutSubtreeIfNeeded;

    }];
}

- ( IBAction ) buttonAction:( id ) sender
{
    [self animateLine:sender];
}

就是这样。其余的发生在情节提要中。我设置了约束,以便通过将第一项和第二项交换为口味来使距离有意义。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...