ios - 动画条随着时间的增加而上升

标签 ios nstimer uianimation

我有一些代码以不同的部分(小时、分钟、秒)显示时间。

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

我想要随着时间的增加而上升的三个条形(小时、分钟、秒)。就像这个 cydia 锁屏调整:http://patrickmuff.ch/repo/

编辑:我应该补充一点,我对 Objective-C 非常陌生,而且非常缺乏经验,因此非常感谢任何提示/帮助!

最佳答案

您当前正在获取单位数(小时、分钟、秒)的文本 - 您需要获取这些数字的 floatint 版本并使用它们设置“盒子”的框架(应该是 UIView 实例)。

您显示的屏幕截图实际上是通过将 View 框架全部设置为相同大小并使用半透明背景色来完成的。然后,每个 View 都有一个 subview ,您可以在其中使用单位值设置框架高度。并且 subview 将具有完全不透明的背景颜色。

当然,这一切都可以通过核心图形来完成,而不是使用 View 。


好的,从您的评论来看,不错,背景很好。在您的 XIB 中,创建 3 个 View 并将它们放置在背景区域上。将 socket 连接到它们,以便您可以在代码中使用它们。将它们的背景颜色和高度设置为零。

在您的代码中,每次获得新的单位值时,都会修改 View 框架(以增加高度并减少“y”位置),例如(写出我的头顶):

NSInteger hours = ...;

CGRect frame = self.hoursView.frame;

if ((NSInteger)frame.size.height != hours) { // check if we need to modify
    frame.origin.y -= (hours - frame.size.height);
    frame.size.height = hours;

    self.hoursView.frame = frame;
}

关于ios - 动画条随着时间的增加而上升,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18031549/

相关文章:

objective-c - 按下 UIButton 后 UILabel 显示 3 秒

ios - 以编程方式创建自定义 View ,然后为其设置动画

ios - 为许多图像设置动画时出现内存问题

ios - 如何在不将views设置为零的情况下进行水平翻转动画?

ios - Swift - 使用 NEVPNManager 连接到 VPN

ios - 为 previewingGestureRecognizerForFailureRelationship 设置委托(delegate)会引发异常

ios - 通过 NSTimer 在 ViewDidLoad 中调用函数 - swift

iphone - 为 NSTimer 添加暂停功能

android - 线程中的 KMM In CorrectDereferenceException

ios - 如何在 Swift 中监控 WKWebView 页面加载进度?