我正在使用 WatchKit 为 Apple Watch 开发一个应用程序,我需要在录制音频时实现循环进度,类似于 Apple 演示。
我不知道 WatchKit 是否包含默认执行此操作的功能,因此我创建了自己的图像(13 个图像,持续 12 秒[总共 209 KB]),并使用计时器进行更改。
这是我的源代码:
开始/停止录制的按钮操作
- (IBAction)recordAction {
NSLogPageSize();
NSLog(@"Recording...");
if (!isRecording) {
NSLog(@"Start!");
isRecording = YES;
_timerStop = [NSTimer scheduledTimerWithTimeInterval:12.0
target:self
selector:@selector(timerDone)
userInfo:nil
repeats:NO];
_timerProgress = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(progressChange)
userInfo:nil
repeats:YES];
} else {
NSLog(@"Stop :(");
[self timerDone];
}
}
计时器结束或再次点击按钮时执行的操作
-(void) timerDone{
NSLog(@"timerDone");
[_timerProgress invalidate];
_timerProgress = nil;
[recordButtonBackground setBackgroundImageNamed:[NSString stringWithFormat:@"ic_playing_elipse_12_%d", 12]];
counter = 0;
isRecording = NO;
}
更改进度的方法
- (void)progressChange
{
counter++;
NSLog(@"%d", counter);
NSString *image = [NSString stringWithFormat:@"ic_playing_elipse_12_%d", counter];
[recordButtonBackground setBackgroundImageNamed:image];
NSLog(image);
}
这是一个显示错误的 gif。它开始显示,但随机更改图像,直到几秒钟。 (注意:我已经检查过第一张图像是否有正确的进度)
更新(其他解决方案):使用 startAnimating
有一种新方法可以使用 startAnimatingWithImagesInRange:duration:repeatCount 来显示循环进度
您需要指定动画的图像范围和持续时间。请参阅下面的示例:
[self.myElement startAnimatingWithImagesInRange:NSMakeRange(0, 360) duration:myDuration repeatCount:1];
最佳答案
我相信这是 WatchKit 解释图像名称方式的副作用。
以数字结尾的图像名称用于表示动画图像中的帧,因此“ic_playing_elipse_12_10”被解释为名为“ic_playing_eclipse_12_1”的图像的第一帧,并且当您希望它显示第一个图像时,将显示第 10 个图像一个。
您应该能够只更改图像名称,这样数字就不是图像名称的最后一部分,它会修复它。
关于ios - Apple Watch 圆形进度(或径向弧形进度),图像错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29284104/