gpt4 book ai didi

ios - 如何为 setFrame : with auto layout in Objective-C 设置动画

转载 作者:行者123 更新时间:2023-11-29 13:19:17 25 4
gpt4 key购买 nike

我正在开发一个 iOS (6) 应用程序,我有一个 View ,我更改了它的大小(动画),并且在该 View 中有一个 UILabel,我希望它随 View 一起增长,但不是按比例增长。

即如果 View 是特定大小,我需要 View 占据,比如 View 的 90%,但是当我把它变大(到另一个特定大小)时,我只希望它占据,比如 80%。这是为了容纳 UILabel 周围的其他 subview ,这些 subview 将在 super View 增长时变得可见。

我尝试过使用 NSLayoutContraints 并将前导空间设置为所需的空间,并设置 UILabel 的宽度以使其适当增长,我还尝试设置前导和尾随空格(因为宽度会自动增长)。

奇怪的是,在同一个动画代码块(使用 UIView animateWithDuration:...)中,我还有其他 subview 正在使用它们的 NSLayoutContraints,它们都可以工作。然而,所有这些都是间距限制,而不是大小限制,并且没有涉及 UILabels 所以我不知道这是否与它有任何关系......

然后我尝试设置 UILabel 的 translatesAutoresizingMaskIntoConstraints = YES,并且只在标签上使用 setFrame:,虽然这将标签的大小和位置设置在我想要的位置,但变化不是动画的,所以它看起来很糟糕。 ..

有没有其他人有过类似的经验或有什么想法可以帮助我?

谢谢

以下是我一直在尝试的一些代码,其中“left”是标签的前导空间约束,“width”是标签的宽度约束。

setFrame 方法在父 View Controller 的动画代码块中调用。

- (void)setFrame:(CGRect)frame {
[super setFrame:frame];

float gap = ((frame.size.height - 36) - 9 * [self.account.accountData numberOfBars]) / ([self.account.accountData numberOfBars] + 1);
for (NSLayoutConstraint *constraint in constraints) {
constraint.constant = gap;
}

self.left.constant = frame.size.width * 34/160 + (2 - 34 * 140 / 160);
self.width.constant = frame.size.width * 0.575 + 55.5;

[self layoutIfNeeded];

}

最佳答案

您应该能够使用 constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant: 在代码中执行此操作。一起使用乘数和常量应该可以。将标签的宽度设置为等于其父 View 的宽度,但乘数为 0.7 或 0.8,然后是一个常数,可以在较小的 View 尺寸下提供正确的宽度。随着 View 的增长,该常量值将占总宽度的较小部分。例如,乘数为 .7,常量为 20,如果父 View 的宽度为 100 点,则标签将为 90 点 (100*.7 +20)。如果 View 增长到 200,标签将是 160 宽(或父 View 的 80%)。

编辑后:

其实我觉得我上面说的方法行不通。在 WWDC 2012 的演讲“掌握自动布局的最佳实践”中,他们谈到了使用核心动画或直接对约束进行动画处理。我认为这是你必须做后者的情况。如果您使用核心动画,标签会在 View 动画时跳转到它的新大小,即使两者都在一个动画 block 内(至少它对我来说是这样——Rob,如果你知道更多,请发帖)。要直接(在 IOS 中)为约束设置动画,您必须使用计时器,并增加约束常量。您可以使用 NSTimer,但我发现由于其分辨率(50-100 毫秒)的限制,它有点不稳定。一种更流畅的方法是使用与显示刷新率相关的 CADisplayLink(我认为是 16.7 毫秒)。所以这是我使用的代码。我在 IB 中的设置是一个 190 宽的 View ,里面有一个 170 宽的标签。我对两者都有宽度限制(你需要为此添加 QuartzCore 框架)。

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface ViewController ()
@property (strong,nonatomic) CADisplayLink *displayLink;
@end

@implementation ViewController {

IBOutlet NSLayoutConstraint *labelWidthCon;
IBOutlet NSLayoutConstraint *viewWidthCon;
}


-(IBAction)startViewTimer:(id)sender {
[self startDisplayLink];
}

-(void)startDisplayLink {
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(expandView:)];
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}

- (void)stopDisplayLink {
[self.displayLink invalidate];
self.displayLink = nil;
}


-(void)expandView:(CADisplayLink *) link {
labelWidthCon.constant += 5;
viewWidthCon.constant +=10;
if (viewWidthCon.constant > 370) [self stopDisplayLink];
}

关于ios - 如何为 setFrame : with auto layout in Objective-C 设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14793370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com