gpt4 book ai didi

ios - 在 .xib 文件中对 UIView 进行动画处理

转载 作者:行者123 更新时间:2023-11-29 03:54:47 24 4
gpt4 key购买 nike

我在 Xcode 中编写了一个可以显示时间的程序。我想要一个随着秒数增加(时间进展)而动画/变长的矩形,并在秒数达到 60 时再次开始(并返回到 0)。我没有 Storyboard并且已经开始了,所以我不想回去重新开始。

我应该使用 UIView 实例并为“frame”属性设置动画吗?

这是我的时钟代码:

- (void)viewDidLoad
{
[self updateTime];

[super viewDidLoad];

hourFormatter = [[NSDateFormatter alloc] init];
hourFormatter.dateFormat = @"HH";
minuteFormatter = [[NSDateFormatter alloc] init];
minuteFormatter.dateFormat = @"mm";
secondFormatter = [[NSDateFormatter alloc] init];
secondFormatter.dateFormat = @"ss";
}

- (void)updateTime {

[updateTimer invalidate];
updateTimer = nil;

currentTime = [NSDate date];
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setTimeStyle:NSDateFormatterMediumStyle];

hourLabel.text = [hourFormatter stringFromDate: currentTime];
minuteLabel.text = [minuteFormatter stringFromDate: currentTime];
secondLabel.text = [secondFormatter stringFromDate: currentTime];

updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
}

最佳答案

是的,您可以使用 UIView 并为其框架设置动画。

不过,您不必使用计时器来执行此操作,只需将动画设置为在整个持续时间和完整的帧高度更改内运行,然后使用计时器重新启动下一个动画即可。这将提供最流畅的动画。

要更新时间显示,您只需要一个每秒运行一次的计时器。

看看像这样运行的动画:

CGRect frame = self.expandingTimeView.frame;
frame.height = 0;
self.expandingTimeView.frame = frame;

[UIView animateWithDuration:60
animations:^{
frame.height = MAX_FRAME_HEIGHT;
self.expandingTimeView.frame = frame;
}];

有一个很好的指南 here .

此代码将放入您的 updateTime 方法中,我假设计时器每分钟都会调用该方法。无论调用什么 View 属性,您都需要更正 expandingTimeView 的名称,并且需要将 MAX_FRAME_HEIGHT 替换为 View 在动画期间应增长到的最大高度。

您可能需要 2 个方法,其中 1 个包含 View 动画 (updateTimeView),每分钟调用一次,另外 1 个包含标签文本更新 (updateTimeLabels),每分钟调用一次第二。在这种情况下,应在 viewWillAppear:

中创建 2 个计时器

关于ios - 在 .xib 文件中对 UIView 进行动画处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16637337/

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