forked from iimgal/StudyiOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccelerometerViewController.m
More file actions
162 lines (137 loc) · 4.87 KB
/
Copy pathAccelerometerViewController.m
File metadata and controls
162 lines (137 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// AccelerometerViewController.m
// StudyiOS
//
// Created by on 11-10-13.
// Copyright (c) 2011年 ZhangYiCheng. All rights reserved.
//
#import "AccelerometerViewController.h"
#import "GraphView.h"
#import "AccelerometerFilter.h"
#define kUpdateFrequency 60.0
#define kLocalizedPause NSLocalizedString(@"Pause","pause taking samples")
#define kLocalizedResume NSLocalizedString(@"Resume","resume taking samples")
@interface AccelerometerViewController()
// Sets up a new filter. Since the filter's class matters and not a particular instance
// we just pass in the class and -changeFilter: will setup the proper filter.
-(void)changeFilter:(Class)filterClass;
@end
@implementation AccelerometerViewController
@synthesize unfiltered, filtered, pause, filterLabel;
// Implement viewDidLoad to do additional setup after loading the view.
-(void)viewDidLoad
{
[super viewDidLoad];
// 增加Code按钮,可跳转至教学页面
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"Code" style:UIBarButtonItemStyleBordered target:self action:@selector(code)];
self.navigationItem.rightBarButtonItem = item;
isPaused = NO;
useAdaptive = NO;
// 设置过滤器
[self changeFilter:[LowpassFilter class]];
// 设置加速计采集频率(单位:秒)
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0 / kUpdateFrequency];
// 设置委托(注意对象销毁时要将委托设置为空)
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
[unfiltered setIsAccessibilityElement:YES];
[unfiltered setAccessibilityLabel:NSLocalizedString(@"unfilteredGraph", @"")];
[filtered setIsAccessibilityElement:YES];
[filtered setAccessibilityLabel:NSLocalizedString(@"filteredGraph", @"")];
}
-(void)viewDidUnload
{
[super viewDidUnload];
self.unfiltered = nil;
self.filtered = nil;
self.pause = nil;
self.filterLabel = nil;
}
// 本对象销毁时将Accelerometer的委托设置为空,否则会继续触发委托方法导致报错
- (void)dealloc
{
[[UIAccelerometer sharedAccelerometer] setDelegate:nil];
}
// 加速计委托方法,根据设置的采集频率定时调用
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
//NSLog([NSString stringWithFormat:@"%@", [NSDate date]]);
if (acceleration == nil) {
return;
}
// Update the accelerometer graph view
if(!isPaused)
{
// 过滤加速计数据
[filter addAcceleration:acceleration];
// 取3个方向的值
[unfiltered addX:acceleration.x y:acceleration.y z:acceleration.z];
// 过滤后绘图
[filtered addX:filter.x y:filter.y z:filter.z];
}
}
// 改变过滤器
-(void)changeFilter:(Class)filterClass
{
// Ensure that the new filter class is different from the current one...
if(filterClass != [filter class])
{
filter = [[filterClass alloc] initWithSampleRate:kUpdateFrequency cutoffFrequency:5.0];
// Set the adaptive flag
filter.adaptive = useAdaptive;
// And update the filterLabel with the new filter name.
filterLabel.text = filter.name;
}
}
// 开始与暂停
-(IBAction)pauseOrResume:(id)sender
{
if(isPaused)
{
// If we're paused, then resume and set the title to "Pause"
isPaused = NO;
[pause setTitle:kLocalizedPause forState:UIControlStateNormal];
[pause resignFirstResponder];
}
else
{
// If we are not paused, then pause and set the title to "Resume"
isPaused = YES;
[pause setTitle:kLocalizedResume forState:UIControlStateNormal];
}
// Inform accessibility clients that the pause/resume button has changed.
UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, nil);
}
-(IBAction)filterSelect:(id)sender
{
if([sender selectedSegmentIndex] == 0)
{
// Index 0 of the segment selects the lowpass filter
[self changeFilter:[LowpassFilter class]];
}
else
{
// Index 1 of the segment selects the highpass filter
[self changeFilter:[HighpassFilter class]];
}
// Inform accessibility clients that the filter has changed.
UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, nil);
}
-(IBAction)adaptiveSelect:(id)sender
{
// Index 1 is to use the adaptive filter, so if selected then set useAdaptive appropriately
useAdaptive = [sender selectedSegmentIndex] == 1;
// and update our filter and filterLabel
filter.adaptive = useAdaptive;
filterLabel.text = filter.name;
// Inform accessibility clients that the adaptive selection has changed.
UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, nil);
}
// 跳转至教学页面
- (void)code
{
CodeViewController *controller = [[CodeViewController alloc] initWithNibName:@"CodeViewController" bundle:nil];
NSString *name = [NSString stringWithUTF8String:object_getClassName(self)];
controller.className = name;
[self.navigationController pushViewController:controller animated:YES];
}
@end