objective-c - 对 uiview 的 socket 集合应用许多约束并为其设置动画

标签 objective-c xcode animation nslayoutconstraint iboutletcollection

我有一个奥特莱斯标签集合。标签位于堆栈 View 的父级堆栈 View 中。当 View 加载时,我想让每个标签淡入并一个接一个地稍微向右移动。我可以在循环中应用约束来抵消它。但只有一个会动画回到最终位置。

-(void)viewDidLoad {
[super viewDidLoad];
for (UILabel *lbl in _constructionlabels) {
    lbl.alpha = 0.0;
    leadingCnst=[NSLayoutConstraint
    constraintWithItem:lbl
    attribute:NSLayoutAttributeLeading
    relatedBy:NSLayoutRelationEqual
    toItem:[lbl superview]
    attribute:NSLayoutAttributeLeading
    multiplier:1.0
    constant:-25];
    [self.view addConstraint:leadingCnst];
}

}

-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];

leadingCnst.constant = 0;
[UIView animateWithDuration:0.33 delay:2 options:UIViewAnimationOptionCurveEaseOut animations:^{
    for (UILabel *lbl in self->_constructionlabels) {
        lbl.alpha = 1.0;
    }
    [self.view layoutIfNeeded];
} completion:^(BOOL finished) {
}];

}

如何对每个需要的标签应用约束,然后依次为所有标签设置动画?

最佳答案

保留对每个标签约束的引用并立即启动所有动画,每个动画都有一个延迟。

// Declare array to hold references to constraints
NSMutableArray* _labelConstraints = [NSMutableArray array];

-(void) viewDidLoad {
    [super viewDidLoad];
    for (UILabel * lbl in _constructionlabels) {
        lbl.alpha = 0.0;

        NSLayoutConstraint* leadingCnst = [NSLayoutConstraint
            constraintWithItem: lbl
            attribute: NSLayoutAttributeLeading
            relatedBy: NSLayoutRelationEqual
            toItem: [lbl superview]
            attribute: NSLayoutAttributeLeading
            multiplier: 1.0
            constant: -25
        ];
        [self.view addConstraint: leadingCnst];

        // Add constraint reference
        [_labelConstraints addObject: @(leadingCnst)];
    }
}

-(void) viewDidAppear: (BOOL) animated {
    [super viewDidAppear: animated];

    for (i = 0; i < [_constructionlabels count]; i++) {

        // Get label
        Label* lbl = [_constructionlabels objectAtIndex:i];        

        // Get constraint
        NSLayoutConstraint* labelConstraint = [_labelConstraints objectAtIndex:i];      

        // Animate
        [UIView animateWithDuration: 0.33 delay: i options: UIViewAnimationOptionCurveEaseOut animations: ^ {
                lbl.alpha = 1.0;
                labelConstraint.constant = 0;
                [self.view layoutIfNeeded];
            }
            completion: ^ (BOOL finished) {}
        ];  
    }
}

注意:这只是一个概念证明 - 您可能需要重构代码。

(自从我写 ObjC 以来已经有一段时间了,如果你让我知道任何错误,我会纠正它们。)

关于objective-c - 对 uiview 的 socket 集合应用许多约束并为其设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61086179/

相关文章:

iphone - ios编程中切换UIViews的正确方法

r - ld : warning: text-based stub file are out of sync. 回退到库文件进行链接

objective-c - 升级到 Xcode 5.0.2 : xib warning. 属性不可用

ios - 在 iOS 中使用 NSDictionary 的可扩展 UITableview 单元格

xcode - 错误 ITMS-90096 : Your binary is not optimized for iPhone 5

ios - 如何为 Facebook Paper 应用程序的菜单按钮设置动画/变形

ios - 如何制作具有动态变化和循环文本的 UILabel?

jquery - 我如何动画显示 : none/block property on flex items?

ios - 覆盖本地化字符串的 NSManagedObject getter

iphone - 共享模型的联合 Mac 和 iPhone 应用程序的 Xcode 设置