繁体   English   中英

创建计时器并使用条形图表示剩余时间

[英]Creating a timer and using a bar to represent remaining time

我目前正在为游戏创建计时器,但对于以条形表示剩余时间的最佳方法感到有些困惑。 我已经启动并运行计时器,我将使用它来缩放图像以表示剩余时间:

经过的时间/总时间=经过的时间百分比(x100为PTE,但更容易了)Image Width =(1-PTE)* StartImageWidth

我找不到以这种格式缩放图像的任何简便方法,因为宽度参数似乎是只读的,并且如果我没有记错的话,缩放功能是4.x及更高版本? 有人知道这样做的最好方法吗?

谢谢,艾略特

尝试这个

[UIView animateWithDuration:0.1 animations: ^ {
 CGRect frame = CGRectMake(xMargin, yMargin, width, height);
 myBar.frame = frame;
}];

尝试将条形图图像转换为StretchableImage。 我做了类似的事情,但是更新了一天中的时间。 这是我的代码:

UIImage *dayPassed = [UIImage imageNamed:@"dayPassedSmall.png"];
UIImage *passed = [dayPassed stretchableImageWithLeftCapWidth:20.0 topCapHeight:0.0];

UIImageView *dayPassedView = [[[UIImageView alloc] initWithFrame:dayFrame] autorelease];
dayPassedView.image = passed;
[self.view addSubview:dayPassedView];

然后,您可以稍后使用以下方法调整宽度:

CGRect imageFrame = dayPassedView.frame;
double relWidth = (1-PTE)*width;
imageFrame.size.width = relWidth;
dayPassedView.frame = imageFrame;

如果您希望为从旧框架到新框架的更改添加动画效果,则可以,但是我认为最主要的是图像必须具有可拉伸性。

暂无
暂无

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

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