Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // RotaryControlBlogCircleImageView.h
- // RotaryControlBlog
- //
- // Created by Ryan Dillon on 9/6/11.
- // Copyright 2011 Gig Bag Productions.
- /*
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
- #import <UIKit/UIKit.h>
- @interface RotaryControlBlogCircleImageView : UIImageView {
- double touchBeginAngle;
- }
- @end
- //
- // RotaryControlBlogCircleImageView.m
- // RotaryControlBlog
- //
- // Created by Ryan Dillon on 9/6/11.
- // Copyright 2011 Gig Bag Productions.
- //
- #import "RotaryControlBlogCircleImageView.h"
- #import <QuartzCore/QuartzCore.h>
- @implementation RotaryControlBlogCircleImageView
- -(void)setup {
- 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.
- self.userInteractionEnabled = YES;
- // Calculate the angle between each label based on the number of labels.
- int numberOfSections = 9;
- CGFloat angleSize = 2*M_PI/numberOfSections;
- // Each label gets the exact same inital frame, but a different transform is applied to space them around the circle.
- for (int i = 0; i < numberOfSections; ++i) {
- UILabel *sectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.bounds.size.width/2.0f, 50.0f)];
- sectionLabel.text = [NSString stringWithFormat:@"%i", i];
- sectionLabel.font = [UIFont systemFontOfSize:50.0];
- sectionLabel.textColor = [UIColor whiteColor];
- sectionLabel.backgroundColor = [UIColor clearColor];
- //sectionLabel.backgroundColor = [UIColor colorWithRed:1.0f green:0.0f blue:0.2f alpha:0.5f];
- /***** Here's the most important part: changing the anchorPoint, adjusting the position accordingly, and applying rotation ****/
- sectionLabel.layer.anchorPoint = CGPointMake(1.0f, 0.5f);
- 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.
- sectionLabel.transform = CGAffineTransformMakeRotation(angleSize*i);
- [self addSubview:sectionLabel];
- }
- touchBeginAngle = 0.0;
- }
- -(double)angleBetweenCenterAndPoint:(CGPoint)point {
- CGPoint center = CGPointMake(self.bounds.size.width/2.0f, self.bounds.size.height/2.0f);
- double theAngle = atan2f(center.y - point.y, point.x - center.x);
- return theAngle;
- }
- -(id)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (!self) {
- return nil;
- }
- else {
- [self setup];
- return self;
- }
- }
- -(id)initWithCoder:(NSCoder*)aDecoder {
- self = [super initWithCoder:aDecoder];
- if (!self) {
- return nil;
- }
- else {
- [self setup];
- return self;
- }
- }
- -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
- CGPoint thisPoint = [[touches anyObject] locationInView:self];
- touchBeginAngle = [self angleBetweenCenterAndPoint:thisPoint];
- }
- -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
- CGPoint thisPoint = [[touches anyObject] locationInView:self];
- CGFloat currentAngle = [self angleBetweenCenterAndPoint:thisPoint];
- CGFloat angleDiff = (touchBeginAngle - currentAngle);
- self.transform = CGAffineTransformRotate(self.transform, angleDiff);
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement