Advertisement
rdsquared

Rotary Control Blog 2

Sep 6th, 2011
2,227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  RotaryControlBlogCircleImageView.h
  3. //  RotaryControlBlog
  4. //
  5. //  Created by Ryan Dillon on 9/6/11.
  6. //  Copyright 2011 Gig Bag Productions.
  7.  
  8. /*
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27.  
  28. #import <UIKit/UIKit.h>
  29.  
  30.  
  31. @interface RotaryControlBlogCircleImageView : UIImageView {
  32.    
  33.     double  touchBeginAngle;
  34.    
  35. }
  36.  
  37. @end
  38.  
  39.  
  40. //
  41. //  RotaryControlBlogCircleImageView.m
  42. //  RotaryControlBlog
  43. //
  44. //  Created by Ryan Dillon on 9/6/11.
  45. //  Copyright 2011 Gig Bag Productions.
  46. //
  47.  
  48. #import "RotaryControlBlogCircleImageView.h"
  49. #import <QuartzCore/QuartzCore.h>
  50.  
  51. @implementation RotaryControlBlogCircleImageView
  52.  
  53. -(void)setup {
  54.     self.image = [UIImage imageNamed:@"CircleInSquare"]; // Obviously, you'll have to provide your own image. Or don't even use one and just subclass UIView. The numbers will still show up just fine.
  55.     self.userInteractionEnabled = YES;
  56.     // Calculate the angle between each label based on the number of labels.
  57.     int numberOfSections = 9;
  58.     CGFloat angleSize = 2*M_PI/numberOfSections;
  59.    
  60.     // Each label gets the exact same inital frame, but a different transform is applied to space them around the circle.
  61.     for (int i = 0; i < numberOfSections; ++i) {
  62.         UILabel *sectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.bounds.size.width/2.0f, 50.0f)];
  63.         sectionLabel.text = [NSString stringWithFormat:@"%i", i];
  64.         sectionLabel.font = [UIFont systemFontOfSize:50.0];
  65.         sectionLabel.textColor = [UIColor whiteColor];
  66.         sectionLabel.backgroundColor = [UIColor clearColor];
  67.         //sectionLabel.backgroundColor = [UIColor colorWithRed:1.0f green:0.0f blue:0.2f alpha:0.5f];
  68.        
  69.         /***** Here's the most important part: changing the anchorPoint, adjusting the position accordingly, and applying rotation ****/
  70.         sectionLabel.layer.anchorPoint = CGPointMake(1.0f, 0.5f);
  71.         sectionLabel.layer.position = CGPointMake(self.bounds.size.width/2.0, self.bounds.size.height/2.0); // places anchorPoint of each label directly in the center of the circle.
  72.         sectionLabel.transform = CGAffineTransformMakeRotation(angleSize*i);
  73.         [self addSubview:sectionLabel];
  74.     }
  75.  
  76.     touchBeginAngle = 0.0;
  77. }
  78.  
  79. -(double)angleBetweenCenterAndPoint:(CGPoint)point {
  80.    
  81.     CGPoint center = CGPointMake(self.bounds.size.width/2.0f, self.bounds.size.height/2.0f);
  82.     double theAngle = atan2f(center.y - point.y, point.x - center.x);
  83.     return theAngle;
  84.  
  85. }
  86.  
  87. -(id)initWithFrame:(CGRect)frame {
  88.    
  89.     self = [super initWithFrame:frame];
  90.    
  91.     if (!self) {
  92.         return nil;
  93.     }
  94.     else {
  95.         [self setup];
  96.         return self;
  97.     }
  98. }
  99.  
  100. -(id)initWithCoder:(NSCoder*)aDecoder {
  101.    
  102.     self = [super initWithCoder:aDecoder];
  103.    
  104.     if (!self) {
  105.         return nil;
  106.     }
  107.     else {
  108.         [self setup];
  109.         return self;
  110.     }
  111. }
  112.  
  113. -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  114.    
  115.     CGPoint thisPoint = [[touches anyObject] locationInView:self];
  116.     touchBeginAngle = [self angleBetweenCenterAndPoint:thisPoint];
  117.  
  118. }
  119.  
  120. -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  121.    
  122.     CGPoint thisPoint = [[touches anyObject] locationInView:self];    
  123.     CGFloat currentAngle = [self angleBetweenCenterAndPoint:thisPoint];
  124.     CGFloat angleDiff = (touchBeginAngle - currentAngle);
  125.     self.transform = CGAffineTransformRotate(self.transform, angleDiff);
  126.    
  127. }
  128.  
  129.  
  130.  
  131. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement