gpt4 book ai didi

iOS 音序器速度 slider 导致应用程序崩溃

转载 作者:行者123 更新时间:2023-11-28 21:29:30 24 4
gpt4 key购买 nike

我使用 Objective C 创建了一个鼓音序器。我想要 slider 来控制速度。目前,一切正常,每一步之间的间隔由以下因素控制:

 while (self.running)
{
// sleep until the next step is due
[NSThread sleepUntilDate:time];

// update step
int step = self.step + 1;

// wrap around if we reached NUMSTEPS
if (step >= NUMSTEPS)
step = 0;

// store
self.step = step;

// time duration until next step
time = [time dateByAddingTimeInterval:0.2];
}

所以每一步之间的时间是0.2秒。我已经尝试在 View Controller .m 中实现这样的速度 slider ( slider 的范围为 0.3 到 1.0,因此将输出与当前时间相似的值):

- (IBAction)sliderMoved:(UISlider *)sender
{

AppDelegate* app = [[UIApplication sharedApplication] delegate];
app.tempo = sender.value;
}

并将 while(self.running) 线程中的行更改为:

time = [time dateByAddingTimeInterval: (NSTimeInterval) _tempo];

但是,这会导致步骤之间的时间太短(节奏快得离谱),并且当触摸应用中的任何控件时,它会崩溃。

我想知道我是否需要设置这样的功能,但我不确定里面有什么可以使速度 slider 工作:

- (void)setTempo:(float)tempo
{

}

我已经尽力说清楚了,如果有人能帮助我,我将不胜感激,在此先感谢

最佳答案

-(void) startDrumTick{
[self.myDrumTimer invalidate]; // stop any current existing timer

// perform the call to the method 'drumMethodOperation:'
// every 0.2 sec. NB: drumMethodOperation will run on main thread.
// this means that if you expect to do long-blocking operation,
// you will need to move that op to an async thread, in order to avoid
// the UI blocking

self.myDrumTimer = [NSTimer scheduledTimerWithTimeInterval:0.2
target:self
selector:@selector(drumMethodOperation:)
userInfo:nil
repeats:YES];
}

-(void)drumMethodOperation:(id)sender
{
// update step
int step = self.step + 1;

// wrap around if we reached NUMSTEPS
if (step >= NUMSTEPS)
step = 0;

// store
self.step = step;

// any other needed operation to run every 0.2 secs
}

下面是一个使用 GCD 进行异步线程管理的例子

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){

// Long blocking operation ( DO NOT PERFORM ANY UI OPERATION, like changing a text label, setting an image to an UIImageView, etc. )

[self myLongDbQuery];

dispatch_async(dispatch_get_main_queue(), ^(void){
//Perform you UI Updates here
self.myLabel.text = @"Query done!!!";
});
});

希望对你有帮助

关于iOS 音序器速度 slider 导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36722589/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com