CatNiP prefinal
Sähköinen nuottikirja, HY-TKTKL-OHTUPROJ KESÄ11
/Users/awniemel/Notepad-SVN/svn/trunk/CatNiP/CatNiP/UIScrollBar.m
Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 #import "UIScrollBar.h"
00018 
00019 @implementation UIScrollBar
00020 
00021 @synthesize delegate;
00022 
00028 - (void)setDirection:(float)value
00029 {
00030     // Remakes the rotation matrix according to the new value and combines
00031     // it with the normal transformation matrix.
00032     rotate = CGAffineTransformMakeRotation(value);
00033     super.transform = CGAffineTransformConcat(rotate, transform);
00034 }
00035 
00040 - (float)direction
00041 {
00042     // Extracts the angle from the rotation matrix.
00043     return atan2(rotate.b,rotate.a);
00044 }
00045 
00051 - (void)setTransform:(CGAffineTransform)value
00052 {
00053     transform = value;
00054     super.transform = CGAffineTransformConcat(rotate, transform);
00055 }
00056 
00062 - (CGAffineTransform)transform
00063 {
00064     return transform;
00065 }
00066 
00075 - (id)init
00076 {
00077     [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(thumbWasRelocated:)]];
00078     [self addTarget:self action:@selector(thumbDidMove:event:) forControlEvents:UIControlEventValueChanged];
00079     return self;
00080 }
00081 
00090 - (id)initWithCoder:(NSCoder *)aDecoder
00091 {
00092     if((self = [super initWithCoder:aDecoder]))
00093         return [self init];
00094     else
00095         return nil;
00096 }
00097 
00104 - (id)initWithFrame:(CGRect)frame
00105 {
00106     if((self = [super initWithFrame:frame]))
00107         return [self init];
00108     else
00109         return nil;
00110 }
00111 
00114 - (void)thumbWasRelocated:(UIGestureRecognizer *)gestureRecognizer
00115 {
00116     CGPoint newOffset = delegate.contentOffset;
00117     
00118     // Transformations do not affect internal coordinates (used here), so we
00119     // can safely calculate the new value from the ratio between the x
00120     // coordinate of the touch and the "width" of the scroll bar.
00121     float newValue = [gestureRecognizer locationInView:self].x / self.bounds.size.width;
00122     
00123     // Calculate the new offset based on whether the scroll bar is horizontal or
00124     // vertical.
00125     if((int)(self.direction * 180 / M_PI) % 180 == 0)
00126         newOffset.x = ABS(cos(self.direction)) * newValue * (delegate.contentSize.width - delegate.bounds.size.width);
00127     else
00128         newOffset.y = ABS(sin(self.direction)) * newValue * (delegate.contentSize.height - delegate.bounds.size.height);
00129     
00130     // This also calls scrollViewDidScroll automatically, so there's no need to
00131     // set the new value by hand.
00132     [delegate setContentOffset:newOffset animated:YES];
00133 }
00134 
00136 - (void)thumbDidMove:(id)sender event:(id)event
00137 {
00138     CGPoint newOffset = delegate.contentOffset;
00139     float newValue = ((UISlider *)sender).value;
00140     
00141     // Calculate the new offset based on whether the scroll bar is horizontal or
00142     // vertical.
00143     if((int)(self.direction * 180 / M_PI) % 180 == 0)
00144         newOffset.x = ABS(cos(self.direction)) * newValue * (delegate.contentSize.width - delegate.bounds.size.width);
00145     else
00146         newOffset.y = ABS(sin(self.direction)) * newValue * (delegate.contentSize.height - delegate.bounds.size.height);
00147     
00148     // Don't animate this, the method gets called all the time.
00149     [delegate setContentOffset:newOffset animated:NO];
00150 }
00151 
00154 - (void)scrollViewDidScroll:(UIScrollView *)scrollView
00155 {
00156     // Calculate the new value based on whether the scroll bar is horizontal or
00157     // vertical.
00158     if((int)(self.direction * 180 / M_PI) % 180 == 0)
00159         [self setValue:ABS(cos(self.direction)) * scrollView.contentOffset.x / (scrollView.contentSize.width - scrollView.bounds.size.width) animated:NO];
00160     else
00161         [self setValue:ABS(sin(self.direction)) * scrollView.contentOffset.y / (scrollView.contentSize.height - scrollView.bounds.size.height) animated:NO];
00162 }
00163 
00164 @end
 All Classes Files Functions Variables Enumerations Enumerator Properties Defines