繁体   English   中英

对.xib文件中的UIView进行动画处理

[英]Animating a UIView in a .xib file

我已经用Xcode编写了一个告诉时间的程序。 我希望有一个矩形,随着秒数的增加(时间进度),动画/生长的时间会更长,并在秒数达到60(然后回到0)时重新开始。 我没有情节提要,并且已经开始,所以我不想回去再开始。

我应该使用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;
}];

有一个很好的指导这里

这段代码将放在您的updateTime方法中,我假设它将被计时器每分钟调用一次。 无论您调用哪种view属性,都需要更正expandingTimeView的名称,并且需要将MAX_FRAME_HEIGHT替换为动画期间视图应增长到的最大高度。

您可能希望有2种方法,一种包含每分钟调用一次的视图动画( updateTimeView ),另一种包含每秒调用一次的标签文本更新( updateTimeLabels )的方法。 在这种情况下,应该在viewWillAppear:创建两个计时器viewWillAppear:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM