Subversion Repository Public Repository

Nextrek

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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//
// CustomPageControl.m
// CustomPageControl


#import "CustomPageControl.h"


#define kDotDiameter 4.0f
#define kDotSpace 12.0f


@implementation CustomPageControl

@synthesize numberOfPages ;
@synthesize currentPage ;
@synthesize hidesForSinglePage ;
@synthesize defersCurrentPageDisplay ;

@synthesize type ;
@synthesize onColor ;
@synthesize offColor ;
@synthesize onImage ;
@synthesize offImage ;

@synthesize indicatorDiameter ;
@synthesize indicatorSpace ;

#pragma mark -
#pragma mark Initializers - dealloc

- (id)initWithType:(CustomPageControlType)theType
{
    self = [self initWithFrame: CGRectZero] ;
    [self setType: theType] ;
    return self ;
}

- (id)init
{
    self = [self initWithFrame: CGRectZero] ;
    return self ;
}

- (id)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame: CGRectZero]))
    {
        self.backgroundColor = [UIColor clearColor] ;
    }
    return self ;
}

- (void)dealloc {
    [onColor release], onColor = nil ;
    [offColor release], offColor = nil ;
    
    [super dealloc] ;
}


#pragma mark -
#pragma mark drawRect

- (void)drawRect:(CGRect)rect
{
    // get the current context
    CGContextRef context = UIGraphicsGetCurrentContext() ;
    
    // save the context
    CGContextSaveGState(context) ;
    
    // allow antialiasing
    CGContextSetAllowsAntialiasing(context, TRUE) ;
    
    // get the caller's diameter if it has been set or use the default one
    CGFloat diameter = (indicatorDiameter > 0) ? indicatorDiameter : kDotDiameter ;
    CGFloat space = (indicatorSpace > 0) ? indicatorSpace : kDotSpace ;
    
    // geometry
    CGRect currentBounds = self.bounds ;
    CGFloat dotsWidth = self.numberOfPages * diameter + MAX(0, self.numberOfPages - 1) * space ;
    CGFloat x = CGRectGetMidX(currentBounds) - dotsWidth / 2 ;
    CGFloat y = CGRectGetMidY(currentBounds) - diameter / 2 ;
    
    // get the caller's colors it they have been set or use the defaults
    CGColorRef onColorCG = onColor ? onColor.CGColor : [UIColor colorWithWhite: 1.0f alpha: 1.0f].CGColor ;
    CGColorRef offColorCG = offColor ? offColor.CGColor : [UIColor colorWithWhite: 0.7f alpha: 0.5f].CGColor ;
    
    // actually draw the dots
    for (int i = 0 ; i < numberOfPages ; i++)
    {
        CGRect dotRect = CGRectMake(x, y, diameter, diameter) ;
        
        if (i == currentPage)
        {
            if (type == CustomPageControlTypeOnImageOffImage) {
                [onImage drawInRect:dotRect];
            }
            else if (type == CustomPageControlTypeOnFullOffFull || type == CustomPageControlTypeOnFullOffEmpty)
            {
                CGContextSetFillColorWithColor(context, onColorCG) ;
                CGContextFillEllipseInRect(context, CGRectInset(dotRect, -1.0f, -1.0f)) ;
            }
            else
            {
                CGContextSetStrokeColorWithColor(context, onColorCG) ;
                CGContextStrokeEllipseInRect(context, dotRect) ;
            }
        }
        else
        {
            if (type == CustomPageControlTypeOnImageOffImage) {
                [offImage drawInRect:dotRect];
            }
            else if (type == CustomPageControlTypeOnEmptyOffEmpty || type == CustomPageControlTypeOnFullOffEmpty)
            {
                CGContextSetStrokeColorWithColor(context, offColorCG) ;
                CGContextStrokeEllipseInRect(context, dotRect) ;
            }
            else
            {
                CGContextSetFillColorWithColor(context, offColorCG) ;
                CGContextFillEllipseInRect(context, CGRectInset(dotRect, -1.0f, -1.0f)) ;
            }
        }
        
        x += diameter + space ;
    }
    
    // restore the context
    CGContextRestoreGState(context) ;
}


#pragma mark -
#pragma mark Accessors

- (void)setCurrentPage:(NSInteger)pageNumber
{
    // no need to update in that case
    if (currentPage == pageNumber)
        return ;
    
    // determine if the page number is in the available range
    currentPage = MIN(MAX(0, pageNumber), numberOfPages - 1) ;
    
    // in case we do not defer the page update, we redraw the view
    if (self.defersCurrentPageDisplay == NO)
        [self setNeedsDisplay] ;
}

- (void)setNumberOfPages:(NSInteger)numOfPages
{
    // make sure the number of pages is positive
    numberOfPages = MAX(0, numOfPages) ;
    
    // we then need to update the current page
    currentPage = MIN(MAX(0, currentPage), numberOfPages - 1) ;
    
    // correct the bounds accordingly
    self.bounds = self.bounds ;
    
    // we need to redraw
    [self setNeedsDisplay] ;
    
    // depending on the user preferences, we hide the page control with a single element
    if (hidesForSinglePage && (numOfPages < 2))
        [self setHidden: YES] ;
    else
        [self setHidden: NO] ;
}

- (void)setHidesForSinglePage:(BOOL)hide
{
    hidesForSinglePage = hide ;
    
    // depending on the user preferences, we hide the page control with a single element
    if (hidesForSinglePage && (numberOfPages < 2))
        [self setHidden: YES] ;
}

- (void)setDefersCurrentPageDisplay:(BOOL)defers
{
    defersCurrentPageDisplay = defers ;
}

- (void)setType:(CustomPageControlType)aType
{
    type = aType ;
    
    [self setNeedsDisplay] ;
}

- (void)setOnColor:(UIColor *)aColor
{
    [aColor retain] ;
    [onColor release] ;
    onColor = aColor ;
    
    [self setNeedsDisplay] ;
}

- (void)setOffColor:(UIColor *)aColor
{
    [aColor retain] ;
    [offColor release] ;
    offColor = aColor ;
    
    [self setNeedsDisplay] ;
}

- (void)setIndicatorDiameter:(CGFloat)aDiameter
{
    indicatorDiameter = aDiameter ;
    
    // correct the bounds accordingly
    self.bounds = self.bounds ;
    
    [self setNeedsDisplay] ;
}

- (void)setIndicatorSpace:(CGFloat)aSpace
{
    indicatorSpace = aSpace ;
    
    // correct the bounds accordingly
    self.bounds = self.bounds ;
    
    [self setNeedsDisplay] ;
}

- (void)setFrame:(CGRect)aFrame
{
    // we do not allow the caller to modify the size struct in the frame so we compute it
    aFrame.size = [self sizeForNumberOfPages: numberOfPages] ;
    super.frame = aFrame ;
}

- (void)setBounds:(CGRect)aBounds
{
    // we do not allow the caller to modify the size struct in the bounds so we compute it
    aBounds.size = [self sizeForNumberOfPages: numberOfPages] ;
    super.bounds = aBounds ;
}



#pragma mark -
#pragma mark UIPageControl methods

- (void)updateCurrentPageDisplay
{
    // ignores this method if the value of defersPageIndicatorUpdate is NO
    if (self.defersCurrentPageDisplay == NO)
        return ;
    
    // in case it is YES, we redraw the view (that will update the page control to the correct page)
    [self setNeedsDisplay] ;
}

- (CGSize)sizeForNumberOfPages:(NSInteger)pageCount
{
    CGFloat diameter = (indicatorDiameter > 0) ? indicatorDiameter : kDotDiameter ;
    CGFloat space = (indicatorSpace > 0) ? indicatorSpace : kDotSpace ;
    
    return CGSizeMake(pageCount * diameter + (pageCount - 1) * space + 44.0f, MAX(44.0f, diameter + 4.0f)) ;
}


#pragma mark -
#pragma mark Touches handlers

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    // get the touch location
    UITouch *theTouch = [touches anyObject] ;
    CGPoint touchLocation = [theTouch locationInView: self] ;
    
    // check whether the touch is in the right or left hand-side of the control
    if (touchLocation.x < (self.bounds.size.width / 2))
        self.currentPage = MAX(self.currentPage - 1, 0) ;
    else
        self.currentPage = MIN(self.currentPage + 1, numberOfPages - 1) ;
    
    // send the value changed action to the target
    [self sendActionsForControlEvents: UIControlEventValueChanged] ;
}

@end

Commits for Nextrek/iOS/Emmanuele Rossi/Trivagolando/Trivagolando/CustomPageControl.m

Diff revisions: vs.
Revision Author Commited Message
67 FAquili picture FAquili Thu 23 Jan, 2014 14:31:55 +0000

Emmanuele Rossi apps
- mostri
- trikster
- trivagolando