Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 12th, 2012  |  syntax: None  |  size: 6.85 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How do I change the value by a variable
  2. #import "FirstViewController.h"
  3. #import <QuartzCore/QuartzCore.h>
  4.  
  5.  
  6. @implementation FirstViewController
  7. @synthesize needleImageView;
  8. @synthesize speedometerCurrentValue;
  9. @synthesize prevAngleFactor;
  10. @synthesize angle;
  11. @synthesize speedometer_Timer;
  12. @synthesize speedometerReading;
  13. @synthesize maxVal;
  14.  
  15. /*
  16. // The designated initializer. Override to perform setup that is required before the view is loaded.
  17. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  18.     if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
  19.         // Custom initialization
  20.     }
  21.     return self;
  22. }
  23. */
  24.  
  25. /*
  26. // Implement loadView to create a view hierarchy programmatically, without using a nib.
  27. - (void)loadView {
  28. }
  29. */
  30.  
  31.  
  32. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  33. - (void)viewDidLoad {
  34.  
  35.     // Add Meter Contents //
  36.     [self addMeterViewContents];
  37.     [super viewDidLoad];
  38. }
  39.  
  40.  
  41. /*
  42. // Override to allow orientations other than the default portrait orientation.
  43. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  44.     // Return YES for supported orientations
  45.     return (interfaceOrientation == UIInterfaceOrientationPortrait);
  46. }
  47. */
  48.  
  49. - (void)didReceiveMemoryWarning {
  50.     // Releases the view if it doesn't have a superview.
  51.     [super didReceiveMemoryWarning];
  52.  
  53.     // Release any cached data, images, etc that aren't in use.
  54. }
  55.  
  56. - (void)viewDidUnload {
  57.     // Release any retained subviews of the main view.
  58.     // e.g. self.myOutlet = nil;
  59. }
  60.  
  61.  
  62. - (void)dealloc {
  63.     [maxVal release];
  64.     [needleImageView release];
  65.     [speedometer_Timer release];
  66.     [speedometerReading release];
  67.  
  68.     [super dealloc];
  69. }
  70.  
  71. #pragma mark -
  72. #pragma mark Public Methods
  73.  
  74. -(void) addMeterViewContents
  75. {
  76.  
  77.     UIImageView *backgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320,460)];
  78.     backgroundImageView.image = [UIImage imageNamed:@"main_bg.png"];
  79.     [self.view addSubview:backgroundImageView];
  80.     [backgroundImageView release];
  81.  
  82.  
  83.     /*UIImageView *meterImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 40, 286,315)];
  84.     meterImageView.image = [UIImage imageNamed:@"meter.png"];
  85.     [self.view addSubview:meterImageView];
  86.     [meterImageView release];*/
  87.  
  88.  
  89.     //  Needle //
  90.     UIImageView *imgNeedle = [[UIImageView alloc]initWithFrame:CGRectMake(143,155, 22, 84)];
  91.     self.needleImageView = imgNeedle;
  92.     [imgNeedle release];
  93.  
  94.     self.needleImageView.layer.anchorPoint = CGPointMake(self.needleImageView.layer.anchorPoint.x, self.needleImageView.layer.anchorPoint.y*2);
  95.     self.needleImageView.backgroundColor = [UIColor clearColor];
  96.     self.needleImageView.image = [UIImage imageNamed:@"arrow.png"];
  97.     [self.view addSubview:self.needleImageView];
  98.  
  99.     // Needle Dot //
  100.     UIImageView *meterImageViewDot = [[UIImageView alloc]initWithFrame:CGRectMake(131.5, 175, 45,44)];
  101.     meterImageViewDot.image = [UIImage imageNamed:@"center_wheel.png"];
  102.     [self.view addSubview:meterImageViewDot];
  103.     [meterImageViewDot release];
  104.  
  105.     // Speedometer Reading //
  106.     UILabel *tempReading = [[UILabel alloc] initWithFrame:CGRectMake(125, 250, 60, 30)];
  107.     self.speedometerReading = tempReading;
  108.     [tempReading release];
  109.     self.speedometerReading.textAlignment = UITextAlignmentCenter;
  110.     self.speedometerReading.backgroundColor = [UIColor blackColor];
  111.     self.speedometerReading.text= @"0";
  112.     self.speedometerReading.textColor = [UIColor colorWithRed:114/255.f green:146/255.f blue:38/255.f alpha:1.0];
  113.     [self.view addSubview:self.speedometerReading ];
  114.  
  115.     // Set Max Value //
  116.     self.maxVal = @"100";
  117.  
  118.     /// Set Needle pointer initialy at zero //
  119.     [self rotateIt:-118.4];
  120.  
  121.     // Set previous angle //
  122.     self.prevAngleFactor = -118.4;
  123.  
  124.     // Set Speedometer Value //
  125.     [self setSpeedometerCurrentValue];
  126. }
  127.  
  128. #pragma mark -
  129. #pragma mark calculateDeviationAngle Method
  130.  
  131. -(void) calculateDeviationAngle
  132. {
  133.  
  134.     if([self.maxVal floatValue]>0)
  135.     {
  136.         self.angle = ((self.speedometerCurrentValue *237.4)/[self.maxVal floatValue])-118.4;  // 237.4 - Total angle between 0 - 100 //
  137.     }
  138.     else
  139.     {
  140.         self.angle = 0;
  141.     }
  142.  
  143.     if(self.angle<=-118.4)
  144.     {
  145.         self.angle = -118.4;  
  146.     }
  147.     if(self.angle>=119)
  148.     {
  149.         self.angle = 119;
  150.     }
  151.  
  152.  
  153.     // If Calculated angle is greater than 180 deg, to avoid the needle to rotate in reverse direction first rotate the needle 1/3 of the calculated angle and then 2/3. //
  154.     if(abs(self.angle-self.prevAngleFactor) >180)
  155.     {
  156.         [UIView beginAnimations:nil context:nil];
  157.         [UIView setAnimationDuration:0.5f];
  158.         [self rotateIt:self.angle/3];
  159.         [UIView commitAnimations];
  160.  
  161.         [UIView beginAnimations:nil context:nil];
  162.         [UIView setAnimationDuration:0.5f];
  163.         [self rotateIt:(self.angle*2)/3];
  164.         [UIView commitAnimations];
  165.  
  166.     }
  167.  
  168.     self.prevAngleFactor = self.angle;
  169.  
  170.  
  171.     // Rotate Needle //
  172.     [self rotateNeedle];
  173.  
  174.  
  175. }
  176.  
  177.  
  178. #pragma mark -
  179. #pragma mark rotateNeedle Method
  180. -(void) rotateNeedle
  181. {
  182.     [UIView beginAnimations:nil context:nil];
  183.     [UIView setAnimationDuration:0.5f];
  184.     [self.needleImageView setTransform: CGAffineTransformMakeRotation((M_PI / 180) * self.angle)];  
  185.     [UIView commitAnimations];
  186.  
  187. }
  188.  
  189. #pragma mark -
  190. #pragma mark setSpeedometerCurrentValue
  191.  
  192. -(void) setSpeedometerCurrentValue
  193. {
  194.     if(self.speedometer_Timer)
  195.     {
  196.         [self.speedometer_Timer invalidate];
  197.         self.speedometer_Timer = nil;
  198.     }
  199.     float hilfe = arc4random() % 100;
  200.     self.speedometerCurrentValue =  hilfe; // Generate Random value between 0 to 100. //
  201.     progress.progress = hilfe/100;
  202.     self.speedometer_Timer = [NSTimer  scheduledTimerWithTimeInterval:2 target:self selector:@selector(setSpeedometerCurrentValue) userInfo:nil repeats:YES];
  203.  
  204.     self.speedometerReading.text = [NSString stringWithFormat:@"%.2f",self.speedometerCurrentValue];
  205.  
  206.     // Calculate the Angle by which the needle should rotate //
  207.     [self calculateDeviationAngle];
  208. }
  209. #pragma mark -
  210. #pragma mark Speedometer needle Rotation View Methods
  211.  
  212. -(void) rotateIt:(float)angl
  213. {
  214.     [UIView beginAnimations:nil context:nil];
  215.     [UIView setAnimationDuration:0.01f];
  216.  
  217.     [self.needleImageView setTransform: CGAffineTransformMakeRotation((M_PI / 180) *angl)];
  218.  
  219.     [UIView commitAnimations];
  220. }
  221.  
  222. #pragma mark -
  223. #pragma mark Slider changed
  224.  
  225. -(IBAction) silderchanged:(id)sender
  226. {
  227.     int progressAsInt =(int)(slider.value + 0.5f);
  228.     NSString *newText =[[NSString alloc] initWithFormat:@"%d",progressAsInt];
  229.     sliderbox.text = newText;
  230.     //progress.progress = ((slider.value + 0.5f)/100);
  231. }
  232.  
  233.  
  234. @end
  235.        
  236. -(IBAction) setValue:(id)sender {
  237.     float  f = [sliderValue value];
  238.     NSLog(@"get this %f", f);
  239.  
  240.     // should be refactored
  241.     self.speedometerCurrentValue = f;
  242.     self.speedometerReading.text = [NSString stringWithFormat:@"%.2f",self.speedometerCurrentValue];
  243.  
  244.     [self calculateDeviationAngle];
  245. }