Created
January 19, 2012 15:28
-
-
Save tikitikipoo/1640600 to your computer and use it in GitHub Desktop.
移動平均法サンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CYMainViewController.h | |
// IdoAvg | |
// | |
// Created by tikitikipoo on 11/12/12. | |
// Copyright (c) 2011 __MyCompanyName__. All rights reserved. | |
// | |
#import "CYFlipsideViewController.h" | |
@interface CYMainViewController : UIViewController <CYFlipsideViewControllerDelegate> | |
@property (nonatomic, strong) NSMutableDictionary* loopCnt; | |
- (IBAction)showInfo:(id)sender; | |
- (IBAction)tapButton:(id)sender; | |
@end | |
// | |
// CYMainViewController.m | |
// IdoAvg | |
// | |
// Created by tikitikipoo on 11/12/12. | |
// Copyright (c) 2011 __MyCompanyName__. All rights reserved. | |
// | |
#import "CYMainViewController.h" | |
@implementation CYMainViewController | |
@synthesize loopCnt; | |
- (void)didReceiveMemoryWarning | |
{ | |
[super didReceiveMemoryWarning]; | |
// Release any cached data, images, etc that aren't in use. | |
} | |
#pragma mark - View lifecycle | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view, typically from a nib. | |
} | |
- (void)viewDidUnload | |
{ | |
[super viewDidUnload]; | |
// Release any retained subviews of the main view. | |
// e.g. self.myOutlet = nil; | |
} | |
- (void)viewWillAppear:(BOOL)animated | |
{ | |
[super viewWillAppear:animated]; | |
// 移動平均のインデックスポインタ | |
self.loopCnt = [[NSMutableDictionary alloc] init]; | |
} | |
- (void)viewDidAppear:(BOOL)animated | |
{ | |
[super viewDidAppear:animated]; | |
} | |
- (void)viewWillDisappear:(BOOL)animated | |
{ | |
[super viewWillDisappear:animated]; | |
} | |
- (void)viewDidDisappear:(BOOL)animated | |
{ | |
[super viewDidDisappear:animated]; | |
} | |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation | |
{ | |
// Return YES for supported orientations | |
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); | |
} | |
#pragma mark - Flipside View | |
- (void)flipsideViewControllerDidFinish:(CYFlipsideViewController *)controller | |
{ | |
[self dismissModalViewControllerAnimated:YES]; | |
} | |
- (IBAction)showInfo:(id)sender | |
{ | |
CYFlipsideViewController *controller = [[[CYFlipsideViewController alloc] initWithNibName:@"CYFlipsideViewController" bundle:nil] autorelease]; | |
controller.delegate = self; | |
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; | |
[self presentModalViewController:controller animated:YES]; | |
} | |
// 移動平均を格納するインデックス値 | |
int _loopIndex = -1; | |
- (IBAction)tapButton:(id)sender | |
{ | |
// 現在のタイムスタンプ値を取得 | |
NSTimeInterval timestamp = [[NSDate date] timeIntervalSince1970]; | |
NSNumber* timeNum = [NSNumber numberWithDouble:timestamp]; | |
NSInteger timeInt = [timeNum intValue]; | |
// 0~5の範囲で移動平均を保持 | |
_loopIndex = (_loopIndex + 1) % 5; | |
[self.loopCnt setValue:[NSNumber numberWithInt:timeInt] | |
forKey:[NSString stringWithFormat:@"%d", _loopIndex]]; | |
// 昇順にソート | |
NSMutableArray* tempArray = [NSMutableArray arrayWithArray:[self.loopCnt allValues]]; | |
tempArray = (NSMutableArray*)[tempArray sortedArrayUsingSelector:@selector(compare:)]; | |
NSLog(@"tempArray:%@", tempArray); | |
NSInteger minValue = [[tempArray objectAtIndex:0] intValue]; | |
NSInteger maxValue = [[tempArray objectAtIndex:[tempArray count]-1] intValue]; | |
// 最初のタップは計算しない | |
if ([tempArray count] < 2) return; | |
// 差の絶対値から平均を求める | |
NSInteger avg = 0; | |
NSInteger sub = abs(maxValue - minValue); | |
avg = ceil(sub / ([tempArray count] -1)); | |
NSLog(@"avg:%d", avg); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment