Advertisement
rdsquared

Rotary Control Blog 1

Aug 22nd, 2011
1,527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. * Permission is hereby granted, free of charge, to any person obtaining a copy
  3. * of this software and associated documentation files (the "Software"), to deal
  4. * in the Software without restriction, including without limitation the rights
  5. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  6. * copies of the Software, and to permit persons to whom the Software is
  7. * furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in
  10. * all copies or substantial portions of the Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. */
  20.  
  21. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  22. - (void)viewDidLoad
  23. {
  24.     [super viewDidLoad];
  25.    
  26.     // Create a UIImageView with the circle image and center it in the main view.
  27.     UIImageView *container = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0f, 300.0f, 300.0f)];
  28.     container.image = [UIImage imageNamed:@"CircleInSquare"];
  29.     container.center = CGPointMake(self.view.bounds.size.width/2.0, self.view.bounds.size.height/2.0);
  30.     [self.view addSubview:container];
  31.    
  32.     // Calculate the angle between each label based on the number of labels.
  33.     int numberOfSections = 9;
  34.     CGFloat angleSize = 2*M_PI/numberOfSections;
  35.  
  36.     // Each label gets the exact same inital frame, but a different transform is applied to space them around the circle.
  37.     for (int i = 0; i < numberOfSections; ++i) {
  38.         UILabel *sectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 50.0f)];
  39.         sectionLabel.text = [NSString stringWithFormat:@"%i", i];
  40.         sectionLabel.font = [UIFont systemFontOfSize:50.0];
  41.         sectionLabel.textColor = [UIColor whiteColor];
  42.         sectionLabel.backgroundColor = [UIColor clearColor];
  43.         //sectionLabel.backgroundColor = [UIColor colorWithRed:1.0f green:0.0f blue:0.2f alpha:0.5f];
  44.        
  45.         /***** Here's the most important part: changing the anchorPoint, adjusting the position accordingly, and applying rotation ****/
  46.         sectionLabel.layer.anchorPoint = CGPointMake(1.0f, 0.5f);
  47.         sectionLabel.layer.position = CGPointMake(container.bounds.size.width/2.0, container.bounds.size.height/2.0); // places anchorPoint of each label directly in the center of the circle.
  48.         sectionLabel.transform = CGAffineTransformMakeRotation(angleSize*i);
  49.         [container addSubview:sectionLabel];
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement