Advertisement
Guest User

BASequenceControl.m

a guest
Jan 24th, 2013
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  Copyright 2011 Dmitry Stadnik. All rights reserved.
  3.  
  4.  Redistribution and use in source and binary forms, with or without modification, are
  5.  permitted provided that the following conditions are met:
  6.  
  7.  1. Redistributions of source code must retain the above copyright notice, this list of
  8.  conditions and the following disclaimer.
  9.  
  10.  2. Redistributions in binary form must reproduce the above copyright notice, this list
  11.  of conditions and the following disclaimer in the documentation and/or other materials
  12.  provided with the distribution.
  13.  
  14.  THIS SOFTWARE IS PROVIDED BY DMITRY STADNIK ``AS IS'' AND ANY EXPRESS OR IMPLIED
  15.  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  16.  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DMITRY STADNIK OR
  17.  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18.  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19.  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  20.  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21.  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  22.  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23.  
  24.  The views and conclusions contained in the software and documentation are those of the
  25.  authors and should not be interpreted as representing official policies, either expressed
  26.  or implied, of Dmitry Stadnik.
  27.  */
  28.  
  29. #import "BASequenceControl.h"
  30.  
  31. @interface BASequenceControl()
  32.  
  33. @property(nonatomic, readonly) NSMutableArray *segments;
  34.  
  35. @end
  36.  
  37.  
  38. @implementation BASequenceControl {
  39. @private
  40.     NSMutableArray *_segments;
  41.     NSInteger _selectedSegmentIndex;
  42. }
  43.  
  44. @synthesize activeSegmentImage = _activeSegmentImage, passiveSegmentImage = _passiveSegmentImage;
  45. @synthesize leftMargin = _leftMargin, rightMargin = _rightMargin, overlapWidth = _overlapWidth;
  46. @synthesize titleFont = _titleFont, activeTitleColor = _activeTitleColor, passiveTitleColor = _passiveTitleColor;
  47.  
  48.  
  49. - (void)awakeFromNib {
  50.     _selectedSegmentIndex = -1;
  51.     [super awakeFromNib];
  52. }
  53.  
  54. - (id)init {
  55.     if ((self = [super init])) {
  56.         _selectedSegmentIndex = -1;
  57.     }
  58.     return self;
  59. }
  60.  
  61. - (NSMutableArray *)segments {
  62.     if (!_segments) {
  63.         _segments = [[NSMutableArray alloc] init];
  64.     }
  65.     return _segments;
  66. }
  67.  
  68. - (NSUInteger)numberOfSegments {
  69.     return [self.segments count];
  70. }
  71.  
  72. - (NSInteger)selectedSegmentIndex {
  73.     return _selectedSegmentIndex;
  74. }
  75.  
  76. - (void)setSelectedSegmentIndex:(NSInteger)selectedSegmentIndex {
  77.     if (selectedSegmentIndex >= [self.segments count]) {
  78.         selectedSegmentIndex = -1;
  79.     } else if (selectedSegmentIndex < -1) {
  80.         selectedSegmentIndex = -1;
  81.     }
  82.     if (_selectedSegmentIndex == selectedSegmentIndex) {
  83.         return;
  84.     }
  85.     _selectedSegmentIndex = selectedSegmentIndex;
  86.     [self setNeedsDisplay];
  87.     [self sendActionsForControlEvents:UIControlEventValueChanged];
  88. }
  89.  
  90. - (void)segmentsDidChange {
  91.     [self setNeedsDisplay];
  92. }
  93.  
  94. - (void)addSegmentWithTitle:(NSString *)title animated:(BOOL)animated {
  95.     if (!title) {
  96.         title = @"";
  97.     }
  98.     [self.segments addObject:title];
  99.     [self segmentsDidChange];
  100. }
  101.  
  102. - (void)insertSegmentWithTitle:(NSString *)title atIndex:(NSUInteger)segment animated:(BOOL)animated {
  103.     if (!title) {
  104.         title = @"";
  105.     }
  106.     [self.segments insertObject:title atIndex:segment];
  107.     [self segmentsDidChange];
  108. }
  109.  
  110. - (void)removeSegmentAtIndex:(NSUInteger)segment animated:(BOOL)animated {
  111.     [self.segments removeObjectAtIndex:segment];
  112.     [self segmentsDidChange];
  113. }
  114.  
  115. - (void)removeAllSegments {
  116.     [self.segments removeAllObjects];
  117.     [self segmentsDidChange];
  118. }
  119.  
  120. - (void)setTitle:(NSString *)title forSegmentAtIndex:(NSUInteger)segment {
  121.     if (!title) {
  122.         title = @"";
  123.     }
  124.     [self.segments replaceObjectAtIndex:segment withObject:title];
  125.     [self segmentsDidChange];
  126. }
  127.  
  128. - (NSString *)titleForSegmentAtIndex:(NSUInteger)segment {
  129.     return [self.segments objectAtIndex:segment];
  130. }
  131.  
  132. - (void)drawRect:(CGRect)rect {
  133.     UIImage *activeSegmentImage = self.activeSegmentImage;
  134.     if (!activeSegmentImage) {
  135.         activeSegmentImage = [[UIImage imageNamed:@"ba-sequence-item-active.png"] stretchableImageWithLeftCapWidth:20
  136.                                                                                                       topCapHeight:22];
  137.     }
  138.     UIImage *passiveSegmentImage = self.passiveSegmentImage;
  139.     if (!passiveSegmentImage) {
  140.         passiveSegmentImage = [[UIImage imageNamed:@"ba-sequence-item-passive.png"] stretchableImageWithLeftCapWidth:20
  141.                                                                                                         topCapHeight:22];
  142.     }
  143.     UIFont *titleFont = self.titleFont;
  144.     if (!titleFont) {
  145.         titleFont = [UIFont boldSystemFontOfSize:18];
  146.     }
  147.     UIColor *activeTitleColor = self.activeTitleColor;
  148.     if (!activeTitleColor) {
  149.         activeTitleColor = [UIColor whiteColor];
  150.     }
  151.     UIColor *passiveTitleColor = self.passiveTitleColor;
  152.     if (!passiveTitleColor) {
  153.         passiveTitleColor = [UIColor grayColor];
  154.     }
  155.     const CGFloat w = self.bounds.size.width;
  156.     const CGFloat h = self.bounds.size.height;
  157.     const CGFloat p = self.overlapWidth;
  158.    
  159.     [passiveSegmentImage drawInRect:CGRectMake(-passiveSegmentImage.size.width, 0,
  160.                                                w + 2 * passiveSegmentImage.size.width, h)];
  161.     if (self.numberOfSegments > 0) {
  162.         const CGFloat sw = rint((w - self.leftMargin - self.rightMargin - p) / self.numberOfSegments);
  163.         if (sw > 0) {
  164.             CGFloat right = w - self.rightMargin;
  165.             for (NSInteger segment = (self.numberOfSegments - 1); segment >= 0; segment--) {
  166.                 UIImage *image = nil;
  167.                 if (self.selectedSegmentIndex == segment) {
  168.                     image = activeSegmentImage;
  169.                     [activeTitleColor set];
  170.                 } else {
  171.                     image = passiveSegmentImage;
  172.                     [passiveTitleColor set];
  173.                 }
  174.                 [image drawInRect:CGRectMake(right - sw - p, 0, p + sw, h)];
  175.                 NSString *title = [self titleForSegmentAtIndex:segment];
  176.                 const CGSize titleSize = [title sizeWithFont:titleFont];
  177.                 const CGFloat titleWidth = MIN(sw - p, titleSize.width);
  178.                 CGRect titleFrame = CGRectMake(rint(right - sw + (sw - p - titleWidth) / 2),
  179.                                                rint((h - titleSize.height) / 2),
  180.                                                titleWidth, titleSize.height);
  181.                 //UIRectFrame(titleFrame);
  182.                 [title drawInRect:titleFrame withFont:titleFont lineBreakMode:UILineBreakModeTailTruncation];
  183.                 right -= sw;
  184.             }
  185.             [passiveSegmentImage drawInRect:CGRectMake(-p, 0, p + self.leftMargin + p, h)];
  186.         }
  187.     }
  188. }
  189.  
  190. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  191.     [super touchesEnded:touches withEvent:event];
  192.     if (self.numberOfSegments > 0) {
  193.         const CGFloat w = self.bounds.size.width;
  194.         const CGFloat p = self.overlapWidth;
  195.         const CGFloat sw = rint((w - self.leftMargin - self.rightMargin - p) / self.numberOfSegments);
  196.         if (sw > 0) {
  197.             UITouch *touch = [touches anyObject];
  198.             const CGFloat x = [touch locationInView:self].x;
  199.             if (x >= self.leftMargin && x <= (w - self.rightMargin)) {
  200.                 NSInteger segment = (x - self.leftMargin - p / 2) / sw;
  201.                 if (segment >= self.numberOfSegments) {
  202.                     segment = self.numberOfSegments - 1;
  203.                 } else if (segment < 0) {
  204.                     segment = 0;
  205.                 }
  206.                 self.selectedSegmentIndex = segment;
  207.             }
  208.         }
  209.     }
  210. }
  211.  
  212. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement