- (void)viewDidLoad { [super viewDidLoad];
page = 2;
NSString *pathToPdfDoc = [[NSBundle mainBundle] pathForResource:@"2010_07" ofType:@"pdf"];
NSURL *pdfUrl = [NSURL fileURLWithPath:pathToPdfDoc];
myDocumentRef = CGPDFDocumentCreateWithURL((CFURLRef)pdfUrl);
CGRect pageRect = CGRectIntegral(CGPDFPageGetBoxRect(CGPDFDocumentGetPage(myDocumentRef, page), kCGPDFCropBox));
CATiledLayer *tiledLayer = [CATiledLayer layer];
tiledLayer.delegate = self;
tiledLayer.tileSize = CGSizeMake(593.0, 370.0);
tiledLayer.levelsOfDetail = 6;
tiledLayer.levelsOfDetailBias = 2;
tiledLayer.frame = pageRect;
myContentView = [[UIView alloc] initWithFrame:pageRect];
[myContentView.layer addSublayer:tiledLayer];
CGRect viewFrame = CGRectMake(0.0, 0.0, 480.0, 300.0);
viewFrame.origin = CGPointZero;
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:viewFrame];
scrollView.delegate = self;
scrollView.contentSize = pageRect.size;
scrollView.maximumZoomScale = 2;
scrollView.minimumZoomScale = 0.8094;
scrollView.zoomScale = 0.8094;
[scrollView addSubview:myContentView];
UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[nextButton addTarget:self action:@selector(nextPage)
forControlEvents:UIControlEventTouchUpInside];
[nextButton setTitle:@"Next" forState:UIControlStateNormal];
nextButton.frame = CGRectMake(425.0, 255.0, 50.0, 20.0);//425,275,50,20
nextButton.alpha = 0.8;
[self.view addSubview:scrollView];
[self.view addSubview:nextButton];
}
- (void) nextPage
{
NSLog(@"Next Page");
page = 3;
[self setNeedsDisplay];
BOOL displayNeeded = [self needsDisplay];
if(displayNeeded)
NSLog(@"Display Needed");
[self displayIfNeeded];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return myContentView;
}
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0);
CGContextFillRect(ctx, CGContextGetClipBoundingBox(ctx));
CGContextTranslateCTM(ctx, 0.0, layer.bounds.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(CGPDFDocumentGetPage(myDocumentRef, page), kCGPDFCropBox, layer.bounds, 0, true));
CGContextDrawPDFPage(ctx, CGPDFDocumentGetPage(myDocumentRef, page));
}
@end
I have code here that displays a PDF page. There's a button that supposedly will draw the next page of the PDF document. I have tried to -setNeedsDisplay on almost everything but it never seemed to update. I'm not trying to draw it offscreen so it should update correctly, right?
I don't have much understanding on Core Animation so I'm quite lost. My understanding is that -drawLayer will only draw when it needs to after you -setNeedsDisplay. Is this why nothing updates? Can you force it to draw?