// // RuledScrollView.m // SpectogramPrototype // // Created by Joshua Moerman on 24/12/13. // Copyright (c) 2013 Joshua Moerman. All rights reserved. // #import "RuledScrollView.h" @interface RuledScrollView () { CGFloat rulerHeight; CGFloat rulerWidth; } - (void) initView; - (void) placeTicks; @end @implementation RuledScrollView @synthesize content, topRuler, leftRuler; - (id)initWithFrame:(CGRect)frame{ if(self = [super initWithFrame:frame]){ [self initView]; } return self; } - (id)initWithCoder:(NSCoder *)aDecoder{ if(self = [super initWithCoder:aDecoder]){ [self initView]; } return self; } - (void)initView { rulerHeight = 44; rulerWidth = 120; self.delegate = self; self.backgroundColor = [UIColor whiteColor]; content = [[UIView alloc] initWithFrame:self.frame]; content.autoresizesSubviews = NO; [self addSubview:content]; CGRect frame2 = self.frame; frame2.size.height = rulerHeight; topRuler = [[UIView alloc] initWithFrame:frame2]; topRuler.layer.opacity = 0.5; topRuler.backgroundColor = self.backgroundColor; [self addSubview:topRuler]; CGRect frame3 = self.frame; frame3.size.width = rulerWidth; leftRuler = [[UIView alloc] initWithFrame:frame3]; leftRuler.layer.opacity = 0.5; leftRuler.backgroundColor = self.backgroundColor; [self addSubview:leftRuler]; } /* we should draw the ticks in drawRect: [[UIColor blackColor] setFill]; UIRectFill((CGRect){0,200,rect.size.width,1}); and do tiling for the text */ - (void)placeTicks { [topRuler.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; for (CGFloat x = rulerWidth + 50; x < topRuler.frame.size.width; x += 100){ UIView * line = [[UIView alloc] initWithFrame:CGRectMake(x, 0, 1, rulerHeight)]; line.backgroundColor = [UIColor blackColor]; [topRuler addSubview:line]; CGFloat width = 100; UILabel * text = [[UILabel alloc] initWithFrame:CGRectMake(x - width/2, 0, width, rulerHeight)]; text.text = [NSString stringWithFormat:@"%.0f", x]; text.textAlignment = NSTextAlignmentCenter; [topRuler addSubview:text]; } } - (void)layoutSubviews { { CGRect frame = self.topRuler.frame; frame.origin.y = self.contentOffset.y; self.topRuler.frame = frame; [self placeTicks]; } { CGRect frame = self.leftRuler.frame; frame.origin.x = self.contentOffset.x; self.leftRuler.frame = frame; } } - (void)setContentSize:(CGSize)contentSize{ [super setContentSize:CGSizeMake(contentSize.width + rulerWidth, contentSize.height + rulerHeight)]; content.frame = CGRectMake(rulerWidth, rulerHeight, contentSize.width, contentSize.height); topRuler.frame = CGRectMake(0, 0, contentSize.width + rulerWidth, rulerHeight); leftRuler.frame = CGRectMake(0, 0, rulerWidth, contentSize.height + rulerHeight); } - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{ return content; } @end