简体   繁体   English

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

[英]Animating a UIView in a .xib file

I've made a program in Xcode that tells the time. 我已经用Xcode编写了一个告诉时间的程序。 I want to have a rectangle that animates/grows longer as the seconds increase (time progresses), and starts again when the seconds reach 60 (and go back to 0). 我希望有一个矩形,随着秒数的增加(时间进度),动画/生长的时间会更长,并在秒数达到60(然后回到0)时重新开始。 I don't have storyboarding and have already started so I don't want to go back and start again. 我没有情节提要,并且已经开始,所以我不想回去再开始。

Should I use a UIView instance and animate the 'frame' property? 我应该使用UIView实例并为'frame'属性设置动画吗?

Here is my clock code: 这是我的时钟代码:

- (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];
}

Yes, you can use a UIView and animate its frame. 是的,您可以使用UIView并为其框架设置动画。

You shouldn't necessarily do this using a timer though, just set the animation to run for the full duration and the complete frame height change and use a timer to restart the next animation. 不过,您不必一定要使用计时器来执行此操作,只需将动画设置为在整个持续时间内运行,并更改整个帧的高度,然后使用计时器重新启动下一个动画即可。 This will give the smoothest animation. 这将提供最流畅的动画。

For updating the time display you only need a timer running once per second. 要更新时间显示,您只需要每秒运行一次计时器即可。

Look at running the animation like this: 看一下这样运行动画:

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;
}];

There's a good guide here . 有一个很好的指导这里

This code would go in your updateTime method, which I'm assuming would be called each minute by a timer. 这段代码将放在您的updateTime方法中,我假设它将被计时器每分钟调用一次。 You need to correct the name of expandingTimeView for whatever your view property is called, and you need to replace MAX_FRAME_HEIGHT with the maximum height the view should grow to during the animation. 无论您调用哪种view属性,都需要更正expandingTimeView的名称,并且需要将MAX_FRAME_HEIGHT替换为动画期间视图应增长到的最大高度。

You may want to have 2 methods, 1 containing the view animation ( updateTimeView ), called each minute and 1 containing the label text update ( updateTimeLabels ), called each second. 您可能希望有2种方法,一种包含每分钟调用一次的视图动画( updateTimeView ),另一种包含每秒调用一次的标签文本更新( updateTimeLabels )的方法。 In this case the 2 timers should be created in viewWillAppear: 在这种情况下,应该在viewWillAppear:创建两个计时器viewWillAppear:

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

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