CatNiP prefinal
Sähköinen nuottikirja, HY-TKTKL-OHTUPROJ KESÄ11
|
00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 #import "UIScoreView.h" 00013 #import "UIDocumentPDF.h" 00014 00015 @implementation UIScoreView 00016 00017 @synthesize scrollBar; 00018 @synthesize documentView; 00019 00021 static const CGFloat SCROLLBAR_WIDTH = 50; 00022 00031 - (id)init 00032 { 00033 // Some sorcery to retrieve the default settings for the object from 00034 // UIScoreView.xib 00035 UIView *scoreView = nil; 00036 NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"UIScoreView" owner:self options:nil]; 00037 for(id object in nib) 00038 if([object isKindOfClass:[UIView class]]) 00039 scoreView = (UIView *)[object retain]; 00040 00041 if(scoreView) 00042 { 00043 // The sorcery continues 00044 scoreView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height); 00045 [self addSubview:scoreView]; 00046 00047 // Connect documentView to scrollBar, turn directional locking on 00048 // because apparently Interface Builder doesn't know how 00049 documentView.delegate = scrollBar; 00050 documentView.directionalLockEnabled = YES; 00051 00052 // Rotate scroll bar because Interface Builder doesn't know how, 00053 // link it to documentView 00054 scrollBar.direction = M_PI_2; 00055 [scrollBar setFrame:CGRectMake(self.frame.size.width - SCROLLBAR_WIDTH, 0, SCROLLBAR_WIDTH, self.frame.size.height)]; 00056 scrollBar.delegate = documentView; 00057 scrollBar.autoresizingMask = UIViewAutoresizingFlexibleHeight |UIViewAutoresizingFlexibleLeftMargin; 00058 00059 // Add tap gesture recognition 00060 UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)]; 00061 [gestureRecognizer setDelegate:self]; 00062 [self addGestureRecognizer:gestureRecognizer]; 00063 } 00064 00065 return self; 00066 } 00067 00076 - (id)initWithCoder:(NSCoder *)aDecoder 00077 { 00078 if((self = [super initWithCoder:aDecoder])) 00079 return [self init]; 00080 else 00081 return nil; 00082 } 00083 00090 - (id)initWithFrame:(CGRect)frame 00091 { 00092 if((self = [super initWithFrame:frame])) 00093 return [self init]; 00094 else 00095 return nil; 00096 } 00097 00103 - (BOOL)setScore:(NSString *)scorePath 00104 { 00105 UIDocument *document; 00106 if((document = [UIDocumentPDF documentWithContentsOfFile:scorePath])) { 00107 // Set minimum zoom level as fit-to-width in portrait mode, maximum 00108 // as twice that. 00109 documentView.minimumZoomScale = documentView.bounds.size.width / document.size.width; 00110 documentView.maximumZoomScale = 2 * documentView.minimumZoomScale; 00111 00112 documentView.document = document; 00113 return YES; 00114 } 00115 return NO; 00116 } 00117 00122 - (void)showScrollBar:(BOOL)show 00123 { 00124 if(show) 00125 { 00126 [UIView animateWithDuration:0.7 00127 delay:0.0 00128 options:UIViewAnimationOptionCurveEaseIn 00129 animations:^{ scrollBar.transform = CGAffineTransformMakeTranslation(0, 0); } 00130 completion:NULL]; 00131 00132 } 00133 else 00134 { 00135 [UIView animateWithDuration:0.7 00136 delay:0.0 00137 options:UIViewAnimationOptionCurveEaseIn 00138 animations:^{ scrollBar.transform = CGAffineTransformMakeTranslation(scrollBar.frame.size.width, 0); } 00139 completion:NULL]; 00140 } 00141 } 00142 00143 #pragma mark - Gesture recognizer methods 00144 00147 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 00148 { 00149 return YES; 00150 } 00151 00153 - (void)tapGesture:(UIGestureRecognizer *)gestureRecognizer 00154 { 00155 CGPoint touch = [gestureRecognizer locationInView:self]; 00156 if(touch.x >= self.frame.size.width - scrollBar.frame.size.width) { 00157 [self showScrollBar:YES]; 00158 } 00159 else { 00160 [self showScrollBar:NO]; 00161 } 00162 } 00163 00164 - (void)dealloc 00165 { 00166 [documentView release]; 00167 [scrollBar release]; 00168 [super dealloc]; 00169 } 00170 00171 @end