/
TDMenu.m
204 lines (150 loc) · 6.29 KB
/
TDMenu.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//
// TDMenu.m
// TDMenuSelector
//
// Created by Purush V on 23/01/13.
// Copyright (c) 2013 __MyCompanyName__. All rights reserved.
//
#import "TDMenu.h"
#import <QuartzCore/QuartzCore.h>
@implementation TDMenu
@synthesize numeberOfitems,delegate;
/*- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}*/
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// setup the initial properties of the
// draggable item
// [self setItemPropertiesToDefault:self];
}
return self;
}
-(void)setNumeberOfitems:(NSUInteger)numeberOfitem
{
numeberOfitems = numeberOfitem;
}
-(void)drawMenuItems
{ //5px is space between the items
int spaceWidth = 0;
//Subtracting space between items
float space = (numeberOfitems -1)*spaceWidth;
//Finding menu items width
float itemWidth = (self.frame.size.width - space)/numeberOfitems;
float itemHeight = self.frame.size.height;
float itemX = 0;
float itemY = 0;
NSMutableArray * colorAry = [[NSMutableArray alloc]initWithObjects:[UIColor blueColor],[UIColor yellowColor],[UIColor orangeColor],[UIColor brownColor],[UIColor purpleColor], nil];
//creating items
itemsAry = [[NSMutableArray alloc]init];
itemsXPositionAry = [[NSMutableArray alloc]init];
for (int i =0; i<numeberOfitems; i++) {
UIView *itemView = [[UIView alloc]initWithFrame:CGRectMake(itemX, itemY, itemWidth, itemHeight)];
itemView.backgroundColor = [colorAry objectAtIndex:i];
itemView.tag = i;
itemView.userInteractionEnabled = YES;
[self addSubview:itemView];
itemX = itemView.frame.origin.x + itemWidth + spaceWidth;
[itemsAry addObject:itemView];
[itemsXPositionAry addObject:[NSString stringWithFormat:@"%f",itemView.frame.origin.x]];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[singleTap setNumberOfTapsRequired:1];
[singleTap setNumberOfTouchesRequired:1];
[itemView addGestureRecognizer:singleTap];
}
if ([itemsAry count]>0) {
[self creatingSelectorView:0 y:itemY width:itemWidth height:itemHeight];
}
}
-(void)creatingSelectorView:(float)x y:(float)y width:(float)width height:(float)height
{
//Creating selector view and placing it in first item
UIView *selectorView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
selectorView.backgroundColor = [UIColor greenColor];
UIView *lastView = [itemsAry lastObject];
selectorView.tag = [lastView tag]+1;
lastView.userInteractionEnabled = YES;
[itemsAry addObject:selectorView];
[self addSubview:selectorView];
//Set moving property
[self setPanGestureRecognizer:selectorView];
}
-(void)setPanGestureRecognizer:(UIView *)view
{
UIPanGestureRecognizer* panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
panRecognizer.minimumNumberOfTouches = 1;
panRecognizer.delegate = self; // Very important
[view addGestureRecognizer:panRecognizer];
}
- (void)handleTap:(UIPanGestureRecognizer *)recognizer {
float tappedViewX = recognizer.view.frame.origin.x;
UIView *view = [itemsAry lastObject];
[UIView beginAnimations:@"MoveView" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.5f];
view.frame = CGRectMake(tappedViewX, view.frame.origin.y, view.frame.size.width, view.frame.size.height);
[UIView commitAnimations];
float selectedViewX =view.frame.origin.x;
NSUInteger index = [itemsXPositionAry indexOfObject:[NSString stringWithFormat:@"%f",selectedViewX]];
[delegate SelectedMenu:index];
}
- (void)handlePan:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:self];
CGFloat newX = MIN(recognizer.view.frame.origin.x + translation.x, self.frame.size.width - recognizer.view.frame.size.width);
CGRect newFrame = CGRectMake( newX,recognizer.view.frame.origin.y, recognizer.view.frame.size.width, recognizer.view.frame.size.height);
recognizer.view.frame = newFrame;
[recognizer setTranslation:CGPointZero inView:self];
if (recognizer.state == UIGestureRecognizerStateEnded) {
[self calculateAppropriateSelectorXposition:recognizer.view];
}
}
-(void)calculateAppropriateSelectorXposition:(UIView *)view
{
float selectorViewX = view.frame.origin.x;
float itemXposition;
for (int i =0; i<[itemsXPositionAry count]; i++) {
itemXposition = [[itemsXPositionAry objectAtIndex:i]floatValue];
if (selectorViewX < itemXposition)
break;
}
if (selectorViewX > 0) {
if ((itemXposition - selectorViewX)< (view.frame.size.width)/2) {
view.frame = CGRectMake(view.frame.origin.x + itemXposition - selectorViewX, view.frame.origin.y, view.frame.size.width, view.frame.size.height);
}
else{
view.frame = CGRectMake(itemXposition - view.frame.size.width, view.frame.origin.y, view.frame.size.width, view.frame.size.height);
}
}
else{
view.frame = CGRectMake(0, view.frame.origin.y, view.frame.size.width, view.frame.size.height);
}
float selectedViewX =view.frame.origin.x;
NSUInteger index = [itemsXPositionAry indexOfObject:[NSString stringWithFormat:@"%f",selectedViewX]];
[delegate SelectedMenu:index];
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end