Metronome
Metronome是节拍器的意思,用在艺术创作中,例如声乐中打节拍.
节拍器中间有个小竖棍,上面有个滑块,上下拨动滑块,就可以调节节拍的快拍.
app有一个配置页面,用来设置2/4,3/4,4/4拍.配置设置好后,调用
1 if (newIndexPath.row == 0) {
2 [appDelegate setTimeSignature:TimeSignatureTwoFour];
3 }
4 else if (newIndexPath.row == 1) {
5 [appDelegate setTimeSignature:TimeSignatureThreeFour];
6 }
7 else {
8 [appDelegate setTimeSignature:TimeSignatureFourFour];
9 }
通过
[self presentModalViewController:controller animated:YES];
来显示配置页面,之后通过
PreferencesViewControllerDelegate
的通知,来
dismissModalViewControllerAnimated
"关闭"配置页面.
关键代码:
这里涉及到多线程的操作
1 // Animation creates a separate thread.
2 - (void)startSoundAndAnimateArmToRight:(BOOL)startToRight {
3 if (armIsAnimating == YES) {
4 return;
5 }
6
7 // Start by animating arm to full extent of swing in the swiped direction.
8 if (startToRight == YES) {
9 [self rotateArmToDegree:kDisplacementAngle];
10 armIsAtRightExtreme = YES;
11 } else {
12 [self rotateArmToDegree:-kDisplacementAngle];
13 armIsAtRightExtreme = NO;
14 }
15
16 beatNumber = 1;
17 [self startDriverThread];//启动一个线程
18 armIsAnimating = YES;
19 }
线程执行函数:
1 // This method is invoked from the driver thread
2 - (void)startDriverTimer:(id)info {
3 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
4
5 // Give the sound thread high priority to keep the timing steady.
6 [NSThread setThreadPriority:1.0];
7 BOOL continuePlaying = YES;
8
9 while (continuePlaying) { // Loop until cancelled.
10
11 // An autorelease pool to prevent the build-up of temporary objects.
12 NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];
13
14 [self playSound];
15 [self performSelectorOnMainThread:@selector(animateArmToOppositeExtreme) withObject:nil waitUntilDone:NO];//调用主线程的animateArmToOppositeExtreme方法
16 NSDate *curtainTime = [[NSDate alloc] initWithTimeIntervalSinceNow:self.duration];
17 NSDate *currentTime = [[NSDate alloc] init];
18
19 // Wake up periodically to see if we've been cancelled.
20 while (continuePlaying && ([currentTime compare:curtainTime] != NSOrderedDescending)) {
21 if ([soundPlayerThread isCancelled] == YES) {
22 continuePlaying = NO;
23 }
24 [NSThread sleepForTimeInterval:0.01];
25 [currentTime release];
26 currentTime = [[NSDate alloc] init];
27 }
28 [curtainTime release];
29 [currentTime release];
30 [loopPool drain];
31 }
32 [pool drain];
33 }
个人时间关系,代码没有细看。大概意思应该是:线程不停的用timer来查询是否到时间,到时间的时候,播放个声音。还有一些界面显示的,用的是CA层的动画。